Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 972555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:08:36+00:00 2026-05-16T03:08:36+00:00

Not sure if this question makes for some poor performance down the track, but

  • 0

Not sure if this question makes for some poor performance down the track, but seems to at least feel “a better way” right now..

What I am trying to do is this:

I have a table called CONTACTS which amongst other things has a primary key field called memberID

I also have an XML field which contains the ID’s of your friends (for example).. like:

  <root><id>2</id><id>6</id><id>14</id></root>

So what I am trying to do via a stored proc is pass in say your member ID, and return all of your friends info, for example:

  select name, address, age, dob from contacts
  where id... xml join stuff...

The previous way I had it working (well sort of!) selected all the XML nodes (/root/id) into a temp table, and then did a join from that temp table to the contact table to get the contact fields…

Any help much appreciated.. just a bit overloaded from the .query .nodes examples, and of course which is maybe a better way of doing this…

THANKS IN ADVANCE!

<– EDIT –>
I did get something working, but looks like a SQL frankenstein statement!
Basically I needed to get the friends contact ID’s from the XML field, and populate into a temp table like so:

Declare @contactIDtable TABLE (ID int)
INSERT INTO @contactIDtable (ID)
        SELECT CONVERT(INT,CAST(T2.memID.query('.') AS varchar(100))) AS friendsID
        FROM dbo.members
        CROSS APPLY memberContacts.nodes('/root/id/text()') AS T2(memID)

But crikey! the convert/cast thing looks serious.. as I need to get an INT for the next bit which is the actual join to return the contact data as follows:

SELECT memberID, memberName, memberAddress1
    FROM members
    INNER JOIN @contactIDtable cid
    ON members.memberID = cid.ID
    ORDER BY memberName

RESULT…
Well it works.. in my case, my memberContacts XML field had 3 nodes (id’s in this case), and the above query returned 3 rows of data (memberID, memberName, memberAddress1)…

The whole point of this of course was to try to save creating a many join table i.e. list of all my friends ID’s… just not sure if the above actually makes this quicker and easier…

Anymore ideas / more efficient ways of trying to do this???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:08:37+00:00Added an answer on May 16, 2026 at 3:08 am

    SQL Server’s syntax for reading XML is one of the least intuitive around. Ideally, you’d want to:

    select   f.name
    from     friends f
    join     @xml x
    on       x.id = f.id
    

    Instead, SQL Server requires you to spell out everything. To turn an XML variable or column into a “rowset”, you have to spell out the exact path and think up two aliases:

    @xml.nodes('/root/id') as table_alias(column_alias)
    

    Now you have to explain to SQL Server how to turn <id>1</id> into an int:

    table_alias.column_alias.value('.', 'int')
    

    So you can see why most people prefer to decode XML on the client side 🙂

    A full example:

    declare @friends table (id int, name varchar(50))
    insert @friends (id, name)
              select  2, 'Locke Lamorra'
    union all select  6, 'Calo Sanzo'
    union all select 10, 'Galdo Sanzo'
    union all select 14, 'Jean Tannen'
    
    declare @xml xml
    set @xml = ' <root><id>2</id><id>6</id><id>14</id></root>'
    
    select  f.name
    from    @xml.nodes('/root/id') as table_alias(column_alias)
    join    @friends f
    on      table_alias.column_alias.value('.', 'int') = f.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.