1

我需要解析我的 XML 文件并将解析的 XML 保存到 SQL 数据库。我的 XML 内容有几个标签<goods_item>。如果它只包含一个标签,它工作正常。但其他方面也有错误。

SQL查询:

declare @insert nvarchar(4000), @pathname varchar(200), @xml xml
set @pathname = 'D:\Tests\price1.xml'
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

INSERT INTO XML_import
SELECT 
Tbl.Col.value('goods_level[1]','int'),
Tbl.Col.value('goods_isgroup[1]', 'NVARCHAR(50)'),
Tbl.Col.value('goods_code[1]', 'NVARCHAR(50)'),
Tbl.Col.value('goods_name[1]', 'NVARCHAR(50)'),
Tbl.Col.value('goods_parent[1]', 'NVARCHAR(50)'),
Tbl.Col.value('goods_art[1]', 'NVARCHAR(50)'),
Tbl.Col.value('goods_buhart[1]', 'NVARCHAR(50)'),
Tbl.Col.value('goods_inpack[1]', 'NVARCHAR(50)'),
Tbl.Col.value('goods_packtxt[1]', 'NVARCHAR(50)'),
Tbl.Col.value('goods_country[1]', 'NVARCHAR(50)'),
Tbl.Col.value('goods_gtd[1]', 'NVARCHAR(50)'),
Tbl.Col.value('goods_nds[1]', 'NVARCHAR(50)'),
Tbl.Col.value('goods_unit[1]', 'NVARCHAR(50)')
FROM @xml.nodes('//goods_item') Tbl(Col)

drop table #tmp_xml

和 XML:

<data_exchange>

<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</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</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>

</data_exchange>

这是我的第一个 SQL 项目,也许我很笨 :)

4

1 回答 1

1

如果我是你,我会把代码改成这样:干杯

Declare @Xml    Xml

Select  @Xml    =   Convert(Xml, bulkcolumn )
    From    OpenRowSet  (
        Bulk    N'D:\Tests\price1.xml'
    ,   Single_Blob
    );

If  (
    Object_Id('XML_import') Is  Null
)   Select  Tbl.Col.value('goods_level[1]',     'Int')
    ,   Tbl.Col.value('goods_isgroup[1]',   'NVARCHAR(50)')
    ,   Tbl.Col.value('goods_code[1]',      'NVARCHAR(50)')
    ,   Tbl.Col.value('goods_name[1]',      'NVARCHAR(50)')
    ,   Tbl.Col.value('goods_parent[1]',    'NVARCHAR(50)')
    ,   Tbl.Col.value('goods_art[1]',       'NVARCHAR(50)')
    ,   Tbl.Col.value('goods_buhart[1]',    'NVARCHAR(50)')
    ,   Tbl.Col.value('goods_inpack[1]',    'NVARCHAR(50)')
    ,   Tbl.Col.value('goods_packtxt[1]',   'NVARCHAR(50)')
    ,   Tbl.Col.value('goods_country[1]',   'NVARCHAR(50)')
    ,   Tbl.Col.value('goods_gtd[1]',       'NVARCHAR(50)')
    ,   Tbl.Col.value('goods_nds[1]',       'NVARCHAR(50)')
    ,   Tbl.Col.value('goods_unit[1]',      'NVARCHAR(50)')
        Into    XML_import
        From    @Xml.nodes('//goods_item')  Tbl(Col)
Else    Insert  XML_import
        Select  Tbl.Col.value('goods_level[1]',     'Int')
        ,   Tbl.Col.value('goods_isgroup[1]',   'NVARCHAR(50)')
        ,   Tbl.Col.value('goods_code[1]',      'NVARCHAR(50)')
        ,   Tbl.Col.value('goods_name[1]',      'NVARCHAR(50)')
        ,   Tbl.Col.value('goods_parent[1]',    'NVARCHAR(50)')
        ,   Tbl.Col.value('goods_art[1]',       'NVARCHAR(50)')
        ,   Tbl.Col.value('goods_buhart[1]',    'NVARCHAR(50)')
        ,   Tbl.Col.value('goods_inpack[1]',    'NVARCHAR(50)')
        ,   Tbl.Col.value('goods_packtxt[1]',   'NVARCHAR(50)')
        ,   Tbl.Col.value('goods_country[1]',   'NVARCHAR(50)')
        ,   Tbl.Col.value('goods_gtd[1]',       'NVARCHAR(50)')
        ,   Tbl.Col.value('goods_nds[1]',       'NVARCHAR(50)')
        ,   Tbl.Col.value('goods_unit[1]',      'NVARCHAR(50)')
            From    @Xml.nodes('//goods_item')  Tbl(Col)
于 2012-09-06T14:28:14.377 回答