0

I seem to be unable to run a bulk insert on a SQL table from a CSV file that gets sent through FTP to our server. It runs without error, but alters 0 rows.

If I copy the data into another file, it works, but I need to be able to do this automatically without messing with the file myself. Opening them both, the only differences I can see are that the line breaks are CRLF on the new file, and just LF on the original. Encoding looks to be the same as well on both, so I must be missing something not in the other similar questions asked.

Sample script below:

    BULK INSERT dbo.t_process_order_import
        FROM 'C:\Root\Product Data\H888 ProcOrd.csv'
        WITH
        (
            FIRSTROW = 2, -- as 1st one is header
            FIELDTERMINATOR = '|',
            ROWTERMINATOR = '\n',
            TABLOCK
        )

Sample CSV Data:

ProcessOrder|ProductNumber|MaterialDescription|OrderQuantity|WorkCenter|StartDate|StartTime
001001101111|000000000000101111|TEST|40500.000 |CKET02|20201014|220000
001001221111|000000000000101111|TEST|14124.000 |GHFD02|20210225|032000
user_0
  • 3,173
  • 20
  • 33

1 Answers1

1

You need to use ROWTERMINATOR='0x0a'
Your code will become:

BULK INSERT dbo.t_process_order_import
    FROM 'C:\Root\Product Data\H888 ProcOrd.csv'
    WITH
    (
        FIRSTROW = 2, -- as 1st one is header
        FIELDTERMINATOR = '|',
        ROWTERMINATOR = '0x0a',
        TABLOCK
    )

As suggested, I try to improve with my source: https://learn.microsoft.com/en-us/sql/relational-databases/import-export/specify-field-and-row-terminators-sql-server?view=sql-server-ver16

At paragraph "Specifying \n as a Row Terminator for Bulk Import"

Reporting here what is important for the question:

When you specify \n as a row terminator for bulk import, or implicitly use the default row terminator, bcp and the BULK INSERT statement expect a carriage return-line feed combination (CRLF) as the row terminator. If your source file uses a line feed character only (LF) as the row terminator - as is typical in files generated on Unix and Linux computers - use hexadecimal notation to specify the LF row terminator. For example, in a BULK INSERT statement

user_0
  • 3,173
  • 20
  • 33