我在SP,SQL Server 2005中使用以下代码
declare @path varchar(500)
set @path = 'E:\Support\test.csv';
print @path
Create table #mytable(
name varchar(max), class varchar(max), roll varchar(max)
)
BULK INSERT #mytable FROM @path
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);
Go
select * from #mytable
drop table #mytable
但它引发了以下错误:
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near '@path'.
Msg 319, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'with'.
If this statement is a common table expression or an xmlnamespaces clause,
the previous statement must be terminated with a semicolon.
Msg 208, Level 16, State 0, Line 1
Invalid object name '#mytable'.
有人可以帮助我。
答案 0 :(得分:0)
您无法执行以下操作
BULK INSERT #mytable FROM @path
如果您希望将其翻译为
BULK INSERT #mytable FROM 'E:\Support\test.csv'
这不是varchar中的文件名,SQL认为@Path 是数据,而不是包含路径的字符串。
如果需要为路径使用变量,则需要使用一些粗略转换为(借口语法错误)的动态SQL
DECLARE @SQL varchar(max)
SET @SQL = 'BULK INSERT #mytable FROM '+ @path + '
--Add the rest of your code here
EXEC (@SQL)
如果你的变量永远不会改变,我会继续将字符串粘贴到语句本身。
希望有所帮助。