1

I just want to ask if there is a way to use the bulk insert feature but to check if the last line is empty and skip it.

I have a text file that is being populated with data but the last line will always be empty cause when it repopulates, it will start from there and the the end of the previous line that is already populated.

My query so far looks like this:

BULK INSERT #TEMP 
FROM 'C:\Test\Test.txt'
WITH (FIELDTERMINATOR ='\t', 
      ROWTERMINATOR = '\r', 
      FIRSTROW = 2, KEEPNULLS)

It will then be input into a temp table but the query will not go this far because of the last line in the text file. Is there a setting to say, skip the last line if its empty?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Christoph Bethge
  • 217
  • 1
  • 5
  • 20

2 Answers2

0

If you're expecting one line to be nonimportable then include

MAXERRORS =1

In the command and BULK INSERT should import the other ones fine.

BULK INSERT #TEMP 
FROM 'C:\Test\Test.txt'
WITH (FIELDTERMINATOR ='\t', 
      ROWTERMINATOR = '\r', 
      MAXERRORS =1,
      FIRSTROW = 2, KEEPNULLS)

Unless there's an unexpected problem in another line (in which case you'll want to trap the error anyway)

https://msdn.microsoft.com/en-us/library/ms188365.aspx

Tom Page
  • 1,211
  • 1
  • 7
  • 8
-1

Well i found my answer here:Difference between CR LF, LF and CR line break types?

What i did is changed the from ROWTERMINATOR = '\r' to ROWTERMINATOR = '0x0a'

You can read up on the link about the differences in end of line chars which soved my issue. I hope this works for others as well! Best of luck guys!

Community
  • 1
  • 1
Christoph Bethge
  • 217
  • 1
  • 5
  • 20