Loading text files is a skill that probably every DBA needs. I know that the import wizard is available, but there are times that you might want to automate this without using SSIS. In those cases, it’s nice to know how to load data from a programmatic standpoint.
I had the need to do this recently with a text file that looked normal. When I opened it in my editor, it looked like a normal text file, one column of data.
I thought this would be easy to load, so I created a simple table:
CREATE TABLE MyTable ( teststring VARCHAR(100))
I then ran a simple BULK INSERT command.
BULK insert MyTable
And I received this:
Msg 4863, Level 16, State 1, Line 3
Bulk load data conversion error (truncation) for row 1, column 1 (mychar).
That’s not good. I suspected this was because of the format of the file, so I added a row terminator.
BULK insert MyTable
with ( ROWTERMINATOR = ‘\r’)
That didn’t help. I suspected this was because of the terminators for some reason. I also tried the newline (\n) terminator, and both, but nothing worked.
Since I was worried about formatting, I decided to look at the file. My first choice here is XVI32, and when I opened the file, I could see that only a line feed (0x0A) was used.
However, I wasn’t sure how to get this in my code.
I tried CHAR(), and that didn’t work.
I could look to edit the code with XVI32, but that seems odd. However, let’s try that.
I replaced the \r with 0x0A and then deleted the r. Once I saved this, and reloaded into SSMS (do not normalize to CRLF), I could run this.
I suppose I could also do this with the ALT key, and a number pad, though I couldn’t get that to work on my laptop. I need to try that on my desktop, but it’s not a great way to code. Easy to forget that characters are in the code.
I tried searching a bit and found that SQLDenis had a solution. He used dynamic SQL, but with a little formatting, the code is still easy to read, and this works fine.
DECLARE @cmd VARCHAR(8000)
SELECT @cmd = ‘BULK insert Mytable
with ( ROWTERMINATOR = ”’ + Char(10) + ”’)’
I executed this and loaded my file just fine.
It’s not often you might need to do this, but it’s a handy little trick for those files that might be formatted from other OSes.