SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL to XML


SQL to XML

Author
Message
Bill.Smith
Bill.Smith
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 12
Any suggestion on how to create XML like this from SQL Server?

I was able to create the XML without the Metadata lines using, "FOR XML RAW ('Request'), ELEMENTS".

<Request>
<RequestType>NEW</RequestType>
<CompanyName>Main St. Bistro</CompanyName>
<CompanyID>1234567890</MerchantID>
<Country>US</Country>
<PostalCode>60602</PostalCode>
<Metadata name="PROGRAM" global="true">A</Metadata>
<Metadata name="LEVEL" global="true">2</Metadata>
<Metadata name=”PROMOTION”>HAPPYMEAL</Metadata>
</Request>
Steven Willis
Steven Willis
SSC Eights!
SSC Eights! (859 reputation)SSC Eights! (859 reputation)SSC Eights! (859 reputation)SSC Eights! (859 reputation)SSC Eights! (859 reputation)SSC Eights! (859 reputation)SSC Eights! (859 reputation)SSC Eights! (859 reputation)

Group: General Forum Members
Points: 859 Visits: 1721
Bill.Smith (4/29/2013)
Any suggestion on how to create XML like this from SQL Server?

I was able to create the XML without the Metadata lines using, "FOR XML RAW ('Request'), ELEMENTS".

<Request>
<RequestType>NEW</RequestType>
<CompanyName>Main St. Bistro</CompanyName>
<CompanyID>1234567890</MerchantID>
<Country>US</Country>
<PostalCode>60602</PostalCode>
<Metadata name="PROGRAM" global="true">A</Metadata>
<Metadata name="LEVEL" global="true">2</Metadata>
<Metadata name=”PROMOTION”>HAPPYMEAL</Metadata>
</Request>


Perhaps the examples in this post will help give you some ideas:

SQL to XML Examples

 
Adi Cohn
Adi Cohn
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3777 Visits: 6513
Can you post also a small script that creates the table and insert test data? Without it I don't think that anyone will be able to help you with the SQL statement that converts the relational data to XML.

Adi

--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89677 Visits: 41144
Heh... by the same token (pun intended), considering the tag bloat of XML, I'm still fasciated that anyone would actually use it especially for such flat data.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Bill.Smith
Bill.Smith
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 12
-- Here's the sample data. I was able to get one Metadata line. I was hoping this would be easy to do using the XML functionality of SQL server. I was intending to put this in a stored procedure that returns XML. The stored procedure is consumed by an application that would write the XML to a file, etc... If this gets too complicated in T-SQL then the XML formatting could be moved off to the application.

declare @Request table
(
[RequestType] varchar(10)
,[CompanyName] varchar(50)
,[CompanyID] int
,[Country] varchar(2)
,[PostalCode] varchar(9)
--
,[Program] char(1)
,[Level] int
,[Promotion] varchar(10)
);

insert into @Request values ('NEW', 'Main St. Bistro', 1234567890, 'US', '60602', 'A', 2, 'HAPPYMEAL');

DECLARE @ExportXML XML

SET @ExportXML =
(
SELECT
[RequestType]
,[CompanyName]
,[CompanyID]
,[Country]
,[PostalCode]
--<Metadata name="PROGRAM" global="true">A</Metadata>
,'PROGRAM' 'Metadata/@name'
,'true' 'Metadata/@global'
,Program Metadata
-- Need the Level and Promotion as additonal Metada rows like this...
--<Metadata name="LEVEL" global="true">2</Metadata>
--,'PROGRAM' 'Metadata/@name'
--,'true' 'Metadata/@global'
--,Level Metadata
--<Metadata name=”PROMOTION”>HAPPYMEAL</Metadata>
FROM
@Request
FOR XML PATH(''), ROOT('Request')
)

SELECT @ExportXML AS ExportXML

--Trying to add additional Metadata rows results in
--Msg 6852, Level 16, State 1, Line 18
--Attribute-centric column 'Metadata/@name' must not come after a non-attribute-centric sibling in XML hierarchy in FOR XML PATH.
Adi Cohn
Adi Cohn
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3777 Visits: 6513
If I understood what you want, then you can use sub queries that create XML. Notice that when you do it you have to use the directive type (If you'll play with it a bit, you'll see why). Bellow is code that does it:

declare @Request table
(
[RequestType] varchar(10)
,[CompanyName] varchar(50)
,[CompanyID] int
,[Country] varchar(2)
,[PostalCode] varchar(9)
--
,[Program] char(1)
,[Level] int
,[Promotion] varchar(10)
);

insert into @Request values ('NEW', 'Main St. Bistro', 1234567890, 'US', '60602', 'A', 2, 'HAPPYMEAL');

DECLARE @ExportXML XML

SET @ExportXML =
(
SELECT
[RequestType]
,[CompanyName]
,[CompanyID]
,[Country]
,[PostalCode]
--<Metadata name="PROGRAM" global="true">A</Metadata>
,'PROGRAM' 'Metadata/@name'
,'true' 'Metadata/@global'
,Program Metadata
-- Need the Level and Promotion as additonal Metada rows like this...
--<Metadata name="LEVEL" global="true">2</Metadata>
,(select 'LEVEL' as 'Metadata/@name'
,'true' 'Metadata/@global' for xml path (''), type)
--<Metadata name=”PROMOTION”>HAPPYMEAL</Metadata>
,(select 'PROMOTION' as 'Metadata/@name' for xml path(''), type)
FROM
@Request
FOR XML PATH(''), ROOT('Request')
)

select @ExportXML




Adi

--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Bill.Smith
Bill.Smith
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 12
Adi. Thanks. That's exactly what I was looking for.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search