1

I am doing some XML querying in SQL and in order to get the attributes of multiple sibling nodes I need to do the following query:

select 
    C.value('@attribute[1]', 'varchar(30)') 
from 
    tblData
cross apply 
    XmlFieldL.nodes('/Data/Children') as T(C)

By default the [1] only gives the first item, so the cross apply gets around that and will give me a list of the @attribute for each child node. Cool, works great.

My question is what is the syntax around the T(C)? It looks like a function of some kind at first glance, or some kind of grouping. I reference the C, but why is the T portion necessary? What does the syntax mean?

For reference, this was the original problem/solution and better describes it. I'm just trying to understand exactly what I'm doing here.

Community
  • 1
  • 1
Andrew Dunaway
  • 1,206
  • 3
  • 16
  • 29

2 Answers2

2

T is your derived table's alias.

C is the column name that is shredded from the /Data/Children nodes.

Code Different
  • 90,614
  • 16
  • 144
  • 163
1

Basically, the .nodes() call returns a "pseudo table" with a single column for each of the matched XML fragments.

That's what the T (for the pseudo table) and C (column in that pseudo table) refer to

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459