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 6908723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:35:57+00:00 2026-05-27T08:35:57+00:00

Consider the XML and SQL: declare @xml xml = ‘ <root> <person id=11272> <notes

  • 0

Consider the XML and SQL:

declare @xml xml = '
<root>
    <person id="11272">
        <notes for="107">Some notes!</notes>
        <item id="107" selected="1" />
    </person>
    <person id="77812">
        <notes for="107"></notes>
        <notes for="119">Hello</notes>
        <item id="107" selected="0" />
        <item id="119" selected="1" />
    </person>
</root>'

select  Row.Person.value('data(../@id)', 'int') as person_id,
        Row.Person.value('data(@id)', 'int') as item_id,
        Row.Person.value('data(../notes[@for=data(@id)][1])', 'varchar(max)') as notes
from    @xml.nodes('/root/person/item') as Row(Person)

I end up with:

person_id   item_id     notes
----------- ----------- -------
77812       107         NULL
77812       119         NULL
11272       107         NULL

What I want is the ‘notes’ column to be pulled based on the @id attribute of the current item. If I replace [@for=data(@id)] in the selector with [@for=107] of course I get the value Some notes! in the last record. Is it possible to do this with XPath/XQuery, or am I barking up the wrong tree here? I think the problem is that

The XML is a bit awkward, yes, but I can’t really change it I’m afraid.

I found one solution that works, but it feels awfully heavy for something like this.

select  Item.Person.value('data(../@id)', 'int') as person_id,
        Item.Person.value('data(@id)', 'int') as item_id,
        Notes.Person.value('text()[1]', 'varchar(max)') as notes
from    @xml.nodes('/root/person/item') as Item(Person)
        inner join @xml.nodes('/root/person/notes') as Notes(Person) on
            Notes.Person.value('data(@for)', 'int') = Item.Person.value('data(@id)', 'int')
            and
            Notes.Person.value('data(../@id)', 'int') = Item.Person.value('data(../@id)', 'int')

Update!

I figured it out! I’m new at XQuery but this works, so I’m calling it job done 🙂 I changed the query for the notes to:

Item.Person.value('
    let $id := data(@id)
    return data(../notes[@for=$id])[1]
', 'varchar(max)') as notes
  • 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-27T08:35:58+00:00Added an answer on May 27, 2026 at 8:35 am

    I would suggest that you do a cross apply instead of doing ../ to find a parent node. According to query plan it is a lot faster.

    select  P.X.value('data(@id)', 'int') as person_id,
            I.X.value('data(@id)', 'int') as item_id,
            I.X.value('let $id := data(@id)
                       return data(../notes[@for=$id])[1]', 'varchar(max)') as notes
    from @xml.nodes('/root/person') as P(X)
      cross apply P.X.nodes('item') as I(X)
    

    You can even remove the ../ in the flwor with one extra cross apply gaining a bit more.

    select P.X.value('@id', 'int') as person_id,
           TI.id as item_id,
           P.X.value('(notes[@for = sql:column("TI.id")])[1]', 'varchar(max)') as notes
    from @xml.nodes('/root/person') as P(X)
      cross apply P.X.nodes('item') as I(X)
      cross apply (select I.X.value('@id', 'int')) as TI(id) 
    

    Comparing the queries against each other I got 67% on your query 17% on my first and 16% on the second. Note: these figures only give you a hint on what query will actually be faster in reality. Test the against your data to know for sure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following script for outputting some XML code: var xmlAsString = '<?xml version=1.0?><person><name
Using MSSQL 2008 and XQUERY Consider the following XML stored in a table: <ROOT>
Consider this scenario: I've an XML file called person.xml with the following data in
Consider following XML document fragment: <Book> <Title>Example</Title> <Content> Some line </Content> <TOC> Again some
Consider simple XML document: <html><body> <table> <tr><td> Item 1</td></tr> <tr><td> Item 2</td></tr> </table> </body></html>
Consider this XML: <Employees> <Person> <ID>1000</ID> <Name>Nima</Name> <LName>Agha</LName> </Person> <Person> <ID>1001</ID> <Name>Ligha</Name> <LName>Ligha</LName> </Person>
Consider this XML: <Employees> <Person> <ID>1000</ID> <Name>Nima</Name> <LName>Agha</LName> </Person> <Person> <ID>1001</ID> <Name>Ligha</Name> <LName>Ligha</LName> </Person>
Lets consider this xml file : <?xml version=1.0 encoding=UTF-8?> <root attribute=value> <element>myElement</element> </root> I'm
Consider this XML: <people> <person> <firstName>Deane</firstName> <lastName>Barker</lastName> </person> </people> What if two XSLT templates
Consider the following XML: <root> <steps> <step>1</step> <step>2</step> <step>3</step> <step>4</step> </steps> <stepDetails step=1>Details</stepDetails> <stepDetails

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.