0

I am trying to query a MSSQL database that has XML and parse it out.

here is the data:

<Root>
<Relatives>
<Relative>
  <Relation>Father</Relation>
  <BcAge>50</BcAge>
  <BilatAge>0</BilatAge>
  <OcAge>0</OcAge>
</Relative>
<Relative>
  <Relation>Mother</Relation>
  <BcAge>58</BcAge>
  <BilatAge>0</BilatAge>
  <OcAge>0</OcAge>
</Relative>

Here is the desired result

Relation BcAge BilatAge OcAge Relation BcAge BilatAge OcAge 
Father   50     0       0      Mother  58    0         0
0

1 Answer 1

1

I doubt, that you really want to get your result side-by-side... This is no valid resultset, your column names are duplicated.

If you really need it this way you might do this:

DECLARE @xml XML=
N'<Root>
<Relatives>
<Relative>
  <Relation>Father</Relation>
  <BcAge>50</BcAge>
  <BilatAge>0</BilatAge>
  <OcAge>0</OcAge>
</Relative>
<Relative>
  <Relation>Mother</Relation>
  <BcAge>58</BcAge>
  <BilatAge>0</BilatAge>
  <OcAge>0</OcAge>
</Relative>
</Relatives>
</Root>';

The query will use a CTE to get the Father and the Mother with an XQuery-predicate

WITH Parents AS
(
    SELECT @xml.query(N'/Root/Relatives/Relative[(Relation/text())[1]="Father"]') AS Father
          ,@xml.query(N'/Root/Relatives/Relative[(Relation/text())[1]="Mother"]') AS Mother
)
SELECT 'Father' AS F_Relation
      ,Father.value(N'(/Relative/BcAge)[1]',N'int') AS F_BcAge
      ,Father.value(N'(/Relative/BilatAge)[1]',N'int') AS F_BilatAge
      ,Father.value(N'(/Relative/OcAge)[1]',N'int') AS F_OcAge
      ,'Mother' AS M_Relation   
      ,Mother.value(N'(/Relative/BcAge)[1]',N'int') AS M_BcAge
      ,Mother.value(N'(/Relative/BilatAge)[1]',N'int') AS M_BilatAge
      ,Mother.value(N'(/Relative/OcAge)[1]',N'int') AS M_OcAge
FROM Parents;

But probably it's this you are looking for (any count of <Relative>):

SELECT rel.value(N'(Relation/text())[1]',N'nvarchar(max)') AS Relation
      ,rel.value(N'(BcAge/text())[1]',N'int') AS BcAge
      ,rel.value(N'(BilatAge/text())[1]',N'int') AS BilatAge
      ,rel.value(N'(OcAge/text())[1]',N'int') AS OcAge
FROM @xml.nodes(N'/Root/Relatives/Relative') AS A(rel)
Sign up to request clarification or add additional context in comments.

Comments

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.