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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:48:47+00:00 2026-05-27T15:48:47+00:00

I have a table like this: ID Name Column1 Column2 Column3 1 ABC 202.2

  • 0

I have a table like this:

ID    Name    Column1    Column2    Column3

1     ABC     202.2      1500       34000
2     IJK     104        10000      27000

I want to generate XML like this:

<doc>
    <record ID="1" Name="ABC" Column1="202.2" Column2="15000" Column3="34000" />
    <record ID="2" Name="IJK" Column1="104" Column2="10000" Column3="27000" />
</doc>

I have got some clue from this forum post and used this code:

CREATE TABLE #tmp (column1 VARCHAR(20), column2 VARCHAR(20), column3 VARCHAR(20))

INSERT INTO #tmp VALUES ( 'data1', 'data2', 'data3' )
INSERT INTO #tmp VALUES ( 'data11', 'data21', 'data31' )

-- FOR XML PATH with ELEMENTS will automatically unpivot the data for you
-- Then reshape your XML using nested FLWOR loops
SELECT
    (
    SELECT *
    FROM #tmp t
    FOR XML PATH, ELEMENTS, TYPE
    ).query('
for $e in row
 return 
 <row>{
  for $f in $e/*
   return <field name="{local-name($f)}">{data($f)}</field>
     }
 </row>
')

I tried the following modified version:

SELECT (SELECT * FROM cte_temp t FOR XML PATH, ELEMENTS, TYPE)
.query('for $e in row return 
 <doc> 
 {
        for $f in $e return <record {local-name($f)}="{data($f)}" />
 }
</doc>')

But I’m getting error:

XQuery [query()]: Invalid source character 0x7b found in an identifier 
                  near 'return'.
  • 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-27T15:48:48+00:00Added an answer on May 27, 2026 at 3:48 pm

    Why you trying to get fancy.

    Select  * from #tmp as record
    FOR XML AUTO,  root('doc')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table structure with columns like this [ID] [Name] [ParentId] [ParentName] The
I have a table like this: name code group john 12 smith 15 how
Let's say I have a table like this: name | score_a | score_b -----+---------+--------
I have a table in mysql like this Name Result T1_09_03_2010 fail T2_09_03_2010 pass
i have a table like this one: -------------------------------- id | name -------------------------------- 1 |
I have a table like this (simplified): ID | Name | Parent --------------------------------- 1
If I have a table like this: CREATE TABLE sizes ( name ENUM('small', 'medium',
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
I have a table where a record looks like this varchar(255) Name varchar(255) Text
I have datatable table like id name address phoneno 1 abc kfjskl 798798 2

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.