OUTPUT issue with sp_executesql

  • Hi,

    I wonder if anyone can answer this/help. I am trying to select a blob of XML and then put this XML into a variable like so:

    DECLARE @retval XML

    DECLARE @sSQL nvarchar(500);

    DECLARE @ParmDefinition nvarchar(500);

    DECLARE @tablename nvarchar(50)

    DECLARE @ApplNbr VARCHAR(20)

    SET @ApplNbr = 24

    SELECT @tablename = N'[myDB].[dbo].[myTable]'

    SELECT @sSQL = N'SELECT * FROM ' + @tablename + 'WHERE [ApplNbr] = '+ @ApplNbr + '

    FOR XML PATH(''myDB.myTable'')';

    SET @ParmDefinition = N'@retvalOUT XML OUTPUT';

    EXEC sp_executesql @sSQL, @ParmDefinition, @retvalOUT=@retval OUTPUT;

    SELECT @retval; --<------- Why does this return NULL?

    However, I want the XML to to set to the @retval var, but this seems to lose its value once the SP has run and OUTPUT. How can I get this var to retain this value as I need to concatenate this into one xml blob?

    SET @OrigXML = (SELECT @retval

    ,@retval2

    ,@retval3

    FOR XML PATH (''), ROOT('Table'))

    Cheers, Phil

  • You need to assign to @retvalOUT inside the sp_ExecuteSQL string,

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Thanks, but I still get the same results. Is the syntax correct:

    DECLARE @retval XML

    DECLARE @sSQL nvarchar(500);

    DECLARE @tablename nvarchar(50)

    DECLARE @ApplNbr VARCHAR(20)

    SET @ApplNbr = 24

    SELECT @tablename = N'[amsbranch].[dbo].[tAppl]'

    SELECT @sSQL = N'DECLARE @retvalOUT XML

    SELECT * FROM ' + @tablename + 'WHERE [ApplNbr] = '+ @ApplNbr + ' FOR XML PATH(''amsbranch.tAppl'')' ;

    EXEC sp_executesql @sSQL

    SELECT @retval

  • phil.layzell (5/13/2012)


    Thanks, but I still get the same results. Is the syntax correct:

    ...

    No. Like this:

    DECLARE @retval XML

    DECLARE @sSQL nvarchar(500);

    DECLARE @ParmDefinition nvarchar(500);

    DECLARE @tablename nvarchar(50)

    DECLARE @ApplNbr VARCHAR(20)

    SET @ApplNbr = 24

    SELECT @tablename = N'[myDB].[dbo].[myTable]'

    SELECT @sSQL = N'

    SELECT @retvalOUT = (SELECT * FROM ' + @tablename + 'WHERE [ApplNbr] = '+ @ApplNbr + '

    FOR XML PATH(''myDB.myTable'') )';

    SET @ParmDefinition = N'@retvalOUT XML OUTPUT';

    EXEC sp_executesql @sSQL, @ParmDefinition, @retvalOUT=@retval OUTPUT;

    SELECT @retval; --<------- Why does this return NULL?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Doh! Thanks that was too simple 🙂

    Cheers, Phil

  • Glad I could help!

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

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

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