SET @xml = file_path

时间:2012-08-16 14:54:21

标签: xml database import sql-server-2008-r2

我需要将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'

1 个答案:

答案 0 :(得分: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