簡體   English   中英

迭代xml節點SQL Server上的值

[英]Iterate values on xml nodes SQL Server

我正在嘗試從每個XML節點提取所有值。 例如對於<Cash>節點,我想恢復值(0,1),但無法獲取它

CREATE TABLE #TableXML (Col1 INT PRIMARY KEY, Col2 XML) 

INSERT INTO #TableXML 
VALUES (1,
'<CookedUP>
  <Evenement Calcul="16">
    <Cookie xmlns="http://services.ariel.morneausobeco.com/2007/02" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <AlternateOptionTypeCodes />
      <Cash>0</Cash>
      <CashInterest>0</CashInterest>
      <CashSource>Undefined</CashSource>
      <Code>A</Code>
      <SmallAmount>0</SmallAmount>
      <SmallAmountType>Undefined</SmallAmountType>
    </Cookie>
    <Cookie xmlns="http://services.ariel.morneausobeco.com/2007/02" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <AlternateOptionTypeCodes />
      <Cash>1</Cash>
      <CashInterest>2</CashInterest>
      <CashSource>Undefined</CashSource>
      <Code>B</Code>
      <SmallAmount>1</SmallAmount>
      <SmallAmountType>1</SmallAmountType>
    </Cookie>
  </Evenement>
</CookedUP> '
)

WITH XMLNAMESPACES ('http://services.ariel.morneausobeco.com/2007/02' AS ns)
SELECT 
    b.Col1,
    x.XmlCol.value('(ns:Cookie/ns:SmallAmount/text())[2]', 'int') AS SmallAmount,
    x.XmlCol.value('(ns:Cookie/ns:Cash/text())[2]', 'int') AS Cash
FROM
    #TableXML b
CROSS APPLY 
    b.Col2.nodes('CookedUP/Evenement') x(XmlCol);

稍微調整一下您已有的內容,就可以了。 直到Cookie節點,名稱空間才起作用。

我將示例更新為表變量,嘗試一下:

DECLARE @TableXML TABLE(Col1 int primary key, Col2 xml) 
Insert into @TableXML values ( 1,
'<CookedUP>
  <Evenement Calcul="16">
    <Cookie xmlns="http://services.ariel.morneausobeco.com/2007/02" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <AlternateOptionTypeCodes />
      <Cash>0</Cash>
      <CashInterest>0</CashInterest>
      <CashSource>Undefined</CashSource>
      <Code>A</Code>
      <SmallAmount>0</SmallAmount>
      <SmallAmountType>Undefined</SmallAmountType>
    </Cookie>
    <Cookie xmlns="http://services.ariel.morneausobeco.com/2007/02" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <AlternateOptionTypeCodes />
      <Cash>1</Cash>
      <CashInterest>2</CashInterest>
      <CashSource>Undefined</CashSource>
      <Code>B</Code>
      <SmallAmount>1</SmallAmount>
      <SmallAmountType>1</SmallAmountType>
    </Cookie>
  </Evenement>
</CookedUP> '
)


;WITH XMLNAMESPACES ('http://services.ariel.morneausobeco.com/2007/02' AS ns)
SELECT b.Col1
        --Also include the namespaces here with ns: for each "field" you are after contained within Cookie
       ,XmlCol.value('(./ns:SmallAmount)[1]', 'int') AS SmallAmount
       ,XmlCol.value('(./ns:Cash)[1]', 'int') AS Cash
       ,XmlCol.value('(./ns:Code)[1]', 'nvarchar(10)') AS Code
       ,XmlCol.value('(./ns:CashSource)[1]', 'nvarchar(10)') AS CashSource
FROM @TableXML b
CROSS APPLY b.Col2.nodes('/CookedUP/Evenement/ns:Cookie') AS x(XmlCol) --ns, the namespace, only at Cookie node.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM