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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:42:38+00:00 2026-06-13T07:42:38+00:00

In my database, I intend to get the – [TableName] as the first column

  • 0

In my database, I intend to get the – [TableName] as the first column and all columns of that table in a “, ” (comma followed by space – delimiter) as the second column – for all tables in the DB.

Table A | ColumnA1, ColumnA2, ColumnA3  
Table B | ColumnB1, ColumnB2, ColumnB3
......................................

And retrieve it as an XML

<TableList>  
<TableName>TableA</TableName> <Columns> ColumnA1, ColumnA2, ColumnA3</Columns>  
<TableName>TableB</TableName> <Columns> ColumnB1, ColumnB2, ColumnB3</Columns>  
</TableList>

How should the SQL Query be written?

  • 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-13T07:42:39+00:00Added an answer on June 13, 2026 at 7:42 am

    This will give you the XML you ask for.

    select T.name as TableName,
           (
             select ', '+C.name
             from sys.columns as C
             where C.object_id = T.object_id
             order by C.column_id
             for xml path(''), type
           ).value('substring((./text())[1], 3)', 'varchar(max)') as Columns
    from sys.tables as T
    order by T.name
    for xml path(''), root('TableList')
    

    But I think this would return an XML that is easier to handle.

    select T.name as TableName,
           (
             select ', '+C.name
             from sys.columns as C
             where C.object_id = T.object_id
             order by C.column_id
             for xml path(''), type
           ).value('substring((./text())[1], 3)', 'varchar(max)') as Columns
    from sys.tables as T
    order by T.name
    for xml path('Table'), root('TableList')
    

    Or perhaps like this.

    select T.name as TableName,
           (
             select C.name as ColumnName
             from sys.columns as C
             where C.object_id = T.object_id
             order by C.column_id
             for xml path(''), type
           ) as Columns
    from sys.tables as T
    order by T.name
    for xml path('Table'), root('TableList')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Database setup (MySQL) table: top_fives id, uid, first, second, third, fourth, fifth, creation_date 1,
Database has tables Photos and PhotoAlbums. I need a query that will select all
I would like to get a certain column from my sql database using the
I'm writing a Function that pulls Records from a DataBase using LINQ to get
I have a table in my database with columns: Lead_id INT(11) PRIMARY KEY NOT
This is my first time trying to set up a database. My intent is
Database is like: table book: book_id, title table wrote: book_id, author_id, author_order table author:
My database project needs to include a SQL file with a command that will
Hy, I was thinking that all my website will have use of cells, using
I am looking to get Microsoft Azure to run Sharepoint on, and I intend

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.