Bulk insert using stored procedure

后端 未结 4 1323
醉酒成梦
醉酒成梦 2020-11-30 08:29

I have a query which is working fine:

BULK INSERT ZIPCodes 
FROM  \'e:\\5-digit Commercial.csv\' 
WITH 
( 
     FIRSTROW = 2 ,
    FIELDTERMINATOR = \',\', 
         


        
4条回答
  •  猫巷女王i
    2020-11-30 08:55

    There is an alternative to dynamic SQL if you have access to the SQLCmd exe.

    The SqlCmd utility allows you to pass string replacement variables using the -v argument.

    You can use a template variable named "filepath" which will be replaced when you execute the script via the cmdline.

    The SQL script would look like:

    BULK INSERT ZIPCodes 
    FROM  '$(filepath)' 
    WITH 
    ( 
         FIRSTROW = 2 ,
        FIELDTERMINATOR = ',', 
        ROWTERMINATOR = '\n' 
    )
    end
    

    You would then execute the script from a commandline using something like the following:

    sqlcmd -b -S SERVER\INSTANCEHERE -E -i "PATH\FILENAMEHERE.Sql" -v FilePath = "e:\5-digit Commercial.csv" -s "|"
    

    The important part of the example is the -v argument:

    -v FilePath = "e:\5-digit Commercial.csv"
    

提交回复
热议问题