title: How to cast variables in T-SQL for bulk insert?
body: <p>The following code gives an error (its part of a T-SQL stored procedure):</p>
  
  <pre><code>-- Bulk insert data from the .csv file into the staging table.
  DECLARE @CSVfile nvarchar(255);
  SET @CSVfile = N'T:\x.csv';
  BULK INSERT [dbo].[TStagingTable]
  -- FROM N'T:\x.csv' -- This line works
  FROM @CSVfile -- This line will not work
  WITH
  (
  FIELDTERMINATOR = ',',
  ROWTERMINATOR = '\n',
  FIRSTROW = 2    
  )
  </code></pre>
  
  <p>The error is:</p>
  
  <pre><code>Incorrect syntax near the keyword 'with'. 
  </code></pre>
  
  <p>If I replace:</p>
  
  <pre><code>FROM @CSVfile
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>FROM 'T:\x.csv'
  </code></pre>
  
  <p>... then it works nicely.</p>