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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:44:20+00:00 2026-06-17T04:44:20+00:00

I want to store data from multiple tables into one table table A (

  • 0

I want to store data from multiple tables into one table

table A (
p, q,
PRIMARY KEY (p,q)
)

table B (
p, m, n,
PRIMARY KEY (p,m,n)
)

table output(
p,
xml
)

The output should be like

for table A

 -------------------------------
|    p      |        xml        |
---------------------------------
|value of p | <q>some value</q> |
 -------------------------------

for table B

 -----------------------------------------------
|    p      |        xml                        |
------------------------------------------------
|value of p | <m>some value</m><n>some data</n> |
 -----------------------------------------------

This can be achieved by this query

SELECT * 
FROM Students s
CROSS APPLY
(
    SELECT 
        (
            SELECT * 
            FROM Students t
            WHERE t.DisplayName = s.DisplayName
            FOR XML RAW
        ) x1
)x

But I want to make the SQL generic enough so that given any table name we can get the above output.

The problem with the inner join is that then the query will not be generic

  • 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-06-17T04:44:22+00:00Added an answer on June 17, 2026 at 4:44 am

    To get the result you want from one table you could use something like this:

    select T.Table1ID,
           (
             select T.*
             for xml raw, type
           ) as x1
    from Table1 as T
    

    To apply the same pattern to another table you need to change the table name and the field name of the key column.

    select T.Table2ID,
           (
             select T.*
             for xml raw, type
           ) as x1
    from Table2 as T
    

    If you want a even more generic version you can use dynamic SQL and build your query from a table name and a column name.

    declare @TableName sysname 
    declare @KeyColumnName sysname
    
    set @TableName = 'master..spt_values'
    set @KeyColumnName = 'Number'
    
    declare @SQL nvarchar(500)
    
    set @SQL = '
    select '+@KeyColumnName+',
           (
             select T.*
             for xml raw, type
           ) as x1
    from '+@TableName+' as T'
    
    exec (@SQL)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to store a data structure thats a table with one column Strings
I want to query some data in multiple tables as if it were one
I want to develop web store on GAE, that takes data directly from OpenERP
I have created a stored procedure to delete data from multiple tables. my work
I have a view which includes data from multiple tables. When I call that
I am trying to create an ANI lookup table from 2 separate tables, one
here are my tables test(tno,tname), test2(t2no,tno,...); test3(t3no,t2no,y,...); I want into insert the data into
In asp.net(C#) i want to fetch data from a database using a stored procedure
I want to store data in string form to MySQL. I have created the
I want to store data with every request (what user viewed what page of

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.