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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:25:55+00:00 2026-05-30T18:25:55+00:00

I’m trying to figure out how to query a table that generates xml that

  • 0

I’m trying to figure out how to query a table that generates xml that looks like below:
(this is a sample from the AdventureWorks database.

I can get the column names as the elements easy enough, but is it possible to make both the column name and value as attributes? I’m trying to figure out how to do this in a generic fashion, so I don’t want to hardcode the column names using FOR EXPLICIT

    <TABLE name="StateProvince">
        <ROW>
            <COL name="StateProvinceID" value="1" />
            <COL name="StateProvinceCode" value="AB" />
            <COL name="CountryRegionCode" value="CA" />
            <COL name="IsOnlyStateProvinceFlag" value="0" />
            <COL name="Name" value="Alberta" />
            <COL name="TerritoryID" value="6" />
            <COL name="rowguid" value="298C2880-AB1C-4982-A5AD-A36EB4BA0D34" />
            <COL name="ModifiedDate" value="2004-03-11T10:17:21.587" />
        </ROW>
    </TABLE>
  • 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-30T18:25:57+00:00Added an answer on May 30, 2026 at 6:25 pm

    Doing this without specifying column names is a bit tricky but doable.
    To use this you need to replace @T with your table name and change the table name constant 'StateProvince' to your tables name.

    declare @T table
    (
      StateProvinceID int,
      StateProvinceCode char(2),
      CountryRegionCode char(2),
      IsOnlyStateProvinceFlag int,
      Name varchar(50),
      TerritoryID int,
      rowguid uniqueidentifier,
      ModifiedDate datetime
    )
    
    insert into @T values
    (1, 'AB', 'CA', 0, 'Alberta', 6, '298C2880-AB1C-4982-A5AD-A36EB4BA0D34', '2004-03-11T10:17:21.587'),
    (2, 'AB', 'CA', 0, 'Alberta', 6, '298C2880-AB1C-4982-A5AD-A36EB4BA0D34', '2004-03-11T10:17:21.587')
    
    select 'StateProvince' as [@name],
      (
        select 
          (
            select T3.N.value('local-name(.)', 'sysname') as [@name],
                   T3.N.value('.', 'nvarchar(max)') as [@value]
            from (
                   select T1.*
                   for xml path(''), type
                 ) T2(N)
              cross apply T2.N.nodes('*') as T3(N)       
            for xml path('COL'), root('ROW'), type
          )         
        from @T as T1
        for xml path(''), type
      )
    for xml path('TABLE')  
    

    Result:

    <TABLE name="StateProvince">
      <ROW>
        <COL name="StateProvinceID" value="1" />
        <COL name="StateProvinceCode" value="AB" />
        <COL name="CountryRegionCode" value="CA" />
        <COL name="IsOnlyStateProvinceFlag" value="0" />
        <COL name="Name" value="Alberta" />
        <COL name="TerritoryID" value="6" />
        <COL name="rowguid" value="298C2880-AB1C-4982-A5AD-A36EB4BA0D34" />
        <COL name="ModifiedDate" value="2004-03-11T10:17:21.587" />
      </ROW>
      <ROW>
        <COL name="StateProvinceID" value="2" />
        <COL name="StateProvinceCode" value="AB" />
        <COL name="CountryRegionCode" value="CA" />
        <COL name="IsOnlyStateProvinceFlag" value="0" />
        <COL name="Name" value="Alberta" />
        <COL name="TerritoryID" value="6" />
        <COL name="rowguid" value="298C2880-AB1C-4982-A5AD-A36EB4BA0D34" />
        <COL name="ModifiedDate" value="2004-03-11T10:17:21.587" />
      </ROW>
    </TABLE>
    

    Note: The column names in your table has to be a valid XML element name. You can for instance not have any spaces in the column name.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I've got a string that has curly quotes in it. I'd like to replace
I'm trying to create an if statement in PHP that prevents a single post
I am trying to render a haml file in a javascript response like so:
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I would like to run a str_replace or preg_replace which looks for certain words

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.