I have a column in an SQL Server 2005 DB which contains an XML stored as a string. Within that XML is the following element tree
<DriverDetails>
<DriverDetail>
<ID>2334</ID>
<PRN>1</PRN>
</DriverDetail>
<DriverDetail>
<ID>2335</ID>
<PRN>2</PRN>
</DriverDetail>
<DriverDetail>
<ID>2336</ID>
<PRN>3</PRN>
</DriverDetail>
<DriverDetail>
<ID>2337</ID>
<PRN>4</PRN>
</DriverDetail>
</DriverDetails>
I've retrieved this using:
CONVERT(xml, detailRiskInformation).query('
//DriverDetails
')
I need to query each ID within each DriverDetail node to see it if exists in another table, called DriverDetails. The relevant column is [DriverDetail].[Id].
Now, I can run XQuery like this:
CONVERT(xml, detailRiskInformation).query('
for $i in //DriverDetail
return data( $i )
')
However it just returns a single result with the 4 IDs separated by spaces.
How can I perform an iterative query on each of these IDs in one query? Or, if not, how can I get these out using a cursor or something much cleverer?
Thanks in advance Ant