Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to display xml attributes in sql syntax with xquery

How to display xml attributes in sql syntax with xquery

How to display xml attributes in sql syntax with xquery

I have the following (snippet) of xml code in a SQL table colum designated as xml datatype.

<RequirementInfo AppliesToPartyID="INS_1_L9019226" FulfillerPartyID="VENDOR_EMSI" id="REQ_L9019226_476043_0000_5">
  <ReqCode tc="5">Collect Urine Specimen (HOS)</ReqCode>
  <RequirementInfoSysKey SystemCode="PLUS" VendorCode="0029">476043</RequirementInfoSysKey>

I can easily pull out the xml element data using the following:

SELECT xml_data.query ('data(//RequirementInfo[1]//ReqCode)') as [XML Sections]
 FROM   dbo.XmlImportTest

My question is, what would the syntax be to read out an ATTRIBUTE in the above xml code snippet, like the tc attribute = "5" in the element 'ReqCode' ??

Thanks for any help you can provide.
Replies continue below

Recommended for you

RE: How to display xml attributes in sql syntax with xquery

I found the answer to my own question:

SELECT xml_id, xml_data.query ('data(//RequirementInfo[2]//ReqCode//@tc)') as [XML_Sections]
FROM   dbo.XmlImportTest
where xml_id = 3



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close