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


Select using FOR XML


Select using FOR XML

Author
Message
Bryan McGuire
Bryan McGuire
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 507

Hi,


I have ben asked to supply an external supplier some data from our SQL Server 2012 database. Normally we would supply this data in CSV format. However, the supplier would like the data in XML format. I know I can use AUTO, RAW, PATH or EXPLICIT at the end of my select statement but I can't seem to replicate the example XML I was given, see below;
<Header>

<Header_UserName Val="Your user id"/>

<Header_Password Val="Your password"/>

<Header_TransDate Val="25/01/2017"/>

<Header_TransTime Val="09:00:00"/>

<Header_TransRef Val="test1234"/>

<Header_DocsReqdInd Val="N"/>

</Header>

As you can see the values are enclosed within quotes
Can anyone tell me what XML format is being used above and shine some light on where I might be going wrong.

Thanks for your help in advance

Bryan Smile


Mark Cowne
Mark Cowne
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19019 Visits: 26929
See if this helps


WITH Data AS (
SELECT 'Your user id' AS UserName,
'Your password' AS Password,
'25/01/2017' AS TransDate,
'09:00:00' AS TransTime,
'test1234' AS TransRef,
'N' AS DocsReqdInd
)
SELECT UserName AS 'Header_UserName/@Val',
Password AS 'Header_Password/@Val',
TransDate AS 'Header_TransDate/@Val',
TransTime AS 'Header_TransTime/@Val',
TransRef AS 'Header_TransRef/@Val',
DocsReqdInd AS 'Header_DocsReqdInd/@Val'
FROM Data
FOR XML PATH('Header');



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




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