我如何在sql server中使用inside apply?我知道另一个没有apply的解决方案,但是我们可以使用outside apply吗?如果“是”,那么如何使用?

7gyucuyw  于 2022-11-21  发布在  SQL Server
关注(0)|答案(1)|浏览(149)

**我们是否可以在SQL服务器中使用inside any apply?**为了遍历XML文件中的所有节点,我尝试使用outer apply,以便可以一次遍历。

DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)

SELECT @XML = XMLData FROM XMLwithOpenXML

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

SELECT CustomerID, CustomerName, Address
FROM OPENXML(@hDoc, 'ROOT/Customers/Customer')
WITH 
(
CustomerID [varchar](50) '@CustomerID',
CustomerName [varchar](100) '@CustomerName',
Address [varchar](100) 'Address'
)
OUTER APPLY
(SELECT OrderDate, OrderID--, Address
FROM OPENXML(@hDoc, 'ROOT/Customers/Customer/Orders/Order')
WITH 
(
OrderDate [varchar](100) '@OrderDate',
OrderID [varchar](100) '@OrderID'
Address [varchar](100) 'Address'
) as Orders
OUTER APPLY
(SELECT Quantity, ProductID--, Address
FROM OPENXML(@hDoc, 'ROOT/Customers/Customer/Orders/Order/OrderDetail')
WITH 
(
Quantity [varchar](100) '@Quantity',
ProductID [varchar](100) '@ProductID'
Address [varchar](100) 'Address'
) as OrderDetail

这是XML文件

<ROOT>
  <Customers>
    --root/customers/cusomer/orders/order/OrderDetail
    <Customer CustomerName="Arshad Ali" CustomerID="C001">
      <Orders>
        <Order OrderDate="2012-07-04T00:00:00" OrderID="10248">
          <OrderDetail Quantity="5" ProductID="10"/>
          <OrderDetail Quantity="12" ProductID="11"/>
          <OrderDetail Quantity="10" ProductID="42"/>
        </Order>
      </Orders>
      <Address> Address line 1, 2, 3</Address>
    </Customer>
  </Customers>
</ROOT>

这就是我想要结果
| 客户ID|客户名称|地址|订单编号|订单日期|产品ID|数量|
| - -|- -|- -|- -|- -|- -|- -|
| 第001号|阿尔沙德·阿里|地址行1、2、3|小行星10248| 2012年07月04日上午10:00:00| 10个|五个|
| 第001号|阿尔沙德·阿里|地址行1、2、3|小行星10248| 2012年07月04日上午10:00:00|十一|十二|
| 第001号|阿尔沙德·阿里|地址行1、2、3|小行星10248| 2012年07月04日上午10:00:00|四十二|10个|
我知道另一个解决方案没有应用。但我们可以做外部应用吗?如果“是”,那么如何?我是新的,所以请帮助我。

waxmsbnn

waxmsbnn1#

要做到这一点,你可以选择去最深的层次,然后从它“爬”上来。

DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)

SELECT @XML = N'<ROOT>
  <Customers>
    --root/customers/cusomer/orders/order/OrderDetail
    <Customer CustomerName="Arshad Ali" CustomerID="C001">
      <Orders>
        <Order OrderDate="2012-07-04T00:00:00" OrderID="10248">
          <OrderDetail Quantity="5" ProductID="10"/>
          <OrderDetail Quantity="12" ProductID="11"/>
          <OrderDetail Quantity="10" ProductID="42"/>
        </Order>
      </Orders>
      <Address> Address line 1, 2, 3</Address>
    </Customer>
  </Customers>
</ROOT>'

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

select *
FROM OPENXML(@hDoc, 'ROOT/Customers/Customer/Orders/Order/OrderDetail')
WITH
    (
        CustomerID      NVARCHAR(30)    '../../../../Customer/@CustomerID'
    ,   CustomerName    NVARCHAR(100)   '../../../../Customer/@CustomerName'
    ,   Address         NVARCHAR(100)   '../../../Address'
    ,   OrderID         NVARCHAR(30)    '../@OrderID'
    ,   OrderDate       DATETIME    '../@OrderDate'
    ,   ProductID       INT '@ProductID'
    ,   Quantity        INT '@Quantity'

    )
    
exec sp_xml_removedocument  @hDoc OUTPUT -- Always clean up your XMLs!

编辑:您的版本与固定的语法也工作:

SELECT CustomerID, CustomerName, Address
FROM OPENXML(@hDoc, 'ROOT/Customers/Customer')
WITH 
(
CustomerID [varchar](50) '@CustomerID',
CustomerName [varchar](100) '@CustomerName',
Address [varchar](100) 'Address'
)
OUTER APPLY
(SELECT OrderDate, OrderID--, Address
FROM OPENXML(@hDoc, 'ROOT/Customers/Customer/Orders/Order')
WITH 
(
OrderDate [varchar](100) '@OrderDate',
OrderID [varchar](100) '@OrderID',
Address [varchar](100) 'Address'
)) as Orders
OUTER APPLY
(SELECT Quantity, ProductID--, Address
FROM OPENXML(@hDoc, 'ROOT/Customers/Customer/Orders/Order/OrderDetail')
WITH 
(
Quantity [varchar](100) '@Quantity',
ProductID [varchar](100) '@ProductID',
Address [varchar](100) 'Address'
)) as OrderDetail

或者你在找什么样的申请?

相关问题