3

我需要将 XML 文件导入 SQL Server 2008 R2 数据库。

因此,我使用硬编码的 XML 数据进行了查询:

DECLARE @xml XML
SET @xml='
<goods_item>
<goods_level>1</goods_level>
<goods_isgroup>1</goods_isgroup>
<goods_code>51852</goods_code>
<goods_name></goods_name>
<goods_parent></goods_parent>
<goods_art></goods_art>
<goods_buhart></goods_buhart>
<goods_inpack>0</goods_inpack>
<goods_packtxt></goods_packtxt>
<goods_country></goods_country>
<goods_gtd></goods_gtd>
<goods_nds>0</goods_nds>
<goods_unit></goods_unit>
</goods_item>
<goods_item>
<goods_level>1</g    oods_level>
<goods_isgroup>1</goods_isgroup>
<goods_code>51852</goods_code>
<goods_name></goods_name>
<goods_parent></goods_parent>
<goods_art></goods_art>
<goods_buhart></goods_buhart>
<goods_inpack>0</goods_inpack>
<goods_packtxt></goods_packtxt>
<goods_country></goods_country>
<goods_gtd></goods_gtd>
<goods_nds>0</goods_nds>
<goods_unit></goods_unit>
</goods_item>'

它工作正常。但是如何将硬编码 XML 数据更改为 XML 文件的路径?

关于这个:

SET @xml='D:\xml1.xml'
4

1 回答 1

1

您可以先将其导入到临时表中,如下所示:

declare @insert nvarchar(1000), @pathname varchar(200), @xml xml

set @pathname = 'your path here'

create table #tmp_xml(xmldata xml)

set @insert = 'INSERT INTO #tmp_xml(xmldata) SELECT bulkcolumn 
      FROM OPENROWSET (BULK N''' + @pathname+ ''', SINGLE_BLOB) AS data'

exec sp_executesql @insert

select @xml = xmldata from #tmp_xml

-- read, parse xml here ....
drop table #tmp_xml
于 2012-08-16T15:04:35.750 回答