Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to SELECT FROM stored procedure

I have a stored procedure that returns rows:

CREATE PROCEDURE MyProc
AS
BEGIN
    SELECT * FROM MyTable
END

My actual procedure is a little more complicated, which is why a stored procedure is necessary.

Is it possible to select the output by calling this procedure?

Something like:

SELECT * FROM (EXEC MyProc) AS TEMP

I need to use SELECT TOP X, ROW_NUMBER, and an additional WHERE clause to page my data, and I don't really want to pass these values as parameters.

like image 868
jonathanpeppers Avatar asked Sep 29 '09 13:09

jonathanpeppers


People also ask

Can I select from stored procedure?

SQL Server select from stored procedure with parameters In a stored procedure, we can pass multiple parameters and also use these parameter values to execute multiple queries within a stored procedure. And in this topic, we will try to learn how to select data returned by a stored procedure having multiple parameters.

How do I query a stored procedure?

Click on your database and expand “Programmability” and right click on “Stored Procedures” or press CTRL+N to get new query window. You can write the SELECT query in between BEGIN and END to get select records from the table.

How do you call a stored procedure in SQL select query?

In Object Explorer, connect to an instance of the SQL Server Database Engine, expand that instance, and then expand Databases. Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure.

How do I write a stored procedure for select query in SQL Server?

How to create a SELECT stored procedure? Click on your Database and expand “Programmability” item and right click on “Stored Procedures” or press CTRL + N to get new query window. In the query area between BEGIN and END, type your SELECT statement to select records from the table.


6 Answers

You can

  1. create a table variable to hold the result set from the stored proc and then
  2. insert the output of the stored proc into the table variable, and then
  3. use the table variable exactly as you would any other table...

... sql ....

Declare @T Table ([column definitions here]) Insert @T Exec storedProcname params  Select * from @T Where ... 
like image 66
Charles Bretana Avatar answered Oct 02 '22 13:10

Charles Bretana


You can use a User-defined function or a view instead of a procedure.

A procedure can return multiple result sets, each with its own schema. It's not suitable for using in a SELECT statement.

like image 21
mmx Avatar answered Oct 02 '22 14:10

mmx


You either want a Table-Valued function or insert your EXEC into a temporary table:

INSERT INTO #tab EXEC MyProc
like image 29
CMerat Avatar answered Oct 02 '22 14:10

CMerat


You need to declare a table type which contains the same number of columns your store procedure is returning. Data types of the columns in the table type and the columns returned by the procedures should be same

declare @MyTableType as table
(
FIRSTCOLUMN int
,.....
)  

Then you need to insert the result of your stored procedure in your table type you just defined

Insert into @MyTableType 
EXEC [dbo].[MyStoredProcedure]

In the end just select from your table type

Select * from @MyTableType
like image 25
Aamir Avatar answered Oct 02 '22 14:10

Aamir


You must read about OPENROWSET and OPENQUERY

SELECT  * 
INTO    #tmp FROM    
OPENQUERY(YOURSERVERNAME, 'EXEC MyProc @parameters')
like image 27
Rizwan Mumtaz Avatar answered Oct 02 '22 15:10

Rizwan Mumtaz


It is not necessary use a temporary table.

This is my solution

SELECT  *  FROM    
OPENQUERY(YOURSERVERNAME, 'EXEC MyProc @parameters')
WHERE somefield = anyvalue
like image 43
DavideDM Avatar answered Oct 02 '22 14:10

DavideDM