views:

20

answers:

1

The database have Table EMP with 3 columns empID , badgID , XMLDATA.

The XMLDATA have datatype "clob" and data is in the form -

<Record id="11">
    <Demo rID="5"/>
</Record>

How can I read the attribute "rID" in node "Demo"value in the above XMLDATA in single query?

Server - SQL Server 2005

+2  A: 

If you have your data stored with datatype XML in SQL Server, then you can do this:

SELECT 
    empID, badgID,
    XmlData.value('(/Record/Demo/@rID)[1]', 'int') as 'rID'
FROM 
    dbo.YourTable

If your datatype is not XML - it really should be! You might need to use something like this (might not work in all cases):

SELECT 
    empID, badgID,
    (CAST XmlData AS XML).value('(/Record/Demo/@rID)[1]', 'int') as 'rID'
FROM 
    dbo.YourTable
marc_s
Thanks.I tried both queries ,it fails and says "schema XMLDATA does not exist."But XMLDATA column is present in the Table.
indigenious
@indigenious: can you show us the actual table structure, e.g. a database diagram or something??
marc_s
My apologies , i only have read access to the database and do not aware about the architecture.
indigenious