Andrew Dunaway
Andrew Dunaway

Reputation: 1226

In SQL, what do the parentheses in the 'as' clause mean?

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.

Upvotes: 1

Views: 1144

Answers (2)

marc_s
marc_s

Reputation: 754538

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

Upvotes: 1

Code Different
Code Different

Reputation: 93161

T is your derived table's alias.

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

Upvotes: 2

Related Questions