T-SQL Bulk insert not sorting first row at the top

58 Views Asked by At

The intention of the below script is to BULK INSERT from a file and always retrieve the first row of the File.csv because I need the column names that reside in the first row.

Here's an example of the file contents

SiteCode,ItemCode
1,ABC
2,DSG
1,TGB

As you can see this file is comma delimited, and therefore the desired output of the below BULK INSERT script should be "SiteCode,ItemCode" without quotes. File.csv has a ROWTERMINATOR of CRLF and the encoding is UTF-8.

The below script works perfectly with files that have smaller number of rows, however, it intermittently does not return the column headings (first row) when there are more rows. For example, if you take a file with the same structure as in the example above but with 2462 rows the desired output will work every time the script is run.

However, adding just 1 additional row to make it 2463 rows will NOT return the first row every time the script is run. The weird thing is, almost like clockwork, it will produce the desired result 5 times and fail every 6th time

Here is the simple BULK INSERT script

CREATE TABLE StagingFirstRow (RowData NVARCHAR(MAX));

BULK INSERT StagingFirstRow
FROM 'C:\TEST\File.csv'
WITH (
    FIELDTERMINATOR = ',',
    ROWTERMINATOR = '0x0D0A',
    CODEPAGE = '65001',
    FIRSTROW = 1
)

SELECT TOP 1 * FROM StagingFirstRow
DROP TABLE StagingFirstRow

This is of course a simplified version of my script - My original script iterates through multiple files that I need retrieve column names from but I was still able to produce the issue with the basic script above.

Any thoughts on why this is happening?
All I need is to retrieve the first row of the file (which is the column headings) every single time the script it run.

0

There are 0 best solutions below