简体   繁体   English

使用 OPENROWSET 从 XML 检索属性时,将 XML 文档中的数据提取到 SQL 表中,空表

[英]Fetching data from XML document into SQL table, empty table when using OPENROWSET for retrieving attribute from XML

I need to insert attributes from a XML file into a SQL table.我需要将 XML 文件中的属性插入到 SQL 表中。 I have roamed Stackoverflow to find a solution but nothing I tried seems to work.我已经漫游 Stackoverflow 以找到解决方案,但我尝试的任何方法似乎都不起作用。

I have tried with and without defining and using the namespace and also all kind of different node-path combinations for navigating through the XML.我已经尝试过定义和使用命名空间以及各种不同的节点路径组合来浏览 XML。

Here is a part of the Xml which I would like to retrieve the attributes from:这是我想从中检索属性的 Xml 的一部分:

``<export xmlns="http://www.arcticgroup.se/tariff/arctictariff/export" xmlns:at="http://www.arcticgroup.se/tariff/arctictariff/export" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.arcticgroup.se/tariff/arctictariff/export GoodsNomenclature.xsd ">
  <id>e37c6378-892a-4c25-b3fe-d0f3fcdf5a29</id>
  <exportType>GoodsNomenclatureObject</exportType>
  <parameters />
  <items>
    <goodsNomenclature at:goodsNomenclatureCode="0101109090" at:dateEnd="2011-06-30" at:national="0" at:productLineSuffix="80" at:SID="74778" at:dateStart="2002-01-01" at:statisticalIndicator="0" at:changeType="U">
      <goodsNomenclatureIndent at:national="0" at:quantityIndents="03" at:SID="74084" at:dateStart="2002-01-01" />
      <goodsNomenclatureDescriptionPeriod at:national="0" at:SID="92833" at:dateStart="2002-01-01">
        <goodsNomenclatureDescription at:description="andere" at:languageId="NL" at:national="0" />
        <goodsNomenclatureDescription at:description="Other" at:languageId="EN" at:national="0" />
      </goodsNomenclatureDescriptionPeriod>
    </goodsNomenclature>
  </items>
</export>``

This is the last code which I tried:这是我尝试的最后一个代码:

INSERT INTO TblGoodsNomenclature(XMLData, CreatedDate)
SELECT Convert(Xml, BulkColumn) as BulkColumn, GETDATE()
FROM OPENROWSET(Bulk '\\shareapp\c$\temp\GoodsNomenclature.xml', Single_Blob) as x;

DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)

SELECT @XML = XmlData FROM TblGoodsNomenclature

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML, '<export xmlns="http://www.arcticgroup.se/tariff/arctictariff/export" xmlns:at="http://www.arcticgroup.se/tariff/arctictariff/export"/>'

INSERT INTO NomenclatureCodes (NomenclatureCode, Description, DescriptionLanguage)
SELECT NomenclatureCode, Description, DescriptionLanguage
    FROM OPENXML(@hDoc, '/items/goodsNomenclature/goodsNomenclatureDescriptionPeriod/goodsNomenclatureDescription/at:node')
    WITH 
    (
        NomenclatureCode INT '@at:goodsNomenclatureCode',
        Description [varchar](100) '@at:description',
        DescriptionLanguage [varchar](5) '@at:languageID'

    )

The result which I am getting now is just an empty table, no error messages.我现在得到的结果只是一个空表,没有错误消息。 What I would like to have is the 'goodsNomeclatureCode', the 'description' and the 'languageID' in a table.我想要的是表格中的“goodsNomeclatureCode”、“description”和“languageID”。

As mentioned in the comments, you're far better off using XQUERY here.正如评论中提到的,您最好在这里使用 XQUERY。 As you have multiple namespaces as well we need to declare them too.由于您也有多个命名空间,我们也需要声明它们。 This gives you the following query:这为您提供以下查询:

WITH XMLNAMESPACES (DEFAULT 'http://www.arcticgroup.se/tariff/arctictariff/export',
                    'http://www.arcticgroup.se/tariff/arctictariff/export' AS [at])
SELECT GN.XMLData,
       EI.gN.value('@at:goodsNomenclatureCode[1]','varchar(15)') AS goodsNomenclatureCode
FROM dbo.TblGoodsNomenclature GN
     CROSS APPLY GN.XMLData.nodes('export/items/goodsNomenclature') EI(gN);

db<>fiddle 数据库<>小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM