我有一个没有变量的查询。它按预期工作:
INSERT INTO XMLwithOpenXML(XMLData, LoadedDateTime)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE()
FROM OPENROWSET(BULK 'C:\xml\hamlet.xml', SINGLE_BLOB) AS x;
但是当我添加变量时。它不起作用(我用变量替换了一个字符串)。我收到此错误=> ' @ path'附近的语法不正确。
DECLARE @path varchar(50) = 'C:\xml\hamlet.xml';
INSERT INTO XMLwithOpenXML(XMLData, LoadedDateTime)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE()
FROM OPENROWSET(BULK @path, SINGLE_BLOB) AS x;
有谁知道这里有什么问题?
答案 0 :(得分:2)
参数化的动态SQL查询可能无效。尝试明确地连接路径:
DECLARE @path varchar(50) = 'C:\xml\hamlet.xml',
@sql nvarchar(max)= ''
set @sql = '
INSERT INTO XMLwithOpenXML(XMLData, LoadedDateTime)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE()
FROM OPENROWSET(BULK ''' + @path +''', SINGLE_BLOB) AS x;'
exec sp_executesql @sql,N''