2016-11-11 30 views
0

我有这样的选择在SQL:如何从XMLData字段中的多个节点获取多行?

DECLARE @myOrder xml 
 
    SET @myOrder = 
 
    '<genxml> 
 
    <files /> 
 
    <hidden /> 
 
    <textbox /> 
 
    <checkbox /> 
 
    <dropdownlist> 
 
    <orderstatus>040</orderstatus> 
 
    </dropdownlist> 
 
    <checkboxlist /> 
 
    <radiobuttonlist /> 
 
    <carteditmode /> 
 
    <clientmode>False</clientmode> 
 
    <clientdisplayname /> 
 
    <totalqty datatype="double">3</totalqty> 
 
    <totalweight datatype="double">700</totalweight> 
 
    <totalunitcost datatype="double">17.28</totalunitcost> 
 
    <subtotalcost datatype="double">26.33</subtotalcost> 
 
    <subtotaldealercost datatype="double">21.9</subtotaldealercost> 
 
    <subtotal datatype="double">26.33</subtotal> 
 
    <appliedsubtotal datatype="double">28.23</appliedsubtotal> 
 
    <discountstatus datatype="double" /> 
 
    <voucherdiscount datatype="double">0</voucherdiscount> 
 
    <totaldealerdiscount datatype="double">0</totaldealerdiscount> 
 
    <applieddiscount datatype="double">6.06</applieddiscount> 
 
    <totaldealerbonus datatype="double">4.43</totaldealerbonus> 
 
    <totaldiscount datatype="double">6.06</totaldiscount> 
 
    <totalsalediscount datatype="double">1.9</totalsalediscount> 
 
    <taxcost datatype="double">2.01</taxcost> 
 
    <appliedtax datatype="double">0</appliedtax> 
 
    <dealertotal datatype="double">17.74</dealertotal> 
 
    <total datatype="double">22.17</total> 
 
    <appliedtotal datatype="double">22.17</appliedtotal> 
 
    <currentcartstage>cartsummary</currentcartstage> 
 
    <shippingcost datatype="double">0</shippingcost> 
 
    <shippingdealercost datatype="double">0</shippingdealercost> 
 
    <appliedshipping datatype="double">0</appliedshipping> 
 
    <shippingproductcode /> 
 
    <pickuppointref /> 
 
    <pickuppointaddr /> 
 
    <lang /> 
 
    <discountprocessed>True</discountprocessed> 
 
    <billaddress> 
 
    <genxml> 
 
     <files /> 
 
     <hidden /> 
 
     <textbox> 
 
     <firstname>Hans</firstname> 
 
     <lastname>Lastname</lastname> 
 
     <telephone>0000000</telephone> 
 
     <email datatype="email">[email protected]</email> 
 
     <company /> 
 
     <street>Streetname</street> 
 
     <unit /> 
 
     <city>Cityname</city> 
 
     <postalcode>1234AA</postalcode> 
 
     </textbox> 
 
     <checkbox /> 
 
     <dropdownlist> 
 
     <selectaddress selectedtext="" /> 
 
     <country selectedtext="Netherlands">NL</country> 
 
     <region selectedtext="Overijssel">Country.NL.Region:OV</region> 
 
     </dropdownlist> 
 
     <checkboxlist /> 
 
     <radiobuttonlist /> 
 
    </genxml> 
 
    </billaddress> 
 
    <extrainfo> 
 
    <genxml> 
 
     <files /> 
 
     <hidden> 
 
     <shippingproductcode /> 
 
     <pickuppointref /> 
 
     <pickuppointaddr /> 
 
     </hidden> 
 
     <textbox> 
 
     <extramessage /> 
 
     <promocode>E2COLGQS</promocode> 
 
     </textbox> 
 
     <checkbox> 
 
     <chknews>True</chknews> 
 
     </checkbox> 
 
     <dropdownlist /> 
 
     <checkboxlist /> 
 
     <radiobuttonlist> 
 
     <shippingprovider>afhalen op donderdag</shippingprovider> 
 
     <rblshippingoptions /> 
 
     </radiobuttonlist> 
 
    </genxml> 
 
    </extrainfo> 
 
    <isvalidated>True</isvalidated> 
 
    <createddate datatype="date">2016-11-11T19:34:52</createddate> 
 
    <ordernumber>161111263</ordernumber> 
 
    <audit> 
 
    <genxml> 
 
     <date>2016-11-11T19:34:48</date> 
 
     <status>010</status> 
 
     <username>hans</username> 
 
     <showtouser>True</showtouser> 
 
    </genxml> 
 
    <genxml> 
 
     <date>2016-11-11T19:34:49</date> 
 
     <status>020</status> 
 
     <username>hans</username> 
 
     <showtouser>True</showtouser> 
 
    </genxml> 
 
    <genxml> 
 
     <date>2016-11-11T19:34:49</date> 
 
     <status>010</status> 
 
     <username>hans</username> 
 
     <showtouser>True</showtouser> 
 
    </genxml> 
 
    <genxml> 
 
     <date>2016-11-11T19:34:52</date> 
 
     <status>040</status> 
 
     <username /> 
 
     <showtouser>True</showtouser> 
 
    </genxml> 
 
    </audit> 
 
    <paymentproviderkey>molliepayment</paymentproviderkey> 
 
    <posturl>https://www.mollie.com/paymentscreen/ideal/testmode?transaction_id=e7e2ee381d6105c90c74ff5d203eeb1a&amp;bank_trxid=0000000002656871</posturl> 
 
    <items> 
 
    <genxml> 
 
     <productid>125</productid> 
 
     <modelid>KeYZQZUC</modelid> 
 
     <qty>2</qty> 
 
     <productname>Titel van product</productname> 
 
     <summary>Samenvatting van product</summary> 
 
     <modelref>1KG</modelref> 
 
     <modeldesc>! kg verpakking</modeldesc> 
 
     <modelextra>Extra omschrijving bij model</modelextra> 
 
     <unitcost>10.95</unitcost> 
 
     <dealercost>10.95</dealercost> 
 
     <taxratecode>2</taxratecode> 
 
     <saleprice>0</saleprice> 
 
     <basecost>10.95</basecost> 
 
     <isdealer>False</isdealer> 
 
     <options /> 
 
     <itemcode>125-KeYZQZUC</itemcode> 
 
     <textbox /> 
 
     <dropdownlist /> 
 
     <radiobuttonlist /> 
 
     <checkbox /> 
 
     <totalweight datatype="double">400</totalweight> 
 
     <totalcost datatype="double">21.9</totalcost> 
 
     <totaldealercost datatype="double">21.9</totaldealercost> 
 
     <totaldealerbonus datatype="double">0</totaldealerbonus> 
 
     <discountcodeamt datatype="double">4.16</discountcodeamt> 
 
     <discountstatus datatype="double">valid</discountstatus> 
 
     <totaldiscount datatype="double">4.16</totaldiscount> 
 
     <salediscount datatype="double">0</salediscount> 
 
     <totaldealerdiscount datatype="double">0</totaldealerdiscount> 
 
     <appliedtotalcost datatype="double">21.9</appliedtotalcost> 
 
     <appliedcost datatype="double">10.95</appliedcost> 
 
     <taxcost datatype="double">1.24</taxcost> 
 
     <promodiscount datatype="double">0</promodiscount> 
 
     <productxml> 
 
     <genxml> 
 
      <files /> 
 
      <hidden /> 
 
      <textbox> 
 
      <txtproductref>SKU</txtproductref> 
 
      </textbox> 
 
      <checkbox> 
 
      <chkishidden>False</chkishidden> 
 
      <chkdisable>False</chkdisable> 
 
      </checkbox> 
 
      <dropdownlist /> 
 
      <checkboxlist /> 
 
      <radiobuttonlist /> 
 
      <models> 
 
      <genxml> 
 
       <files /> 
 
       <hidden> 
 
       <modelid>KeYZQZUC</modelid> 
 
       </hidden> 
 
       <textbox> 
 
       <availabledate datatype="date" /> 
 
       <txtqtyminstock datatype="double">1</txtqtyminstock> 
 
       <txtmodelref>1KG</txtmodelref> 
 
       <txtunitcost datatype="double">10.95</txtunitcost> 
 
       <txtsaleprice datatype="double">0</txtsaleprice> 
 
       <txtbarcode>BAR1234</txtbarcode> 
 
       <txtqtyremaining datatype="double">500</txtqtyremaining> 
 
       <txtqtystockset datatype="double">500</txtqtystockset> 
 
       <txtdealercost datatype="double">10.95</txtdealercost> 
 
       <txtpurchasecost datatype="double">0</txtpurchasecost> 
 
       <weight datatype="double">200</weight> 
 
       <depth datatype="double">20</depth> 
 
       <width datatype="double">30</width> 
 
       <height datatype="double">50</height> 
 
       <unit>1</unit> 
 
       <delay /> 
 
       </textbox> 
 
       <checkbox> 
 
       <chkstockon>True</chkstockon> 
 
       <chkishidden>False</chkishidden> 
 
       <chkdeleted>False</chkdeleted> 
 
       <chkdealeronly>False</chkdealeronly> 
 
       </checkbox> 
 
       <dropdownlist> 
 
       <modelstatus>010</modelstatus> 
 
       <taxrate>2</taxrate> 
 
       </dropdownlist> 
 
       <checkboxlist /> 
 
       <radiobuttonlist /> 
 
      </genxml> 
 
      </models> 
 
      <imgs> 
 
      <genxml> 
 
       <files /> 
 
       <hidden> 
 
       <imageurl>/images/gwSKry3s.jpg</imageurl> 
 
       <imagepath>\images\gwSKry3s.jpg</imagepath> 
 
       </hidden> 
 
       <textbox /> 
 
       <checkbox> 
 
       <chkhidden /> 
 
       </checkbox> 
 
       <dropdownlist /> 
 
       <checkboxlist /> 
 
       <radiobuttonlist /> 
 
      </genxml> 
 
      <genxml> 
 
       <files /> 
 
       <hidden> 
 
       <imageurl>/images/8bHyZkZi.png</imageurl> 
 
       <imagepath>\images\8bHyZkZi.png</imagepath> 
 
       </hidden> 
 
       <textbox /> 
 
       <checkbox> 
 
       <chkhidden /> 
 
       </checkbox> 
 
       <dropdownlist /> 
 
       <checkboxlist /> 
 
       <radiobuttonlist /> 
 
      </genxml> 
 
      </imgs> 
 
      <options /> 
 
      <docs> 
 
      <genxml> 
 
       <files /> 
 
       <hidden> 
 
       <fileext>.png</fileext> 
 
       <filepath>d78.png</filepath> 
 
       <filename>d78.png</filename> 
 
       <filerelpath>/d78.png</filerelpath> 
 
       </hidden> 
 
       <textbox> 
 
       <txtfilename>Opzetje-Logo.png</txtfilename> 
 
       </textbox> 
 
       <checkbox> 
 
       <chkishidden>False</chkishidden> 
 
       <chkpurchase>False</chkpurchase> 
 
       </checkbox> 
 
       <dropdownlist /> 
 
       <checkboxlist /> 
 
       <radiobuttonlist /> 
 
      </genxml> 
 
      </docs> 
 
      <importref>nxw73yabsx</importref> 
 
      <lang> 
 
      <genxml> 
 
       <files /> 
 
       <hidden /> 
 
       <textbox> 
 
       <txtproductname>Titel van product</txtproductname> 
 
       <txtsummary>Samenvatting van product</txtsummary> 
 
       <txtseoname>SEO Naam</txtseoname> 
 
       <txtseopagetitle>SEO Titel</txtseopagetitle> 
 
       <txttagwords>E-nummer vrij</txttagwords> 
 
       </textbox> 
 
       <checkbox /> 
 
       <dropdownlist /> 
 
       <checkboxlist /> 
 
       <radiobuttonlist /> 
 
       <edt> 
 
       <description>&lt;p&gt;Dit is een faketekst. Alles wat hier staat is slechts om een indruk te geven van het grafische effect van tekst op deze plek. Wat u hier leest is een voorbeeldtekst. Deze wordt later vervangen door de uiteindelijke tekst, die nu nog niet bekend is. De faketekst is dus een tekst die eigenlijk nergens over gaat. Het grappige is, dat mensen deze toch vaak lezen. Zelfs als men weet dat het om een faketekst gaat, lezen ze toch door.&lt;/p&gt; 
 
