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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:55:46+00:00 2026-05-16T01:55:46+00:00

I am getting strange results from a query using XML EXPLICIT mode in T-SQL

  • 0

I am getting strange results from a query using XML EXPLICIT mode in T-SQL (SQL Server 2008).

Can someone explain what I am doing wrong?

Here is my example:

declare @parents table(id int, connection int, title nvarchar(255));
declare @children table(id int, connection int, title nvarchar(255));

insert into @parents(id, connection, title)
values(1, 21, '1');
insert into @parents(id, connection, title)
values(2, 22, '2');
insert into @parents(id, connection, title)
values(3, 23, '3');
insert into @parents(id, connection, title)
values(4, 24, '4');
insert into @parents(id, connection, title)
values(5, 25, '5');
insert into @parents(id, connection, title)
values(6, 26, '6');

insert into @children(id, connection, title)
values(1, 21, '1a');
insert into @children(id, connection, title)
values(2, 22, '2a');
insert into @children(id, connection, title)
values(3, 23, '3a');
insert into @children(id, connection, title)
values(4, 24, '4a');
insert into @children(id, connection, title)
values(5, 25, '5a');
insert into @children(id, connection, title)
values(6, 26, '6a');
insert into @children(id, connection, title)
values(7, 21, '1b');
insert into @children(id, connection, title)
values(8, 22, '2b');
insert into @children(id, connection, title)
values(9, 23, '3b');
insert into @children(id, connection, title)
values(10, 24, '4b');
insert into @children(id, connection, title)
values(11, 25, '5b');
insert into @children(id, connection, title)
values(12, 26, '6b');

select 1 as tag, null as parent,
 id as [p!1!id],
 title as [p!1!title],
 null as [c!2!id],
 null as [c!2!title]
from @parents p
union
 select 2 as tag, 1 as parent,
 p.id, 
 p.title, 
 c.id,
 c.title
from @parents p, @children c
where p.connection = c.connection
for xml explicit

and here is the strange result I am getting:

<p id="1" title="1" />
<p id="2" title="2" />
<p id="3" title="3" />
<p id="4" title="4" />
<p id="5" title="5" />
<p id="6" title="6">
  <c id="1" title="1a" />
  <c id="7" title="1b" />
  <c id="2" title="2a" />
  <c id="8" title="2b" />
  <c id="3" title="3a" />
  <c id="9" title="3b" />
  <c id="4" title="4a" />
  <c id="10" title="4b" />
  <c id="5" title="5a" />
  <c id="11" title="5b" />
  <c id="6" title="6a" />
  <c id="12" title="6b" />
</p>

OK – I think I need to be more specific, and include the “Sequence” portion of my problem as well as the original issue. Given the following data set:

declare @parents table(id int, connection int, title nvarchar(255), sequence int);
declare @children table(id int, connection int, title nvarchar(255), sequence int);

insert into @parents(id, connection, title, sequence)
values(1, 21, '1', 6), (2, 22, '2', 2), (3, 23, '3', 4), (4, 24, '4', 3), (5, 25, '5', 5), (6, 26, '6', 1);

insert into @children(id, connection, title, sequence)
values(1, 21, '1a', 2), (2, 22, '2a', 2), (3, 23, '3a', 2), (4, 24, '4a', 1), (5, 25, '5a', 2), (6, 26, '6a', 1), (7, 21, '1b', 1), (8, 22, '2b', 1), (9, 23, '3b', 1), (10, 24, '4b', 2), (11, 25, '5b', 1), (12, 26, '6b', 2);

How can I get the following result that has data like this:

<p id="6" title="6" sequence="1">
  <c id="6" title="6a" />
  <c id="12" title="6b" />
</p>
<p id="1" title="2" sequence="2">
 <c id="8" title="2b" sequence="1" />
 <c id="2" title="2a" sequence="2" />
</p>

with the

elements ordered by sequence and their child elements order by sequence within each?

Thanks for your help…I hope this more fully explains what I need, and why I am trying to use explicit mode.

  • 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-16T01:55:47+00:00Added an answer on May 16, 2026 at 1:55 am

    With SQL Server 2005 and up, forget about FOR XML EXPLICIT – use FOR XML PATH instead – it’s much easier to use, more expressive, more intuitive.

    Try this:

    SELECT
        p.ID AS '@id',
        p.title AS '@title',
        p.sequence as '@sequence',
        (SELECT 
             c.ID AS '@id', 
             c.Title AS '@title',
             c.sequence as '@sequence'
         FROM @children c 
         WHERE p.connection = c.connection
         ORDER BY c.sequence
         FOR XML PATH('c'), TYPE
        )
    FROM    
        @parents p
    ORDER BY    
        p.sequence
    FOR XML PATH('p')
    

    Output will be this: (order by p.sequence on the outer scope, and c.sequence in the inner scope)

    <p id="6" title="6" sequence="1">
      <c id="6" title="6a" sequence="1" />
      <c id="12" title="6b" sequence="2" />
    </p>
    <p id="2" title="2" sequence="2">
      <c id="8" title="2b" sequence="1" />
      <c id="2" title="2a" sequence="2" />
    </p>
    <p id="4" title="4" sequence="3">
      <c id="4" title="4a" sequence="1" />
      <c id="10" title="4b" sequence="2" />
    </p>
    <p id="3" title="3" sequence="4">
      <c id="9" title="3b" sequence="1" />
      <c id="3" title="3a" sequence="2" />
    </p>
    <p id="5" title="5" sequence="5">
      <c id="11" title="5b" sequence="1" />
      <c id="5" title="5a" sequence="2" />
    </p>
    <p id="1" title="1" sequence="6">
      <c id="7" title="1b" sequence="1" />
      <c id="1" title="1a" sequence="2" />
    </p>
    

    See the MSDN documentation on what’s new in SQL Server 2005 for more hints and tips how to use the FOR XML PATH to generate XML from your database contents…

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

Sidebar

Ask A Question

Stats

  • Questions 501k
  • Answers 501k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Give the Flex Formatter plugin a shot, only one I… May 16, 2026 at 2:23 pm
  • Editorial Team
    Editorial Team added an answer http://www.appfoundation.com/blogs/giametta/2009/04/14/render-pdf-in-adobe-air-with-pdf-scaling/ http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1375018 http://coenraets.org/blog/2009/11/open-in-excel-another-air-2-mini-sample/ also new docs are zip files, so… May 16, 2026 at 2:23 pm
  • Editorial Team
    Editorial Team added an answer Re how it works: the Wikipedia article discusses several techniques… May 16, 2026 at 2:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm getting something pretty strange going on when trying to read some data using
I'm pulling a lot of text from a MS SQL Server database. I'm not
I am using SSMS 2008 and trying to insert with this query but am
I'm having a hard time getting some sql in python to correctly go through
I'm getting a very strange behavior in MySQL, which looks like some kind of
Bit of a strange problem here... I've got an update query that isn't working,
Sigh I keep getting strange problems with IE 8. I have this <input name=Btn_Edit
I am getting a strange error after trying to compile under 2010. The compiler
getting confused here. I'm trying to install a second instance of reporting service sql
Getting this after solving a Error converting data type nvarchar to datetime error. Using

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.