我正在使用OPENROWSET (BULK...)
插入,我将一个小的 XML 文件插入到表中,然后最终粉碎和转换它。以下工作正常(非常感谢其他帖子)。
DECLARE @v_inputfile AS varchar(50)
DECLARE @v_xmlfile AS xml
DECLARE @v_sql AS nvarchar(1000)
DECLARE @v_loadeddatetime AS DATETIME
SET @v_inputfile='E:\inputfile.xml'
SET @v_sql= 'INSERT INTO'+
' Tbl1_XMLStaging1 (XMLData, LoadedDateTime)' +
' SELECT CONVERT(XML, BulkColumn), ' +
' GETDATE() ' +
'FROM OPENROWSET(BULK '''+ @v_inputfile +''', SINGLE_BLOB) AS x;'
EXEC(@v_sql);
但是,我的目标是将 InputFileName 也插入到 Tbl1_XMLStaging1 中,但是我遇到了 Select 语句中的 @v_inputfile 作为变量传递到查询中的问题。
我尝试了以下方法:
DECLARE @v_inputfile AS varchar(50)
DECLARE @v_xmlfile AS xml
DECLARE @v_sql AS nvarchar(1000)
DECLARE @v_loadeddatetime AS DATETIME
SET @v_inputfile='E:\inputfile.xml'
SET @v_sql= 'INSERT INTO'+
' Tbl1_XMLStaging1 (XMLData, LoadedDateTime, XMLFileName)' +
' SELECT CONVERT(XML, BulkColumn), ' +
' GETDATE(), ' +
'@v_inputfile ' +
'FROM OPENROWSET(BULK '''+ @v_inputfile +''', SINGLE_BLOB) AS x;'
EXEC(@v_sql);
我收到各种错误消息:
必须声明标量变量“@v_inputfile”
如果我删除变量周围的单引号,则会出现错误
'E:' 附近的语法不正确
我已经梳理了之前的几篇文章,如果不进行整个其他操作,似乎无法克服这一点。
回应后。我正在尝试以下并收到错误
', 附近的语法不正确
代码:
DECLARE @v_inputfile AS varchar(50)
DECLARE @v_xmlfile AS xml
DECLARE @v_sql AS nvarchar(1000)
DECLARE @v_stmt AS nvarchar(1000)
DECLARE @params AS nvarchar(100)
DECLARE @v_loadeddatetime AS DATETIME
SET @v_inputfile='E:\inputfile.xml'
SET @v_sql= 'INSERT INTO'+
' Tbl1_XMLStaging1 (XMLData, LoadedDateTime, XMLFileName)' +
' SELECT CONVERT(XML, BulkColumn), ' +
' GETDATE(),' +
' @v_inputfile ' +
'FROM OPENROWSET(BULK '''+ @v_inputfile +''', SINGLE_BLOB) AS x;'
EXEC @v_stmt = @v_sql, @params = N'@v_inputfile varchar(50)', @v_inputfile;