views:

98

answers:

2

Hi, I want to join results of two different store procedures that returns same structure in following fashion:

   EXEC StoreProcedure1 p1
   UNION
   EXEC StoreProcedure2 p2

I realize that is not possible, can sombody suggest elegant alternative?

I beleive I should use temp table ?

Thanks

+3  A: 

You could convert the stored procedures into user defined functions that return tables instead and do:

SELECT * FROM dbo.MyFunction1(p1) 
UNION
SELECT * FROM dbo.MyFunction2(p2)
AdaTheDev
thanks, but I want to use store procedure
krul
You still would be: all the functions need to do is call the stored procedures and return the results. It's really the better way to do this.
Joel Coehoorn
Interesting, I'll reconsider
krul
+2  A: 

In SQL Server you could do the following:

create table #temp (
  col1 type,
  col1 type,
  ...
)

insert into #temp
exec sproc1
go
insert into #temp
exec sproc2
go

select * from #temp
go

drop table #temp

However, my preference would be to determine which queries both stored procedures are running and then write a query from scratch performing the same queries and unioning them together.

Adamski
as I expected, thanks, I was wandering if there was way around it
krul
just be aware of the performance hit from using temp tables for this. If you're dealing with small data, you'll prob not notice, but otherwise you will be hit by writing to tempdb
AdaTheDev