Bulk insert, SQL Server 2000, unix linebreaks

后端 未结 8 956
旧时难觅i
旧时难觅i 2020-12-13 13:05

I am trying to insert a .csv file into a database with unix linebreaks. The command I am running is:

BULK INSERT table_name
FROM \'C:\\file.csv\' 
WITH 
( 
         


        
8条回答
  •  旧时难觅i
    2020-12-13 13:42

    Thanks to all who have answered but I found my preferred solution.

    When you tell SQL Server ROWTERMINATOR='\n' it interprets this as meaning the default row terminator under Windows which is actually "\r\n" (using C/C++ notation). If your row terminator is really just "\n" you will have to use the dynamic SQL shown below.

    DECLARE @bulk_cmd varchar(1000)
    SET @bulk_cmd = 'BULK INSERT table_name
    FROM ''C:\file.csv''
    WITH (FIELDTERMINATOR = '','', ROWTERMINATOR = '''+CHAR(10)+''')'
    EXEC (@bulk_cmd)
    

    Why you can't say BULK INSERT ...(ROWTERMINATOR = CHAR(10)) is beyond me. It doesn't look like you can evaluate any expressions in the WITH section of the command.

    What the above does is create a string of the command and execute that. Neatly sidestepping the need to create an additional file or go through extra steps.

提交回复
热议问题