0

I have XML like below

<Parent>  
    <BOMLine> 
    **<partnumber>abc</partnumber>**
    <SortOrder>0</SortOrder> 
    <Component>
      <PartNo>SE3008</PartNo>
      <Comp>12P6383X012</Comp>
      <CompDesc>SQ Controller</CompDesc>
      <Qty>1</Qty>
    </Component>
    <ChangeOrder>
      <Qty>10</Qty>
      <COId>AS</COId>
      <IsQtyLinked>false</IsQtyLinked>
    </ChangeOrder> 
    <ChangeOrder>
      <Qty>10</Qty>
      <COId>AS1</COId>
      <IsQtyLinked>true</IsQtyLinked>
    </ChangeOrder>
  </BOMLine>
  <BOMLine> ....
</Parent> 

and a temp table like this:

enter image description here

I want to join the xml and table on PartNumber properties and need the following output

enter image description here

And I don't want to use doc handle to handle xml,

Please help me to get the desired output.

I have tried following code, but I am confused how to get nested element from XML while joining.

DECLARE @TempTable TABLE (ID INT,
                          partnumber VARCHAR(200),
                          sortnumber INT)

INSERT INTO @TempTable (ID, partnumber, sortnumber) 
VALUES (123, 'abc', 1)



  DECLARE @xml XML ='<Parent>  
        <BOMLine> 
        <partnumber>abc</partnumber>
        <SortOrder>0</SortOrder> 
        <Component>
          <PartNo>SE3008</PartNo>
          <Comp>12P6383X012</Comp>
          <CompDesc>SQ Controller</CompDesc>
          <Qty>1</Qty>
        </Component>
        <ChangeOrder>
          <Qty>10</Qty>
          <COId>AS</COId>
          <IsQtyLinked>false</IsQtyLinked>
        </ChangeOrder> 
        <ChangeOrder>
          <Qty>10</Qty>
          <COId>AS1</COId>
          <IsQtyLinked>true</IsQtyLinked>
        </ChangeOrder>
      </BOMLine> 

     <BOMLine> 
        <partnumber>abc</partnumber>
        <SortOrder>0</SortOrder> 
        <Component>
          <PartNo>SE3008</PartNo>
          <Comp>12P6383X012</Comp>
          <CompDesc>SQ Controller</CompDesc>
          <Qty>1</Qty>
        </Component>
        <ChangeOrder>
          <Qty>10</Qty>
          <COId>AS</COId>
          <IsQtyLinked>false</IsQtyLinked>
        </ChangeOrder> 
        <ChangeOrder>
          <Qty>10</Qty>
          <COId>AS1</COId>
          <IsQtyLinked>true</IsQtyLinked>
        </ChangeOrder>
      </BOMLine> 
    </Parent> '




SELECT  TT.ID,
       TT.partnumber,
       B.CO.value('(Qty/text())[1]','int') AS Qty,
       B.CO.value('(COId/text())[1]','char(2)') AS COId,
       B.CO.value('(IsQtyLinked/text())[1]','varchar(5)') AS IsLinked
FROM @TempTable TT  
     CROSS APPLY @xml.nodes('Parent/BOMLine') P(BOM)
     CROSS APPLY P.BOM.nodes('./ChangeOrder') B(CO)
WHERE TT.partnumber = P.BOM.value('(partnumber/text())[1]','varchar(3)');
4
  • 1
    What have you tried so far, and why didn't it work? Where is the XML stored, in another table, a variable, other? Commented May 3, 2019 at 11:20
  • i tried but i am confuse how to get nested node properties, due to this i have posted this question Commented May 3, 2019 at 11:22
  • If you post your SQL then we can see where you went wrong. Without it, it's vrry difficult (impssible) for us to debug your code. Please edit your question with your attempt(s) so that we can help you. If you got any errors, post those too. Thanks. Commented May 3, 2019 at 11:26
  • please check code Commented May 3, 2019 at 11:40

1 Answer 1

3

You were close. This is the way I would do it with the sample data you've supplied:

SELECT TT.ID,
       TT.partnumber,
       B.CO.value('(Qty/text())[1]','int') AS Qty,
       B.CO.value('(COId/text())[1]','char(2)') AS COId,
       B.CO.value('(IsQtyLinked/text())[1]','varchar(5)') AS IsLinked
FROM @TempTable TT
     CROSS APPLY @xml.nodes('Parent/BOMLine') P(BOM)
     CROSS APPLY P.BOM.nodes('./ChangeOrder') B(CO)
WHERE TT.partnumber = P.BOM.value('(partnumber/text())[1]','varchar(3)');

Note I use nodes twice, once to get a row for each BOMLine, another for each ChangeOrder. I then use the WHERE to create an implicit JOIN to the table (@TempTable).

As this is sample data, note you may need to change the return data types from the XML to something more appropriate for your real data.

Sign up to request clarification or add additional context in comments.

7 Comments

not working with multiple <BOMLine> tag under parent node
It will, @kushal, it uses CROSS APPLY @xml.nodes('Parent/BOMLine') P(BOM); which generates a new rowset for every BomLine node under Parent. Your sample data only contains one BOMLine node though, so that's all I tested against. if you aren't getting multiple lines, that implies that something else is different; but i can't see your data so I have no way of knowing. you'll need to update your question if so.
I have updated XML please use above xml and execute query,
and insert 1 record into temp table , INSERT INTO @TempTable (ID, partnumber, sortnumber) VALUES (124, 'abc1', 1)
I get 4 rows there @kushal, which is as you expect: db<>fiddle
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.