How to cast variables in T-SQL for bulk insert?

后端 未结 6 1535
面向向阳花
面向向阳花 2020-11-30 10:20

The following code gives an error (its part of a T-SQL stored procedure):

-- Bulk insert data from the .csv file into the staging table.
DECLARE @CSVfile nva         


        
6条回答
  •  失恋的感觉
    2020-11-30 10:38

    As I know only literal string is required in the from. In that case you have to write a dynamic query to use bulk insert

    declare @q nvarchar(MAX);
    set @q=
        'BULK INSERT [TStagingTable]
        FROM '+char(39)+@CSVfile+char(39)+'
        WITH
        (
        FIELDTERMINATOR = '','',
        ROWTERMINATOR = ''\n'',
        FIRSTROW = 1  
        )'
    exec(@q)
    

提交回复
热议问题