日期:2014-05-17  浏览次数:20672 次

关于BULK变量的问题
SQL code

BULK INSERT #TMP1
FROM 'D:\盘点单\BM20120815SBK.txt' 
WITH 
 (   FIELDTERMINATOR =',',
     ROWTERMINATOR ='\n',
     KILOBYTES_PER_BATCH=5000
 )


如果我的文件名是写死的没有问题
如果换成变量该怎么处理呢
SQL code

DECLARE @fileName NVARCHAR(50)
SET @fileName='D:\盘点单\BM20120815SBK.txt' 

BULK INSERT #TMP1
FROM 'D:\盘点单\BM20120815SBK.txt' 
WITH 
 (   FIELDTERMINATOR =',',
     ROWTERMINATOR ='\n',
     KILOBYTES_PER_BATCH=5000
 )



------解决方案--------------------
SQL code

--这种只能用动态sql
DECLARE @fileName NVARCHAR(50),@sql nvarchar(1000)
SET @fileName='D:\盘点单\BM20120815SBK.txt' 

select @sql='BULK INSERT #TMP1
FROM '''+@fileName+''' 
WITH 
 (   FIELDTERMINATOR ='','',
     ROWTERMINATOR =''\n'',
     KILOBYTES_PER_BATCH=5000
 )'
exec(@sql)