Member Login

Remember Me
Forgot Password?
Join Us!

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!

Join Tek-Tips
*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.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

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.

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 the post below 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!


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