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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:49:24+00:00 2026-05-22T18:49:24+00:00

Though this works to return the right results, SELECT r.KeyColumn as ‘@Key’, t1.Key1 as

  • 0

Though this works to return the right results,

SELECT r.KeyColumn as '@Key', t1.Key1 as 'Key1', t1.Col1 as 'Col1' 
FROM @resultset r
INNER JOIN Table1 t1 ON t1.Col1 = 'SomeCondition'
--FOR XML PATH('Column1')  -- This errors out when used with union, works as a seperate query.

UNION
SELECT r.KeyColumn as '@Key', t2.Key1 as 'Key2', t2.Col2 as 'Col2' 
FROM @resultset r
INNER JOIN Table2 t2 ON t2.Col1 = 'SomeCondition2'
--FOR XML PATH('Column2')  -- This errors out when used with union, works as a seperate query.

Results:

@Key Key1  Col1
1   1  Table1Col1
1   1  Table2Col

The problem is that the results together doesn’t differentiate between the different naming I’m trying to use, because of the UNION. How can I get both to display under one resultset but with different names? I’m thinking XML to do this, but am unable to figure out how?

What’s an optimal way to combine different queries with different results/number of rows and put everything under one big XML?

Ok, the best I could come up with is:

SELECT r.KeyColumn as '@Key', t1.Key1 as 'Key1', t1.Col1 as 'Col1',  '' as 'Key2', '' as 'Col2'
          FROM @resultset r
    INNER JOIN Table1 t1 ON t1.Col1 = 'SomeCondition'
    --FOR XML PATH('Column1')  --Error

     UNION 

     SELECT r.KeyColumn as '@Key', '' AS 'Key1', '' as 'Col1', t2.Key1 as 'Key2', t2.Col2 as 'Col2' 
    FROM @resultset r
    INNER JOIN Table2 t2 ON t2.Col1 = 'SomeCondition2'
    --FOR XML PATH('Column2')  -- Error

Will give me results (again, as expected because of the Union)

    @Key    Key1        Col1        Key2    Col2
---------------------------------------------------------------------------
    1       1           Table1Col1
    1                               1       Table2Col

I still want to get my results as an XML thus:

<Root>
<Column1 Key="1">
  <Key1>1</Key1>
  <Col1>Table1Col1</Col1>
</Column1>

<Column2 Key="1">
  <Key2>1</Key2>
  <Col2>Tabl2Col</Col2>
</Column2>
</Root>

or something on these lines:

 <Root Key="1">
    <Column1>
      <Key1>1</Key1>
      <Col1>Table1Col1</Col1>
    </Column1>

    <Column2>
      <Key2>1</Key2>
      <Col2>Tabl2Col</Col2>
    </Column2>
    </Root>
  • 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-22T18:49:25+00:00Added an answer on May 22, 2026 at 6:49 pm

    Are you looking for something like this?

    declare @T table 
    (
      KeyColumn int,
      Col1 varchar(10),
      Col2 varchar(10)
    )
    
    insert into @T values(1, 'Col1', 'Col2')
    
    select (
             select KeyColumn,
                    Col1
             from @T
             for xml path('Query1'), type       
           ),
           (
             select KeyColumn,
                    Col2
             from @T
             for xml path('Query2'), type       
           )
    for xml path('root')
    

    Result:

    <root>
      <Query1>
        <KeyColumn>1</KeyColumn>
        <Col1>Col1</Col1>
      </Query1>
      <Query2>
        <KeyColumn>1</KeyColumn>
        <Col2>Col2</Col2>
      </Query2>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anyone seen this: ?? No jgem command works at all?? Though jruby -S
I'm running this query SELECT country, countries.code, countries.lat, countries.lng, countries.zoom, worldip.start, worldip.end FROM countries,
I'm using WCF through Spring.net WCF integration link text This works relatively fine, however
I'm basically work with Middleman 2 though if this can only be done in
Never thought I'd have this problem :) The following snippet of code works in
This code works, for most of time, so I'm thinking of some race condition.
I know this is embarrassing easy but I cannot get this to work right
I use the following stored procedure from my SQL Server 2008 database to return
EDIT: Thank you @yoda and @morispaa. You are both right and @morispaa's solution works,
My PHP project works by grabbing data from a WCF REStful web service with

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.