3

Given a XML data typed column x in table y with values such as:

<a>
<b>
<c>1</c>
<c>2</c>
<c>3</c>
</b>
</a>

How can I obtain a result set with T-SQL XQuery such that one row is returned per <c> element value? Let's say table y also has column z that contains the number of <c> elements in column x - just so we have an additional column to return.

The best we could come up with thus far was:

select 
z, 
cvalues = x.query('data(a/b/c)')
from y

The above however will return one row per row in y and have a white space separated list of the values for the <c> elements in column cvalues.

How can we get a result set such as:

z cvalues
3 1
3 2
3 3

instead of what we get now:

z cvalues
3 1 2 3 

Thanks!

1 Answer 1

3

You can use CROSS APPLY combined with nodes:

SELECT 
  z, 
  cvalues = s.c.value('.', 'int')
FROM y
CROSS APPLY x.nodes('//c') AS s(c);

LiveDemo

Output:

╔═══╦═════════╗
║ z ║ cvalues ║
╠═══╬═════════╣
║ 3 ║       1 ║
║ 3 ║       2 ║
║ 3 ║       3 ║
╚═══╩═════════╝

Keep in mind that:

//c match all c xml element no matter where it is in hierarchy

/a/b/c will match c xml element that is nested inside a and b

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

1 Comment

Very good! Added bonus for the part on matching. Excited to learn more XQuery: seems it can be very useful indeed.

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.