MySql存储过程:如何从过程表中选择?

时间:2021-09-14 16:40:29

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?

但没有成功。)我应该在程序中使用SELECT ... INTO outVariable吗?或者我应该使用返回表格的函数吗?

3 个解决方案

#1


5  

Reformulated the question in this thread: Can a stored procedure/function return a table?. Obviously, it isn't possible without the use for temp tables.

在这个帖子中重新提出了问题:存储过程/函数可以返回一个表吗?显然,没有临时表的使用是不可能的。

#2


2  

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

#3


-2  

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

在SQL Server中,您可以执行SELECT * FROM database.getExamples()

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

如果你想重新使用'过程'那么,是的,我会把它放到一个表值函数中。

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

否则你只需要在存储过程中选择一个#temporary表。

#1


5  

Reformulated the question in this thread: Can a stored procedure/function return a table?. Obviously, it isn't possible without the use for temp tables.

在这个帖子中重新提出了问题:存储过程/函数可以返回一个表吗?显然,没有临时表的使用是不可能的。

#2


2  

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

#3


-2  

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

在SQL Server中,您可以执行SELECT * FROM database.getExamples()

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

如果你想重新使用'过程'那么,是的,我会把它放到一个表值函数中。

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

否则你只需要在存储过程中选择一个#temporary表。