first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

SQL Server 2005 t-SQL: The Use of the nodes() Method for XML Types

DECLARE @xml xml

SET @xml = ' <Root> <Item1 /> <Item2 /> <Item3 /> <Item4 /> <Item5 /> </Root> '

SELECT Temp.Items.query('.') AS [Items] FROM @xml.nodes('//Root/*') AS Temp(Items)

/* The SELECT form avoids an error like:

“The column 'Items' that was returned
from the nodes() method cannot be used directly.
It can only be used with one
of the four xml data type methods,
exist(), nodes(), query(), and value(),
or in IS NULL and IS NOT NULL checks.”

The table alias (with columns) avoids this error:

“The table (and its columns) returned
by a table-valued method need to be aliased.”

These two errors imply that:

     * The nodes() method returns a table.
     * The table returned from the nodes() has XML columns.

It follows that one may never persist a table with XML columns
but temporary XML columns are very likely to be used.

For general information, see
“Introduction to XQuery in SQL Server 2005”
http://technet.microsoft.com/en-us/library/ms345122.aspx

*/

mod date: 2007-12-23T06:06:50.000Z