Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select from a result set created by a stored proc

Author  Topic 

esloat
Starting Member

8 Posts

Posted - 2010-04-02 : 12:15:53
I have a stored procedure with one input parameter. I want to execute this SP and then use the result set (table) as a subquery in a select statement. The result set from the SP does not need to be a permenant table

I found that I cannot use an EXEC cmd within a select statement (i.e. subquery or derived table) and am confused as to how to make the result set from the executed SP available to anothet select statement.

the simplified example is:
(1) I have a table of student ID's and names [Student_Table]
(2) I have a parameratized SP that returns a subset of student ID's and teacher names [SP_Teacher_Names]
(3) I want to select the student records from the [Student_Table] based on a join from the result set returned after executing the SP [SP_Teacher_Names]

I think this is how to set up the SP to create a temporary table, but I haven't been able to make it work.

Any thoughts would be greatly appreciated. Much thanks.

Ed Sloat

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-04-02 : 12:36:45
There are several ways to do this.

1. OPENQUERY
2. SELECT INTO table
3. etc..

Here is a link that has a bunch of methods:
http://www.sqlusa.com/bestpractices2005/selectfromsproc/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-02 : 12:41:04
if you want to use sp result to join to table why not make it a table valued udf? so that it can be used just like table in join.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

esloat
Starting Member

8 Posts

Posted - 2010-04-07 : 13:01:11
Thanks for these suggestions. I will try them out. Also, thanks for the referal to the SP examples. This has helped me a great deal to further understand the different ways to call and use SP and temporary tables. Thanks all.

Ed Sloat
Go to Top of Page
   

- Advertisement -