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

  • Home
  • SEARCH
  • 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 1092421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:40:47+00:00 2026-05-16T23:40:47+00:00

I have a column in an SQL Server 2005 DB which contains an XML

  • 0

I have a column in an SQL Server 2005 DB which contains an XML stored as a string. Within that XML is the following element tree

<DriverDetails>
  <DriverDetail>
    <ID>2334</ID>
    <PRN>1</PRN>
  </DriverDetail>
  <DriverDetail>
    <ID>2335</ID>
    <PRN>2</PRN>
  </DriverDetail>
  <DriverDetail>
    <ID>2336</ID>
    <PRN>3</PRN>
  </DriverDetail>
  <DriverDetail>
    <ID>2337</ID>
    <PRN>4</PRN>
  </DriverDetail>
</DriverDetails>

I’ve retrieved this using:

CONVERT(xml, detailRiskInformation).query('
            //DriverDetails
        ')

I need to query each ID within each DriverDetail node to see it if exists in another table, called DriverDetails. The relevant column is [DriverDetail].[Id].

Now, I can run XQuery like this:

CONVERT(xml, detailRiskInformation).query('
            for $i in //DriverDetail
            return data( $i )
        ')

However it just returns a single result with the 4 IDs separated by spaces.

How can I perform an iterative query on each of these IDs in one query? Or, if not, how can I get these out using a cursor or something much cleverer?

Thanks in advance
Ant

  • 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-16T23:40:48+00:00Added an answer on May 16, 2026 at 11:40 pm

    If you wish to retrieve the ID values as a ‘TABLE’ to use in a select, try something like this

    DECLARE @xml XML
    
    SET @xml = '
    <DriverDetails> 
      <DriverDetail> 
        <ID>2334</ID> 
        <PRN>1</PRN> 
      </DriverDetail> 
      <DriverDetail> 
        <ID>2335</ID> 
        <PRN>2</PRN> 
      </DriverDetail> 
      <DriverDetail> 
        <ID>2336</ID> 
        <PRN>3</PRN> 
      </DriverDetail> 
      <DriverDetail> 
        <ID>2337</ID> 
        <PRN>4</PRN> 
      </DriverDetail> 
    </DriverDetails>
    '
    
    SELECT  T.c.value('.', 'int') ID
    FROM    @xml.nodes('/DriverDetails/DriverDetail/ID') T(c)
    

    Or from a table column it would be something like

    DECLARE @Table TABLE(
            XmlVal XML
    )
    
    INSERT INTO @Table
    SELECT '<DriverDetails> 
      <DriverDetail> 
        <ID>2334</ID> 
        <PRN>1</PRN> 
      </DriverDetail> 
      <DriverDetail> 
        <ID>2335</ID> 
        <PRN>2</PRN> 
      </DriverDetail> 
      <DriverDetail> 
        <ID>2336</ID> 
        <PRN>3</PRN> 
      </DriverDetail> 
      <DriverDetail> 
        <ID>2337</ID> 
        <PRN>4</PRN> 
      </DriverDetail> 
    </DriverDetails>
    '
    
    INSERT INTO @Table
    SELECT '<DriverDetails> 
      <DriverDetail> 
        <ID>1</ID> 
        <PRN>1</PRN> 
      </DriverDetail> 
      <DriverDetail> 
        <ID>2</ID> 
        <PRN>2</PRN> 
      </DriverDetail> 
      <DriverDetail> 
        <ID>3</ID> 
        <PRN>3</PRN> 
      </DriverDetail> 
      <DriverDetail> 
        <ID>4</ID> 
        <PRN>4</PRN> 
      </DriverDetail> 
    </DriverDetails>
    '
    
    
    SELECT  T2.Loc.value('.', 'int') ID
    FROM    @Table T
    CROSS APPLY XmlVal.nodes('/DriverDetails/DriverDetail/ID') as T2(Loc) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sql server 2005 database which contains a user table.. Is it
I have a column in my sql server 2005 table that should hold the
I have a table in my sql server 2005 database which contains about 50
I have a stored procedure in SQL Server 2005 which accepts few parameters of
i have table with filmname and actors column in sql server 2005 i want
I have a column type money in my sql server database, which is moneyAcumulated
I have a few windwos services. They get xml column from Sql server manipulate
I have a SQL Table with a column called FullName which contains, for example,
I have 2 records in a table in SQL Server 2005 db which has
I work with SQL Server 2005. I have a table in which FK: two

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.