Union Exec's

  • Iulian -207023


    Points: 7509


    how to change the code to make UNION between the results of the two exec's

    and the result to have two rows (one with 1 and the 2nd with 2)

    declare @s1 as varchar(100)

    declare @s2 as varchar(100)

    set @s1 = 'SELECT 1'

    set @s2 = 'SELECT 2'

    exec( @s1 )


    exec( @s2 )



  • Gail Shaw

    SSC Guru

    Points: 1004474

    Create a temp table, use Insert ... EXEC to insert the output of the procedures into the table, then query the table.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • srikant maurya

    Hall of Fame

    Points: 3293

    What about this...

    exec ( @s1+ ' UNION '+ @s2 )

  • Iulian -207023


    Points: 7509

    Thanks a lot.


    What about this...

    exec ( @s1+ ' UNION '+ @s2 )

    that is nice, I have not thought about it. Unfortunately this query string is a sample, the real one is more complex.

    I created a stored procedure that inserts records and another one that merges them all.

    Have a great day,


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

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