TSQL: union results from two selects (procedures?)

sql, sql-server, stored-procedures, t-sql, union

Solution

You can convert the procedures to views.

OR

You can exec the procedures into a temp table, and then exec the other one into the same temp table:

create table #sometable (table definition here)

if @Param = 1 or @Param = 3 begin
    insert #sometable exec PROCEDURE1
end

if @Param = 2 or @Param = 3 begin
    insert #sometable exec PROCEDURE2
end

select * from #sometable

Problem

I have two procedures - two huge sets of selects with several sub-select and unions. I need to union results from these procedures and I still need them to exists separately. Something like that: ``` if @Param = 1 Then PROCEDURE1 if @Param = 2 THEN PROCEDURE2 if @Param = 3 Then PROCEDURE1 union PROCEDURE2 ``` I read that it's impossible to have union on procedures and I can't use temporary tables. Any idea?

Original source