Domanda

I have the following xml. If you run it, you will have 3 grid results. I am struggling with the last grid results as I need that grid results to have the relevant associations. In other words, I would like to have the relevant RowID from Grid results #2 in Grid results #3. How can I do that? Eventually grid results #2 will be inserted into a permanent table which has a primary key called SalesOrderID. Grid results #3 will go into another table with SalesOrderID as foreign key.

Thank you very much!

    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
È stato utile?

Soluzione

Store the XML for Lots in #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) 

Use #tmpDetails in your third query with a CROSS APPLY to shred the XML in 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)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top