批量插入的文件位置的串联

时间:2013-11-06 09:18:12

标签: sql-server sql-server-2008 tsql

尝试通过引入全局常量来改进某些tsql批量插入代码:

declare @folder varchar(max)
declare @fileLocation varchar(max)
set @folder = 'C:\SomeFolder\'

set @fileLocation = @folder + 'Data.txt';
BULK INSERT #Table
FROM ''' + @fileLocation + '''
WITH (
    FIRSTROW = 2,
    FIELDTERMINATOR = '\t', 
    ROWTERMINATOR = '\n'
);

尝试FROM代码的每个变体都没有成功。有任何想法吗?感谢。

1 个答案:

答案 0 :(得分:1)

您可以将其转换为动态SQL:

declare @folder varchar(max)
declare @fileLocation varchar(max)
set @folder = 'C:\SomeFolder\'
declare @sql varchar(max)

set @fileLocation = @folder + 'Data.txt';
set @sql = 'BULK INSERT #Table
FROM ''' + @fileLocation + '''
WITH (
    FIRSTROW = 2,
    FIELDTERMINATOR = ''\t'', 
    ROWTERMINATOR = ''\n''
);'

exec(@sql)