Select columns from result set of stored procedure Select columns from result set of stored procedure sql-server sql-server

Select columns from result set of stored procedure


Can you split up the query? Insert the stored proc results into a table variable or a temp table. Then, select the 2 columns from the table variable.

Declare @tablevar table(col1 col1Type,..insert into @tablevar(col1,..) exec MyStoredProc 'param1', 'param2'SELECT col1, col2 FROM @tablevar


Here's a link to a pretty good document explaining all the different ways to solve your problem (although a lot of them can't be used since you can't modify the existing stored procedure.)

How to Share Data Between Stored Procedures

Gulzar's answer will work (it is documented in the link above) but it's going to be a hassle to write (you'll need to specify all 80 column names in your @tablevar(col1,...) statement. And in the future if a column is added to the schema or the output is changed it will need to be updated in your code or it will error out.


CREATE TABLE #Result(  ID int,  Name varchar(500), Revenue money)INSERT #Result EXEC RevenueByAdvertiser '1/1/10', '2/1/10'SELECT * FROM #Result ORDER BY NameDROP TABLE #Result

Source:
http://stevesmithblog.com/blog/select-from-a-stored-procedure/