I'm writing some reporting stored procedures, and need to parse some XML in SQL 2008. It's for the Umbraco CMS system, which stores its versioned data in a peculiar way that's not suited to querying many columns from. It does also keep an XML cache of all published items, which seems more suited for querying.
The XML cache is stored as an NText data type, and I've been able to parse it with SQL's XML library well enough. My question is - Is it possible to cast several XML rows to the XML data type, without using the temporary table?
The example SP is below - which utilizes a temporary table. Extra points for any pointers on being more efficient with retrieving the values.
I query the node several times, so assume that casting it each time would be inefficient.
-- Create temporary table that uses the XML data type
create table #XmlTable (
NodeId int,
NodeXml xml
)
-- Move all XML for documents of type '1121' into there
insert into #XmlTable(NodeId, NodeXml)
select cx.* from cmsContentXml cx
join cmsContent cc on cc.nodeId = cx.nodeId
where cc.contentType = 1121
select un.text [Document Name],
xt.NodeXml.query('//node/data [@alias=''fieldOne'']').value('.', 'nvarchar(max)') [Field One],
xt.NodeXml.query('//node/data [@alias=''fieldTwo'']').value('.', 'nvarchar(max)') [Field Two],
xt.NodeXml.query('//node/data [@alias=''fieldThree'']').value('.', 'nvarchar(max)') [Field Three]
from #XmlTable xt
join umbracoNode un on un.id = xt.NodeId
-- Drop temporary table when finished
drop table #XmlTable
Each XML node looks approximately like:
<node id="111">
<data alias='fieldOne'>Value 1</data>
<data alias='fieldTwo'>Value 2</data>
<data alias='fieldThree'>Value 3</data>
<data alias='fieldFour'>Value 4</data>
<data alias='fieldFive'>Value 5</data>
</node>
Thanks in advance.