SQL to XML

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

  • 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

     

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

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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

  • Adi. Thanks. That's exactly what I was looking for.

Viewing 7 posts - 1 through 6 (of 6 total)

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