Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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 ajoutée par marwa khalaf , .Net Developer , Sands National Academy
Date de publication: 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
par 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