• <bdo id='6aldt'></bdo><ul id='6aldt'></ul>

    <tfoot id='6aldt'></tfoot>

    <small id='6aldt'></small><noframes id='6aldt'>

    <i id='6aldt'><tr id='6aldt'><dt id='6aldt'><q id='6aldt'><span id='6aldt'><b id='6aldt'><form id='6aldt'><ins id='6aldt'></ins><ul id='6aldt'></ul><sub id='6aldt'></sub></form><legend id='6aldt'></legend><bdo id='6aldt'><pre id='6aldt'><center id='6aldt'></center></pre></bdo></b><th id='6aldt'></th></span></q></dt></tr></i><div id='6aldt'><tfoot id='6aldt'></tfoot><dl id='6aldt'><fieldset id='6aldt'></fieldset></dl></div>

    1. <legend id='6aldt'><style id='6aldt'><dir id='6aldt'><q id='6aldt'></q></dir></style></legend>

        在 SQL Server 中选择 XML 元素

        时间:2023-06-07
          <i id='NRbah'><tr id='NRbah'><dt id='NRbah'><q id='NRbah'><span id='NRbah'><b id='NRbah'><form id='NRbah'><ins id='NRbah'></ins><ul id='NRbah'></ul><sub id='NRbah'></sub></form><legend id='NRbah'></legend><bdo id='NRbah'><pre id='NRbah'><center id='NRbah'></center></pre></bdo></b><th id='NRbah'></th></span></q></dt></tr></i><div id='NRbah'><tfoot id='NRbah'></tfoot><dl id='NRbah'><fieldset id='NRbah'></fieldset></dl></div>

            <tbody id='NRbah'></tbody>
          <tfoot id='NRbah'></tfoot>

          <small id='NRbah'></small><noframes id='NRbah'>

            <bdo id='NRbah'></bdo><ul id='NRbah'></ul>

                1. <legend id='NRbah'><style id='NRbah'><dir id='NRbah'><q id='NRbah'></q></dir></style></legend>
                  本文介绍了在 SQL Server 中选择 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一些 XML 需要使用 SQL Server 2008 进行解析.我想我已经接近得到我想要的东西了,但是我没有正确的语法(我相信).

                  I have some XML that I need to parse using SQL Server 2008. I think I'm close to getting what I want, but I don't have the correct syntax (I believe).

                  我有以下几点:

                  DECLARE @doc XML
                  SET @doc = '<ROOT>          
                      <InvoiceDetail>
                          <OrderId>1000000</OrderId>
                          <OrderTypeId>2</OrderTypeId>
                          <Id>2000</Id>
                          <InvoiceItems>
                              <InvoiceItem>
                                  <LineId>1</LineId>
                                  <Cd>123456</Cd>
                                  <Description>Item 1</Description>
                                  <Quantity>1</Quantity>
                                  <UnitPrice>99.990000</UnitPrice>
                              </InvoiceItem>
                              <InvoiceItem>
                                  <LineId>2</LineId>
                                  <Cd>234567</Cd>
                                  <Description>Item 2</Description>
                                  <Quantity>1</Quantity>
                                  <UnitPrice>89.990000</UnitPrice>
                              </InvoiceItem>
                          </InvoiceItems>
                      </InvoiceDetail>
                      <InvoiceDetail>
                          <OrderId>1200000</OrderId>
                          <OrderTypeId>1</OrderTypeId>
                          <Id>3000</Id>
                          <InvoiceItems>
                              <InvoiceItem>
                                  <LineId>1</LineId>
                                  <Cd>234567</Cd>
                                  <Description>Item 2</Description>
                                  <Quantity>1</Quantity>
                                  <UnitPrice>89.990000</UnitPrice>
                              </InvoiceItem>
                              <InvoiceItem>
                                  <LineId>2</LineId>
                                  <Cd>345678</Cd>
                                  <Description>Item 3</Description>
                                  <Quantity>1</Quantity>
                                  <UnitPrice>79.990000</UnitPrice>
                              </InvoiceItem>
                          </InvoiceItems>
                      </InvoiceDetail>
                  </ROOT>'
                  
                  SELECT 
                        Invoices.Node.value('@OrderId', 'VARCHAR(10)') 'OrderID'
                      , Invoices.Node.value('@Id', 'INT') 'InvoiceId'
                      , Items.Cd.value('.', 'VARCHAR(14)') 'ItemId'
                  FROM 
                      @doc.nodes('//InvoiceDetail') Invoices(Node)
                      CROSS APPLY Invoices.Node.nodes('./InvoiceItems/InvoiceItem/Cd') Items(Cd)
                  

                  我得到以下结果:

                  NULL    NULL    123456
                  NULL    NULL    234567
                  NULL    NULL    234567
                  NULL    NULL    345678
                  

                  我正在尝试获得以下信息:

                  I'm trying to get the following:

                  1000000 2000    123456
                  1000000 2000    234567
                  1200000 3000    234567
                  1200000 3000    345678
                  

                  我做错了什么?

                  推荐答案

                  抓取元素的语法是:

                  SELECT Invoices.Node.value('(OrderId)[1]', 'VARCHAR(10)') 'OrderID'
                      , Invoices.Node.value('(Id)[1]', 'INT') 'InvoiceId'
                      , Items.Cd.value('.', 'VARCHAR(14)') 'ItemId'
                  FROM 
                      @doc.nodes('//InvoiceDetail') Invoices(Node)
                      CROSS APPLY Invoices.Node.nodes('./InvoiceItems/InvoiceItem/Cd') Items(Cd)
                  

                  这似乎也可以在没有明确括号的情况下工作:

                  This also appears to work without the explicit parenthesis:

                  Invoices.Node.value('OrderId[1]', 'VARCHAR(10)') 
                  

                  <小时>

                  @ 语法用于属性,而不是 XQuery 中的元素.如果你有


                  The @ syntax is for attributes, not elements in XQuery. If you had

                  <InvoiceDetail title="something">
                  

                  然后你就可以使用:

                  SELECT Invoices.Node.value('@title', 'VARCHAR(MAX)') AS Title
                  FROM @doc.nodes('//InvoiceDetail') Invoices(Node)
                  

                  这是一篇关于使用 XQuery 值的好文章

                  这篇关于在 SQL Server 中选择 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 SQL Server XPath 中获取跟随兄弟 下一篇:如何在 Oracle 中通过 XPath 获取第一个元素

                  相关文章

                  最新文章

                  1. <legend id='ppGfk'><style id='ppGfk'><dir id='ppGfk'><q id='ppGfk'></q></dir></style></legend>
                  2. <i id='ppGfk'><tr id='ppGfk'><dt id='ppGfk'><q id='ppGfk'><span id='ppGfk'><b id='ppGfk'><form id='ppGfk'><ins id='ppGfk'></ins><ul id='ppGfk'></ul><sub id='ppGfk'></sub></form><legend id='ppGfk'></legend><bdo id='ppGfk'><pre id='ppGfk'><center id='ppGfk'></center></pre></bdo></b><th id='ppGfk'></th></span></q></dt></tr></i><div id='ppGfk'><tfoot id='ppGfk'></tfoot><dl id='ppGfk'><fieldset id='ppGfk'></fieldset></dl></div>

                      <bdo id='ppGfk'></bdo><ul id='ppGfk'></ul>

                      <small id='ppGfk'></small><noframes id='ppGfk'>

                      <tfoot id='ppGfk'></tfoot>