Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL: union results from two selects (procedures?)

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?

like image 991
Gregi Avatar asked Jan 09 '10 18:01

Gregi


People also ask

How do you do a UNION with two queries?

To use the UNION operator, you write the dividual SELECT statements and join them by the keyword UNION. The columns returned by the SELECT statements must have the same or convertible data type, size, and be the same order. The database system processes the query by executing two SELECT statements first.

Can I UNION more than 2 tables?

Conclusion. Combining several tables to one large table is possible in all 3 ways. As we have seen, the behavior of UNION in SQL Server and UNION in DAX within Power BI is very similar. Here tables with the same number of columns are placed directly under each other.


1 Answers

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
like image 129
Gabriel McAdams Avatar answered Sep 19 '22 22:09

Gabriel McAdams