</description> 
 
       </edt> 
 
       <models> 
 
       <genxml> 
 
        <files /> 
 
        <hidden /> 
 
        <textbox> 
 
        <txtmodelname>! kg verpakking</txtmodelname> 
 
        <txtextra>Extra omschrijving bij model</txtextra> 
 
        </textbox> 
 
        <checkbox /> 
 
        <dropdownlist /> 
 
        <checkboxlist /> 
 
        <radiobuttonlist /> 
 
       </genxml> 
 
       </models> 
 
       <options /> 
 
       <imgs> 
 
       <genxml> 
 
        <files /> 
 
        <hidden /> 
 
        <textbox> 
 
        <txtimagedesc /> 
 
        </textbox> 
 
        <checkbox /> 
 
        <dropdownlist /> 
 
        <checkboxlist /> 
 
        <radiobuttonlist /> 
 
       </genxml> 
 
       <genxml> 
 
        <files /> 
 
        <hidden /> 
 
        <textbox> 
 
        <txtimagedesc /> 
 
        </textbox> 
 
        <checkbox /> 
 
        <dropdownlist /> 
 
        <checkboxlist /> 
 
        <radiobuttonlist /> 
 
       </genxml> 
 
       </imgs> 
 
       <docs> 
 
       <genxml> 
 
        <files /> 
 
        <hidden /> 
 
        <textbox> 
 
        <txtdocdesc /> 
 
        <txttitle /> 
 
        </textbox> 
 
        <checkbox /> 
 
        <dropdownlist /> 
 
        <checkboxlist /> 
 
        <radiobuttonlist /> 
 
       </genxml> 
 
       </docs> 
 
      </genxml> 
 
      </lang> 
 
     </genxml> 
 
     </productxml> 
 
    </genxml> 
 
    <genxml> 
 
     <productid>206</productid> 
 
     <modelid>7vem1mvV</modelid> 
 
     <qty>1</qty> 
 
     <productname>Productname </productname> 
 
     <summary>Productsummary 
 
