I am trying to insert data from multiple files into SQL Server. This is the code I am using–
DECLARE @MyCounter int;
DECLARE @Fileprefix nvarchar, @Filesuffix nvarchar, @fullname nvarchar, @Counter_string nvarchar;
SET @MyCounter = 1;
SET @Fileprefix= 'C:\Arvind_gpd\patents\';
SET @Filesuffix='data_corrected.csv';
WHILE (@MyCounter < 10)
BEGIN;
Set @Counter_string= Cast(@MyCounter AS varchar(1) );
Set @fullname = (@Fileprefix+ @Counter_string + @Filesuffix );
BULK INSERT GooglePatentsIndividualDec2012.dbo.patent
FROM @fullname WITH ( DATAFILETYPE = 'char', FIELDTERMINATOR = '^', ROWTERMINATOR = '\n' );
SET @MyCounter = @MyCounter + 1;
END;
GO
However I am getting these error messages–
Incorrect syntax near @fullname. Expecting Integer, String, TEXT_LEX.....
Incorrect syntax near DATAFILETYPE. Expecting SELECT or '('
What am I doing wrong in the above query?
You cannot use a variable as a table name. If you want to use a variable you have to create a string and use the
EXECfunction like this: