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

  • Tech Bang (2/27/2016)


    I'm looking for a similar solution, but instead of the 3 select statements, the values will come from 3 stored procs. I tried replacing the procs, get error.

    INSERT INTO #TempExportTable VALUES

    EXEC ('

    EXEC [dbo].[spGetProductsDesc] @tId FOR XML AUTO, ELEMENTS;

    EXEC [dbo].[spGetColoursDesc] @tId FOR XML AUTO, ELEMENTS;

    EXEC [dbo].[spGetSizesDesc] @tId FOR XML AUTO, ELEMENTS;

    ');

    Any help?

    DECLARE @TempExportTable TABLE

    (

    Products XML,

    Colours XML,

    Sizes XML

    )

    INSERT INTO @TempExportTable VALUES

    (

    (SELECT * FROM Products FOR XML AUTO, ELEMENTS),

    (SELECT * FROM Colours FOR XML AUTO, ELEMENTS),

    (SELECT * FROM Sizes FOR XML AUTO, ELEMENTS)

    )

    SELECT

    Products as '*',

    Colours as '*',

    Sizes as '*'

    from @TempExportTable

    FOR XML PATH('ExportList')

    Hi Tech Bang,

    I would suggest either changing the Stored Procs to a User-Defined-Function which returns a Table:

    e.g.

    CREATE FUNCTION MyProc()

    RETURNS TABLE AS

    RETURN (SELECT * FROM MyTable)

    Or using OpenQuery:

    e.g.

    INSERT INTO @TempExportTable VALUES

    (

    (SELECT * FROM OPENQUERY(YOURSERVERNAME, 'EXEC spGetProductsDesc @tId') FOR XML AUTO, ELEMENTS),

    (SELECT * FROM OPENQUERY(YOURSERVERNAME, 'EXEC spGetColoursDesc @tId') FOR XML AUTO, ELEMENTS),

    (SELECT * FROM OPENQUERY(YOURSERVERNAME, 'EXEC spGetSizesDesc @tId') FOR XML AUTO, ELEMENTS)

    )

    ...

    I haven't tested the code but should put you on the right track 🙂

    Regards,

    Riaan