</summary> 
 
     <modelref /> 
 
     <modeldesc /> 
 
     <modelextra /> 
 
     <unitcost>6.33</unitcost> 
 
     <dealercost>0</dealercost> 
 
     <taxratecode>1</taxratecode> 
 
     <saleprice>4.43</saleprice> 
 
     <basecost>6.33</basecost> 
 
     <isdealer>False</isdealer> 
 
     <options /> 
 
     <itemcode>206-7vem1mvV</itemcode> 
 
     <textbox /> 
 
     <dropdownlist /> 
 
     <radiobuttonlist /> 
 
     <checkbox /> 
 
     <promodiscount datatype="double">0</promodiscount> 
 
     <totalweight datatype="double">300</totalweight> 
 
     <totalcost datatype="double">4.43</totalcost> 
 
     <totaldealercost datatype="double">0</totaldealercost> 
 
     <totaldealerbonus datatype="double">4.43</totaldealerbonus> 
 
     <totaldiscount datatype="double">1.9</totaldiscount> 
 
     <salediscount datatype="double">1.9</salediscount> 
 
     <totaldealerdiscount datatype="double">0</totaldealerdiscount> 
 
     <appliedtotalcost datatype="double">4.43</appliedtotalcost> 
 
     <appliedcost datatype="double">4.43</appliedcost> 
 
     <taxcost datatype="double">0.77</taxcost> 
 
     <productxml> 
 
     <genxml> 
 
      <files /> 
 
      <hidden /> 
 
      <textbox> 
 
      <txtproductref>749022</txtproductref> 
 
      </textbox> 
 
      <checkbox> 
 
      <chkishidden>False</chkishidden> 
 
      <chkdisable>False</chkdisable> 
 
      </checkbox> 
 
      <dropdownlist /> 
 
      <checkboxlist /> 
 
      <radiobuttonlist /> 
 
      <models> 
 
      <genxml> 
 
       <files /> 
 
       <hidden> 
 
       <modelid>7vem1mvV</modelid> 
 
       </hidden> 
 
       <textbox> 
 
       <availabledate datatype="date" /> 
 
       <txtqtyminstock datatype="double">0</txtqtyminstock> 
 
       <txtmodelref /> 
 
       <txtunitcost datatype="double">6.33</txtunitcost> 
 
       <txtsaleprice datatype="double">4.43</txtsaleprice> 
 
       <txtbarcode /> 
 
       <txtqtyremaining datatype="double">0</txtqtyremaining> 
 
       <txtqtystockset datatype="double">0</txtqtystockset> 
 
       <txtdealercost datatype="double">0</txtdealercost> 
 
       <txtpurchasecost datatype="double">0</txtpurchasecost> 
 
       <weight datatype="double">300</weight> 
 
       <depth datatype="double">0</depth> 
 
       <width datatype="double">0</width> 
 
       <height datatype="double">0</height> 
 
       <unit /> 
 
       <delay /> 
 
       </textbox> 
 
       <checkbox> 
 
       <chkstockon>False</chkstockon> 
 
       <chkishidden>False</chkishidden> 
 
       <chkdeleted>False</chkdeleted> 
 
       <chkdealeronly>False</chkdealeronly> 
 
       </checkbox> 
 
       <dropdownlist> 
 
       <modelstatus>010</modelstatus> 
 
       <taxrate>1</taxrate> 
 
       </dropdownlist> 
 
       <checkboxlist /> 
 
       <radiobuttonlist /> 
 
      </genxml> 
 
      </models> 
 
      <imgs> 
 
      <genxml> 
 
       <files /> 
 
       <hidden> 
 
       <imageurl>/images/32dD5o65.jpg</imageurl> 
 
       <imagepath>\images\32dD5o65.jpg</imagepath> 
 
       </hidden> 
 
       <textbox /> 
 
       <checkbox> 
 
       <chkhidden /> 
 
       </checkbox> 
 
       <dropdownlist /> 
 
       <checkboxlist /> 
 
       <radiobuttonlist /> 
 
      </genxml> 
 
      </imgs> 
 
      <options /> 
 
      <docs /> 
 
      <importref>pys6ox9d8s</importref> 
 
      <lang> 
 
      <genxml> 
 
       <files /> 
 
       <hidden /> 
 
       <textbox> 
 
       <txtproductname>Producttitel</txtproductname> 
 
       <txtsummary>Productsummary 
 
