が列

2017-05-06 3 views
0
DECLARE @hDoc XML = 
'<orders> 
    <mail> 
     <set id="s" sequence="1" version="1.1" > 
      <set id="order" sequence="4" version=""> 
       <property id="id" label="" sequence="0" resource_label="" show_type="" name="" ><![CDATA[&#8364; 10000]]></property> 
       <set id="article" sequence="1" version=""> 
        <property id="basepricevalue" label="" sequence="0" resource_label="" show_type="" name="" ><![CDATA[155.66]]></property> 
        <property id="quantity" label="" sequence="0" resource_label="" show_type="" name="" ><![CDATA[3]]></property> 
        <property id="senior" label="" sequence="0" resource_label="" show_type="" name="" ><![CDATA[16]]></property>      
        <property id="vat" label="" sequence="0" resource_label="" show_type="" name="" ><![CDATA[21]]></property>      
       </set> 
       <set id="article" sequence="2" version=""> 
        <property id="basepricevalue" label="" sequence="0" resource_label="" show_type="" name="" ><![CDATA[465.99]]></property> 
        <property id="quantity" label="" sequence="0" resource_label="" show_type="" name="" ><![CDATA[12]]></property> 
        <property id="senior" label="" sequence="0" resource_label="" show_type="" name="" ><![CDATA[17]]></property> 
        <property id="vat" label="" sequence="0" resource_label="" show_type="" name="" ><![CDATA[18]]></property>          
       </set> 
      </set> 
     </set> 
    </mail> 
</orders>'; 

所望の出力のプロパティを使用してXMLを変換:が列

OrderID basepricevalue  quantity  senior  vat 
10000  155.66    3   16   21 
10000  465.99    12   17   18 

これは私が現時点で持っているものであるが、すべてのクロスのを適用するため、パフォーマンスは本当に悪いです。私は確かにこれを行うためのより良い方法が必要です。あなたが特定の要素

SELECT 
    orderid = O.orderset.query('property[@id="id"]/text()').value('.','varchar(max)'), 
    price = A.article.query('property[@id="basepricevalue"]/text()').value('.','float'), 
    quantity = A.article.query('property[@id="quantity"]/text()').value('.','int'), 
    senior = A.article.query('property[@id="senior"]/text()').value('.','int'), 
    vat = A.article.query('property[@id="vat"]/text()').value('.','int') 
FROM (VALUES (@hDoc)) hdoc(orders) 
CROSS APPLY orders.nodes('orders/mail//set[@id="order"]') O(orderset) 
CROSS APPLY O.orderset.nodes('set[@id="article"]') A(article) 

を取得するためにクエリを()を使用することができますまたはあなたが実際に直接値を()を使用する可能性があるので

SELECT 
    orderid = nd.x.value('(property/text())[1]', 'varchar(8000)'), 
    price = ndprice.x.value('(text())[1]', 'varchar(100)'), 
    quantity = ndquantity.x.value('(text())[1]', 'varchar(100)'), 
    senior = ndsenior.x.value('(text())[1]', 'varchar(100)'), 
    vat = ndvat.x.value('(text())[1]', 'varchar(100)') 

FROM (VALUES (@hDoc)) hdoc(orders) 
CROSS APPLY orders.nodes('orders/mail//set[@id="order"]') nd(x) 
CROSS APPLY x.nodes('set[@id="article"]') nd2(x) 
CROSS APPLY nd2.x.nodes('property[@id="basepricevalue"]') ndprice(x) 
CROSS APPLY nd2.x.nodes('property[@id="quantity"]') ndquantity(x) 
CROSS APPLY nd2.x.nodes('property[@id="senior"]') ndsenior(x) 
CROSS APPLY nd2.x.nodes('property[@id="vat"]') ndvat(x) 

答えて

0

あなたはより多くを使用して、のを適用CROSSの一部を取り除くことができます特定のxpath。
それは最初の出来事です[1]。

また、XMLタイプからノードを直接取得することで、追加のCROSS APPLYを取り除くことができます。

SELECT 
    orderid = O.orderset.value('(property[@id="id"]/text())[1]','varchar(max)'), 
    price = A.article.value('(property[@id="basepricevalue"]/text())[1]','float'), 
    quantity = A.article.value('(property[@id="quantity"]/text())[1]','int'), 
    senior = A.article.value('(property[@id="senior"]/text())[1]','int'), 
    vat = A.article.value('(property[@id="vat"]/text())[1]','int') 
FROM @hDoc.nodes('orders/mail//set[@id="order"]') O(orderset) 
CROSS APPLY O.orderset.nodes('set[@id="article"]') A(article)