Solved

SQL Server: SELECT query with FOR XML clause

Posted on 2008-06-11
5
1,205 Views
Last Modified: 2012-08-13
I have to create XML output for the table in SQL Server database. All columns should be childred of the element with the table name. One column should be an attribute of the element with the table name. Can it be accomplished somehow using FOR XML clause in SELECT query?
Thanks.
0
Comment
Question by:quasar_ee
  • 3
5 Comments
 
LVL 2

Expert Comment

by:howyue
ID: 21766107
can u provide the sample table structure and the sample xml?
0
 

Author Comment

by:quasar_ee
ID: 21766134
table1:
column1
column2
column3
...
columnN

XML:
<ROOT>
   <table1 column2="value2">
      <column1> value1 </column1>
      <column3> value3 </column3>
      ...
     <columnN> valueN </columnN>
   </table1>
</ROOT>
0
 
LVL 2

Accepted Solution

by:
howyue earned 500 total points
ID: 21766275
is this ur desired result?
CREATE TABLE Table1 (

	Column1 VARCHAR(10),

	Column2 VARCHAR(10),

	Column3 VARCHAR(10),

	Column4 VARCHAR(10)

)

INSERT INTO Table1

SELECT 'Value1', 'Value2', 'Value3', 'Value4'
 

SELECT Column2 AS '@Column2', Column1, Column3, Column4

FROM Table1

FOR XML PATH ('Table1')

Open in new window

0
 
LVL 2

Expert Comment

by:howyue
ID: 21766283
SELECT Column2 AS '@Column2', Column1, Column3, Column4
FROM Table1
FOR XML PATH ('Table1'), ROOT('ROOT')

use this one if u stil wan ur "ROOT"
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 21774997
Try this little test - it does create a table for the purposes of the test...


create table tst_xml (column1 varchar(20),column2 varchar(20),column3 varchar(20),columnN varchar(20))

insert tst_xml values ('column1_R1','column2_R1','column3_R1','columnN_R1')

insert tst_xml values ('column1_R2','column2_R2','column3_R2','columnN_R2')

insert tst_xml values ('column1_R3','column2_R3','column3_R3','columnN_R3')
 

-- now do the 'for XML'

select column2 as '@column2',column1,column3,columnN from tst_xml for xml path('table1'),root('root')

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sql server lock cursor 13 41
How to calculate iops? 12 27
Contained Database Collations 6 20
SqlAdvisor 2016 3 5
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now