SQL SERVER - 带有filepath参数的OPENROWSET

时间:2017-06-22 09:29:07

标签: sql-server openrowset xml-import

我有这个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文件路径。

如何更改脚本?

感谢支持

1 个答案:

答案 0 :(得分:2)

您可以使用动态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