2

我有这个 SQL 脚本可以正常工作:

INSERT INTO #XMLwithOpenXML(XMLData, LoadedDateTime)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() 
FROM OPENROWSET(BULK 'C:\temp\test.wordpress.2017-05-22.xml', SINGLE_BLOB) AS x;


SELECT @XML = XMLData FROM #XMLwithOpenXML

现在我需要使用变量传递 xml 文件路径。

我该如何更改脚本?

感谢支持

4

1 回答 1

3

您可以使用动态 SQL:

create table #XMLwithOpenXML(XMLData xml, LoadedDateTime DateTime)

declare @xml xml
declare @filename nvarchar(100)
declare @sql nvarchar(max)

set  @filename ='F:\a.xml'
set  @sql = 'INSERT INTO #XMLwithOpenXML(XMLData, LoadedDateTime) '
set  @sql = @sql +' SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() '
set  @sql = @sql +' FROM OPENROWSET(BULK ''' + @filename +''', SINGLE_BLOB) AS x;'

EXEC (@Sql)

SELECT @XML = XMLData FROM #XMLwithOpenXML

SELECT @XML

drop table #XMLwithOpenXML
于 2017-06-22T10:48:07.070 回答