Using SQL Server CROSS APPLY to read XML segment

Here is an interesting code snippet:

declare @XML xml
set @XML =’


select ExpressionAlias.student_name,
ExpressionAlias.subject_name,
ExpressionAlias.score
From @XML.nodes(‘./students/student’) Student (rowset)
Cross Apply
Student.rowset.nodes(‘./subjects/subject’) Subject (rowset)
Cross Apply (
Select Student.rowset.value(‘@name’,’NVARCHAR(20)’),
Subject.rowset.value(‘@name’,’NVARCHAR(20)’),
Subject.rowset.value(‘@score’,’INTEGER’)
) ExpressionAlias (student_name, subject_name,score)
Order By
ExpressionAlias.student_name ASC,
ExpressionAlias.score DESC;

3 thoughts on “Using SQL Server CROSS APPLY to read XML segment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.