+5  A: 

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(col1,..
insert into @tablevar(col1,..) exec MyStoredProc 'param1', 'param2'

SELECT col1, col2 FROM @tablevar
Gulzar
this won't work if MyStoredProc calls any other stored procs
chuck taylor
A: 
WITH SprocResults AS
(
    EXEC sproc
)
SELECT Col1, Col2 FROM SprocResults
Mark S. Rasmussen
This gives me the errorsIncorrect syntax near the keyword 'EXEC'Incorrect syntax near ')'
Rossini
+9  A: 

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.

Lance McNearney
A: 

Out of curiosity, does this query work:

SELECT * FROM EXEC MyStoredProc 'param1', 'param2'

If so, what column names does it display in the result set, and can you use those column names in your select list?

Dave Costa
see the last part of the question.
Broken Link
A: 

(Assuming SQL Server)

The only way to work with the results of a stored procedure in T-SQL is to use the INSERT INTO ... EXEC syntax. That gives you the option of inserting into a temp table or a table variable and from there selecting the data you need.

Brannon
+1  A: 

There is a way to do this (though not pretty!):

http://blogs.technet.com/wardpond/archive/2005/08/01/408502.aspx

A: 

This example uses the OpenRowSet command...

http://community.aspenware.com/blogs/johnmarquez/archive/2008/05/20/sql-server-openrowset-command.aspx

The link above seems to be redirecting to the home page of Aspenware.
David Laing
The link now returns 404 Not Found.
Matthew Murdoch
+2  A: 

try this

use mydatabase
create procedure sp_onetwothree as
select 1 as '1', 2 as '2', 3 as '3'
go
SELECT a.[1], a.[2]
FROM OPENROWSET('SQLOLEDB','myserver';'sa';'mysapass',
    'exec mydatabase.dbo.sp_onetwothree') AS a
GO
SelvirK
+1  A: 

It might be helpful to know why this is so difficult. A stored procedure may only return text (print 'text'), or may return multiple tables, or may return no tables at all.

So something like SELECT * FROM (exec sp_tables) Table1 will not work

newbie007
A: 

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 Name
DROP TABLE #Result

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

Peter Nazarov