Home Forums Programming XML Returning Multiple Unrelated Tables in Single XML Output From Stored Proc RE: Returning Multiple Unrelated Tables in Single XML Output From Stored Proc

  • riaan-777462 (10/26/2012)


    Hi There,

    I'm quite inexperienced in returning XML from SQL and have only been using single tables to return normal XML in the past.

    Now, however, I need to return multiple tables (related or unrelated) in a single XML File for Exporting to another program.

    Something like:

    SELECT * FROM Products

    SELECT * FROM Colours

    SELECT * FROM Sizes

    FOR XML AUTO, ROOT, ELEMENTS

    Obviously the UNION operator is not going to work here so how would I need to write my T-SQL statement to return the following XML Hierarchy?

    <root>

    <Products>

    <...>

    </Products>

    <Colours>

    <...>

    </Colours>

    <Sizes>

    <...>

    </Sizes>

    </root>

    Any help will be appreciated. I've scoured the internet using keywords I think would return some result but, alas, clearly I'm not using the right keywords 🙂 so I'm hoping someone could point me in the right direction or perhaps even be so kind as to post the solution and save me from forced medical early retirement...

    Thanks in advance.

    Kind Regards,

    Riaan

    I would split this into three procs instead of trying to do this in a single one.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/