0

I am trying to do a bulk insert but the @CSVPath is not resolving.

 declare @path varchar(255) 
 set @path = 'C\CSVPath.csv'; 

 BULK INSERT #mytable FROM @CSVPath <-- Error line
 WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' ); 

I've tried

 FROM ''' + @CSVpath + '''

If I hard code the path it works. If I wrap it all in a SET statement and execute it works.

 declare @sql varchar(max)
 set @sql = 'BULK INSERT #mytable FROM ''' + @CSVPath + ''' WITH ...
 exec (@sql)

However, I cannot do it this way and need to it the first method but it doesn't seem to be resolving and cannot figure how to get it to work.

  • `data file` can't be passed as parameter – eshirvana Jul 26 '21 at 18:01
  • Does this answer your question? [How to cast variables in T-SQL for bulk insert?](https://stackoverflow.com/questions/5019041/how-to-cast-variables-in-t-sql-for-bulk-insert) – eshirvana Jul 26 '21 at 18:01

1 Answers1

0

properly When running query dynamics, the compiler stores the values of the variable, but does not normally name the table or address through the variable.

 BULK INSERT #mytable
 FROM 'C:\test.csv'
 WITH
 (
    FIRSTROW = 2, -- as 1st one is header
    FIELDTERMINATOR = ',',  -- field delimiter
    ROWTERMINATOR = '\n',   -- next row
    TABLOCK
 )
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17