Select using FOR XML

  • 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 🙂

  • 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

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply