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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:25:08+00:00 2026-05-21T05:25:08+00:00

I’m trying to generate XML that will output the structure and data of any

  • 0

I’m trying to generate XML that will output the structure and data of any table using SQL 2008.

So, for example, you would pass in a table name to this stored procedure and it would generate XML in this format:

<TABLE name="mytable">
  <ROW>
    <COL name="firstname">John</COL>
    <COL name="lastname">Smith</COL>
  </ROW>
  <ROW>
    <COL name="firstname">Bob</COL>
    <COL name="lastname">Jones</COL>
  </ROW>
</TABLE>

If there was a table called mytable (firstname varchar(100), lastname varchar(100))

You would be able to call stored procedure like this:

exec spGenerateTableData @tablename='mytable'

The trick I can’t get is how to make the column names attributes of the COL element. I’m sure you need to use FOR XML clause in select statement. Keep in mind, this should work given any table passed in – so you don’t know the names of the columns in advance.

Thanks for any help!

  • 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-21T05:25:08+00:00Added an answer on May 21, 2026 at 5:25 am

    You can use information_schema.columns and for xml path like this to get the structure you want.

    select
      'MyTable' as 'TABLE/@name',
      (select XMLCol as '*'
       from (select XMLCol
             from MyTable
               cross apply
                (select
                   COLUMN_NAME as 'COL/@name',
                   case COLUMN_NAME when 'FirstName' then FirstName end as COL,
                   case COLUMN_NAME when 'LastName' then LastName end as COL
                 from INFORMATION_SCHEMA.COLUMNS
                 where TABLE_NAME = 'MyTable'
                 for xml path(''), root('ROW'), type) as Row(XMLCol)
            ) as Rows
       for xml path(''), type) as 'TABLE'
    for xml path('')
    

    But in your case I see no other option than to build this dynamically.

    declare @TableName varchar(50) = 'MyTable'
    
    declare @ColList varchar(8000)
    select @ColList = coalesce(@ColList+', ', '') + 'case COLUMN_NAME when '''+COLUMN_NAME+''' then '+COLUMN_NAME+' end as COL'
    from INFORMATION_SCHEMA.COLUMNS
    where TABLE_NAME = @TableName
    
    declare @SQL varchar(max) = 
    'select
      ''_TABLENAME_'' as ''TABLE/@name'',
      (select XMLCol as ''*''
       from (select XMLCol
             from _TABLENAME_
               cross apply
                (select
                   COLUMN_NAME as ''COL/@name'',
                   _COLLIST_
                 from INFORMATION_SCHEMA.COLUMNS
                 where TABLE_NAME = ''_TABLENAME_''
                 for xml path(''''), root(''ROW''), type) as Row(XMLCol)
            ) as Rows
       for xml path(''''), type) as ''TABLE''
    for xml path('''')'
    
    set @SQL = replace(@SQL, '_TABLENAME_', @TableName)
    set @SQL = replace(@SQL, '_COLLIST_', @ColList)
    
    exec (@SQL)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.