</txtsummary> 
 
       <txtseoname>SEO NAME PRoduct </txtseoname> 
 
       <txtseopagetitle /> 
 
       <txttagwords /> 
 
       </textbox> 
 
       <checkbox /> 
 
       <dropdownlist /> 
 
       <checkboxlist /> 
 
       <radiobuttonlist /> 
 
       <edt> 
 
       <description>&lt;p&gt;Lot of text 
 
</description> 
 
       </edt> 
 
       <models> 
 
       <genxml> 
 
        <files /> 
 
        <hidden /> 
 
        <textbox> 
 
        <txtmodelname /> 
 
        <txtextra /> 
 
        </textbox> 
 
        <checkbox /> 
 
        <dropdownlist /> 
 
        <checkboxlist /> 
 
        <radiobuttonlist /> 
 
       </genxml> 
 
       </models> 
 
       <options /> 
 
       <imgs> 
 
       <genxml> 
 
        <files /> 
 
        <hidden /> 
 
        <textbox> 
 
        <txtimagedesc>img description</txtimagedesc> 
 
        </textbox> 
 
        <checkbox /> 
 
        <dropdownlist /> 
 
        <checkboxlist /> 
 
        <radiobuttonlist /> 
 
       </genxml> 
 
       </imgs> 
 
       <docs /> 
 
      </genxml> 
 
      </lang> 
 
     </genxml> 
 
     </productxml> 
 
    </genxml> 
 
    </items> 
 
