SQL Server Querying An XML Field

Posted by Gavin Draper on Stack Overflow See other posts from Stack Overflow or by Gavin Draper
Published on 2010-06-15T12:38:42Z Indexed on 2010/06/15 12:42 UTC
Read the original article Hit count: 200

Filed under:
|
|

I have a table that contains some meta data in an XML field.

For example

<Meta>
    <From>tst@test.com</From>
    <To>
        <Address>testing@123.com</Address>
        <Address>2@2.com</Address>
    </To>
    <Subject>ESubject Goes Here</Subject>
</Meta>

I want to then be able to query this field to return the following results

From                 To                 Subject
tst@test.com         testing@123.com    Subject Goes Here
tst@test.com         2@2.com            Subject Goes Here

I've written the following query

SELECT
    MetaData.query('data(/Meta/From)') AS [From],
    MetaData.query('data(/Meta/To/Address)') AS [To],
    MetaData.query('data(/Meta/Subject)') AS [Subject]
FROM 
    Documents 

However this only returns one record for that XML field. It combines both the 2 addresses into one result. Is it possible for split these on to separate records?

The result I'm getting is

From                 To                         Subject
tst@test.com         testing@123.com 2@2.com    Subject Goes Here

Thanks

Gav

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about Xml