views:

160

answers:

3

Hi!

Let's say we have a stored procedure selecting something from a table:

CREATE PROCEDURE database.getExamples() 
SELECT * FROM examples;

How can I use the result from this procedure in a later select? (I've tried

SELECT * FROM (CALL database.getExamples())

but with no success.) Should I use SELECT... INTO outVariable in the procedure? Or should I use a function returning the table instead?

A: 

If you want to re-use the 'procedure' then, yes, I would put it into a table valued function.

In SQL server you can then do SELECT * FROM database.getExamples()

Otherwise you could just SELECT INTO a #temporary table inside the stored procedure.

RR
Hi RR! I'm trying this, but obviously the data-type "TABLE" isn't valid:CREATE PROCEDURE database.getExamples( OUT examplesTable TABLE /* <<< What datatype to use for a table? */ ) SELECT * FROM examples INTO examplesTable
Cambiata
A: 
CREATE TABLE #TempTable
(OID int IDENTITY (1,1),
VAr1 varchar(128) NOT NULL,
VAr2 varchar(128) NOT NULL)

Populate temporary table

INSERT INTO #TempTable(VAr1 , VAr2 )
SELECT * FROM examples
Dani
of-course change the structure to you needs....
Dani
Thank you Dani!This means that I have to rebuild the entire table every time?That seems like lots of extra work to me. Just running CALL getExamples() obviously gives the data I need - the question is how to access it in a select statement...
Cambiata
This is SQL Server code. in mysql - you can create temporary table use it, and then drop it.
Dani
From performance aspect, and I'm not a pro, i think it will be almost the same as using it in a nested statement, there probably some optimization possibilities in the nested option, but unless you are dealing with very large/complicated select you won't notice it.
Dani
Oh, now I understand what you want to do. I guess it is possible, need to try it myself though.
Dani
A: 

Reformulated the question in this thread: http://stackoverflow.com/questions/1637288/mysql-can-a-stored-procedure-function-return-a-table. Obviously, it isn't possible without the use for temp tables.

Cambiata