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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:03:15+00:00 2026-05-26T12:03:15+00:00

On SQL Server 2008 R2, I am trying to read XML value as table.

  • 0

On SQL Server 2008 R2, I am trying to read XML value as table.

So far, I am here :

DECLARE @XMLValue AS XML;
SET @XMLValue = '<SearchQuery>
    <ResortID>1453</ResortID>
    <CheckInDate>2011-10-27</CheckInDate>
    <CheckOutDate>2011-11-04</CheckOutDate>
    <Room>
        <NumberOfADT>2</NumberOfADT>
        <CHD>
            <Age>10</Age>
        </CHD>
        <CHD>
            <Age>12</Age>
        </CHD>
    </Room>
    <Room>
        <NumberOfADT>1</NumberOfADT>
    </Room>
    <Room>
        <NumberOfADT>1</NumberOfADT>
        <CHD>
            <Age>7</Age>
        </CHD>
    </Room>
</SearchQuery>';

SELECT 
    Room.value('(NumberOfADT)[1]', 'INT') AS NumberOfADT
FROM @XMLValue.nodes('/SearchQuery/Room') AS SearchQuery(Room);

As you can see, Room node sometimes get CHD child nodes but sometimes don’t.

Assume that I am getting this XML value as a Stored Procedure parameter. So, I need to work with the values in order to query my database tables. What would be the best way to read this XML parameter entirely?

EDIT

I think I need to express what I am expecting in return here. The below script code is for the table what I need here :

DECLARE @table AS TABLE(
    ResorrtID INT,
    CheckInDate DATE,
    CheckOutDate DATE,
    NumberOfADT INT,
    CHDCount INT,
    CHDAges NVARCHAR(100)
);

For the XML value I have provide above, the below Insert t-sql is suitable :

INSERT INTO @table VALUES(1453, '2011-10-27', '2011-11-04', 2, 2, '10;12');
INSERT INTO @table VALUES(1453, '2011-10-27', '2011-11-04', 1, 0, NULL);
INSERT INTO @table VALUES(1453, '2011-10-27', '2011-11-04', 1, 1, '7');

CHDCount is for the number of CHD nodes under Room node. Also, how many Room node I have, that many table row I am having here.

As for how it should look, see the below picture :

enter image description here

Actually, this code is for hotel reservation search query. So, I need
to work with these values I got from XML parameter to query my tables
and return available rooms. I am telling this because maybe it helps
you guys to see it through. I am not looking for a complete code for
room reservation system. That would be so selfish.

  • 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-26T12:03:15+00:00Added an answer on May 26, 2026 at 12:03 pm
    select S.X.value('ResortID[1]', 'int') as ResortID,
           S.X.value('CheckInDate[1]', 'date') as CheckInDate,
           S.X.value('CheckOutDate[1]', 'date') as CheckOutDate,
           R.X.value('NumberOfADT[1]', 'int') as NumberOfADT,
           R.X.value('count(CHD)', 'int') as CHDCount,
           stuff((select ';'+C.X.value('.', 'varchar(3)')
                  from R.X.nodes('CHD/Age') as C(X)
                  for xml path('')), 1, 1, '') as CHDAges
    from @XMLValue.nodes('/SearchQuery') as S(X)
      cross apply S.X.nodes('Room') as R(X)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two named instances of SQL Server 2008 and am trying to set
I am trying to connect to SQL Server 2008 server from Java here is
I'm using SQL Server 2008 (non-R2) and trying to use Report Builder 3.0. When
I'm trying to do a SQL Server 2008 setup and I've been given a
I am trying to use a SQL Server 2008 Ranking Function on a query
I'm trying to export records from SQL Server 2008 to mdb file using OpenDataSource.
I've just installed SQL Server 2008 Developer edition and I'm trying to connect using
I am using SQL Server 2008 developer edition. I was trying to attach the
I'm trying to setup the Entity Framework with SQL Server 2008. I'm using Guids
I'm trying to create a simple Add-On for SQL Server 2008; it is simply

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.