Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How can I use bulk insert in sql server with a variable file name, ex:@file_name varchar(max)bulk insert Table_Namefrom @file_name?

user-image
Question added by marwa khalaf , .Net Developer , Sands National Academy
Date Posted: 2013/06/27

The syntax for BULK INSERT statement is : BULK INSERT [ database_name.
[ schema_name ] .
| schema_name.
] [ table_name | view_name ] FROM 'data_file' [ WITH So, the file name must be a string constant.
To solve the problem please use dynamic SQL: DECLARE @sql NVARCHAR(4000) = 'BULK INSERT TblValues FROM ''' + @FileName + ''' WITH ( FIELDTERMINATOR ='','', ROWTERMINATOR =''\n'' )'; EXEC(@sql); it may help :)

Anshul Tiwari
by Anshul Tiwari , Software Developer , RTSPL

For it use dynamic sql DECLARE @sql AS NVARCHAR(1000) SET @sql = 'BULK INSERT ' + @tableName + ' FROM ''' + @filename + ''' WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' )' EXEC (@sql)

More Questions Like This