Cómo convertir variables en T-SQL para la inserción masiva?
El siguiente código da un error (es parte de un procedimiento almacenado T-SQL):
-- 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
)
El error es:
Incorrect syntax near the keyword 'with'.
Si reemplazo:
FROM @CSVfile
con
FROM 'T:\x.csv'
... entonces funciona bien.