</genxml>' 
 

 
SELECT T.C.value('.', 'varchar(100)') as productid 
 
FROM @myOrder.nodes('(/genxml/items/genxml/productid)') as T(C)

其中给出一个漂亮的两排,这两种产品的顺序。 但我怎么能得到一些额外的列,与f.i.节点/ genxml/items/genxml/productname中的产品名称

回答

0

一种选择是在genxml处停止,而不是将更多级别导航到产品,然后选择您的详细信息。

更新:增加了一个更多级别的导航。你可以继续扩展这个每个级别,取决于你开始的地方。由于我们在T(C)中下降到/ genxml/items/genxml /,我们然后使用这个级别并在T1(C2)中再次下降。我去了这个范围(样本),以保持我的select部分小/可读。

SELECT 
    T.C.query('productid').value('.', 'varchar(100)') AS productid 
    ,T.C.query('productname').value('.', 'varchar(100)') productname 
    ,T.C.query('totalweight').value('.', 'float') totalweight 
    ,T1.C2.query('txtbarcode').value('.', 'varchar(100)') txtbarcode 
    FROM @myOrder.nodes('(/genxml/items/genxml/.)') as T(C)  
    CROSS APPLY T.c.nodes('(productxml/genxml/models/genxml/textbox/.)') as T1(C2) 
    --few levels down navigatig all the way to textbox  

该查询仅用于导航产品详细信息与给定的示例。希望能帮助到你。

+0

好的。这工作正常,但这意味着它是不可能返回f.i.条码和产品名称在同一行? – user7043412

+0

对不起,我没有得到“f.i”的一部分。你什么意思?这是XML中的节点吗?条形码部分是否与产品细节有关。 。 – Searching

+0

@ user7043412,好,所以我编辑了它的答案,包括我认为是条形码。 – Searching

相关问题