Bulk Insert with filename parameter [duplicate]

为君一笑 提交于 2019-12-17 16:27:22

问题


I need to load a couple of thousands of data files into SQL Server table. So I write a stored procedure that receives just one parameter - file name. But.. The following doesn't work.. The "compiler" complains on @FileName parameter.. It wants just plain string.. like 'file.txt'. Thanks in advance.

Ilan.

BULK INSERT TblValues
FROM @FileName
WITH 
(
FIELDTERMINATOR =',',
ROWTERMINATOR ='\n'
)

回答1:


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);


来源:https://stackoverflow.com/questions/7306616/bulk-insert-with-filename-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!