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

  • Home
  • SEARCH
  • 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 993303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:24:05+00:00 2026-05-16T06:24:05+00:00

I was wondering how I might take a table schema in SQL Server and

  • 0

I was wondering how I might take a table schema in SQL Server and generate an XML document from it. The ideal would be if I passed in my database name (“SalesOrders”) and an XML doc comes back reading something like:

<table=”SalesOrders”>
<columns>
    <name=”SalesOrderID”/>
        <datatype=”int”/>
        <allowNulls=”false”/>
    </name>
    <name=”DateOfSale”>
        <datatype=”DateTime”/>
        <allowNulls=”false”/>
    </name>
</columns>
</table>

You get the idea. Something along these lines, an XSD schema would be fine too. In the back of my head I think SQL Server has mechanisms for doing this but I’m not positive. Many thanks for your suggestions.

  • 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-16T06:24:06+00:00Added an answer on May 16, 2026 at 6:24 am

    Something like the following would work. Also, note that your example XML is not well formed. I took the liberty of making it well formed.

    declare @tableName varchar(255)
    
    select @tableName = 'SalesOrders'
    
    select (
        select  column_name,
                data_type,
                case(is_nullable)
                    when 'YES' then 'true'
                    else 'false'    
                end as is_nullable
        from information_schema.columns [columns]
        where table_name = @tableName 
        for xml auto, type 
    ).query ('  <table name="{sql:variable("@tableName")}">
                {
                    for $column in /columns
                    return 
    
                    <column name="{data($column/@column_name)}">
                        <dataType value="{data($column/@data_type)}"/> 
                        <allowNulls value="{data($column/@is_nullable)}"/> 
                    </column>
                }
                </table>            
    ')
    

    or

    select @tableName as "@name",
    (
        select  column_name as "@name",
            data_type as "dataType/@value",
            case(is_nullable)
                when 'YES' then 'true'
                else 'false'    
            end as "allowNulls/@value"
        from information_schema.columns
        where table_name = @tableName
        for xml path('column'), type
    )
    for xml path('table')
    

    Both queries would produce the following:

    <table name="SalesOrders"> 
    <columns> 
        <column name="SalesOrderID"> 
            <datatype value="int"/> 
            <allowNulls value="false"/> 
        </column > 
        <column name="DateOfSale"> 
            <datatype value="DateTime"/> 
            <allowNulls value="false"/> 
        </column > 
    </columns> 
    </table>
    

    As a side note:

    Although it’s usually a matter of taste when deciding on elements vs attributes in an XML structure, I would make dataType and allowNulls attribtes as opposed to elements, which seems more intuitive to me. So, the XML structure would look something like this:

    <table name="SalesOrders">    
        <columns>    
            <column name="SalesOrderID" datatype="int" allowNulls="false"/>
            <column name="DateOfSale" datatype="DateTime" allowNulls="false"/>   
        </columns>    
    </table>
    

    The above queries can be easily modified to reflect this change.

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

Sidebar

Related Questions

I was wondering how I might take a CronTrigger and add an extra one-off
I was wondering if anyone knew of an application that would take a GEDCOM
I would like to be able to take something like a two-way table of
I was wondering how you might be able to update a CURL (PHP) request
I'm struggling getting some unit tests running and wondering if anyone might have anything
I'm a noob at this and was wondering if someone might be able to
I was wondering what approaches others might have for testing domain services against a
I'm wondering if it's possible (recommended might be the better word) to use sed
It might be an obvious question, but I was wondering how do you start
It might be a silly question, but I am wondering what other professionals deal

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.