2

我有以下xml。如果你运行它,你会得到 3 个网格结果。我正在为最后一个网格结果苦苦挣扎,因为我需要该网格结果具有相关关联。换句话说,我想在 Grid results #3 中拥有来自 Grid results #2 的相关 RowID。我怎样才能做到这一点?最终,网格结果#2 将被插入到具有名为 SalesOrderID 的主键的永久表中。网格结果 #3 将进入另一个以 SalesOrderID 作为外键的表。

非常感谢!

    DECLARE @string VARCHAR(max);
DECLARE @xml XML;
SET @string ='<ShipmentConfirmationMessage>
  <ExternalCorrelationId>249801</ExternalCorrelationId>
  <ShippedItems>
    <ShipmentConfirmationLine>
      <SalesOrderNumber>SalesOrder1</SalesOrderNumber>
      <ItemId>10982</ItemId>
      <IsBackOrdered>false</IsBackOrdered>
      <TrackingNumber>Track1234</TrackingNumber>
      <Lots>
        <Lot>
          <LotNumber>789</LotNumber>
          <ExpiryDate>2013-12-20T00:00:00</ExpiryDate>
          <Quantity>1.55</Quantity>
        </Lot>

      </Lots>
    </ShipmentConfirmationLine>
    <ShipmentConfirmationLine>
      <SalesOrderNumber>SalesOrder2</SalesOrderNumber>
      <ItemId>10983</ItemId>
      <IsBackOrdered>true</IsBackOrdered>
      <TrackingNumber>Track123456789</TrackingNumber>
      <Lots>
        <Lot>
          <LotNumber>1</LotNumber>
          <ExpiryDate>2013-12-20T00:00:00</ExpiryDate>
          <Quantity>3.30</Quantity>
        </Lot>
        <Lot>
          <LotNumber>21</LotNumber>
          <ExpiryDate>2016-12-20T00:00:00</ExpiryDate>
          <Quantity>34.30</Quantity>
        </Lot>
      </Lots>
    </ShipmentConfirmationLine>
      </ShippedItems>
</ShipmentConfirmationMessage>
';
SET @xml = CONVERT(XML, @string, 1);
-- header
SELECT
@xml.value('/ShipmentConfirmationMessage[1]/ExternalCorrelationId[1]', 'varchar(50)') PurchaseOrderID
-- detail lines
SELECT IDENTITY(int, 1,1) AS RowID
 ,doc.col.value('SalesOrderNumber[1]', 'varchar(50)') SalesOrderNumber
 ,doc.col.value('ItemId[1]', 'varchar(50)') ItemId
 ,doc.col.value('IsBackOrdered[1]', 'varchar(50)') IsBackOrdered
 ,doc.col.value('TrackingNumber[1]', 'varchar(50)') TrackingNumber
 INTO #tmpDetails
FROM @xml.nodes('/ShipmentConfirmationMessage/ShippedItems/ShipmentConfirmationLine') doc(col) 
select * from #tmpDetails

-- lots associated with detail lines
SELECT
'Relevant RowID goes here',
doc.col.value('LotNumber[1]', 'varchar(50)') LotNumber
,doc.col.value('ExpiryDate[1]', 'datetime') ExpiryDate
,doc.col.value('Quantity[1]', 'decimal(13,5)') Quantity
 FROM @xml.nodes('/ShipmentConfirmationMessage/ShippedItems/ShipmentConfirmationLine/Lots/Lot') doc(col) 

 drop table #tmpDetails
4

1 回答 1

0

将 XML 存储Lots#tmpDetails.Lots

SELECT IDENTITY(int, 1,1) AS RowID
 ,doc.col.value('SalesOrderNumber[1]', 'varchar(50)') SalesOrderNumber
 ,doc.col.value('ItemId[1]', 'varchar(50)') ItemId
 ,doc.col.value('IsBackOrdered[1]', 'varchar(50)') IsBackOrdered
 ,doc.col.value('TrackingNumber[1]', 'varchar(50)') TrackingNumber
 ,doc.col.query('Lots') Lots
 INTO #tmpDetails
FROM @xml.nodes('/ShipmentConfirmationMessage/ShippedItems/ShipmentConfirmationLine') doc(col) 

#tmpDetails在您的第三个查询中使用 a将CROSS APPLYXML 分解为Lots.

SELECT
  D.RowID
 ,doc.col.value('LotNumber[1]', 'varchar(50)') LotNumber
 ,doc.col.value('ExpiryDate[1]', 'datetime') ExpiryDate
 ,doc.col.value('Quantity[1]', 'decimal(13,5)') Quantity
FROM #tmpDetails D
  CROSS APPLY D.Lots.nodes('Lots/Lot') doc(col)
于 2013-06-29T08:31:58.963 回答