【问题标题】:XML SQL Inner selectXML SQL 内部选择
【发布时间】:2018-03-08 17:18:25
【问题描述】:

我在将我从外部系统收到的 XML(即我无法更改格式)转换为我可以插入到我的 MS SQL 表中的内容时遇到问题。

我需要从 XML 文件中提取不同的语言文本。

这是我目前拥有的代码:

`DECLARE @articleNumber         NVARCHAR  (30);
DECLARE @articleShortDescription_DEU    NVARCHAR (100);
DECLARE @articleShortDescription_ENG    NVARCHAR (100);

SELECT 
  @articleNumber = ParamValues.myObj.value('(ItemMasterHeader/ItemID/ID)[1]',   'nvarchar  (30)'),
  @articleShortDescription_DEU = ParamValues.myObj.value('(ItemMasterHeader/Description)[1]', 'nvarchar (100)'),
  @articleShortDescription_ENG = ParamValues.myObj.value('(ItemMasterHeader/Description/@languageID)[1]','nvarchar (100)')
FROM @xmlData.nodes('/DataArea/ItemMaster') AS ParamValues(myObj);  

INSERT INTO Article (ArticleNumber, ArticleShortDescription_DEU, ArticleShortDescription_ENG)
SELECT @articleNumber, @articleShortDescription_DEU, @articleShortDescription_ENG`

这是我拥有的 XML 文件:

<ItemMaster>
<ItemMasterHeader>
  <ItemID>
    <ID accountingEntity="999" lid="lid://infor.ln.test_xxx" variationID="1111111">9999999</ID>
  </ItemID>

  <Description>Main text</Description>

  <Description languageID="de_DE">DE text</Description>
  <Description languageID="es_ES">ES text</Description>
  <Description languageID="fr_FR">FR text</Description>
  <Description languageID="it_IT">IT text</Description>
  <Description languageID="pl_PL">PL text</Description>
  <Description languageID="en_US">US text</Description>
  </ItemMasterHeader>

@articleShortDescription_DEU 包含预期的“主要文本”,@articleShortDescription_ENG 包含“de_DE”(这也是预期的)。我需要@articleShortDescription_ENG 字段中的“en_US”文本(在本例中为“US text”)的值。

关于如何做到这一点的任何想法?

【问题讨论】:

    标签: xml sql-server-2016


    【解决方案1】:

    我在这里找到了答案:How to get a particular attribute from XML element in SQL Server

    基本上是这样的:

    @articleShortDescription_ENG = ParamValues.myObj.value('(ItemMasterHeader/Description[@languageID="en_US"])[1]' , 'nvarchar (100)')

    【讨论】:

      猜你喜欢
      • 2018-06-19
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2021-08-19
      相关资源
      最近更新 更多