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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1629324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:21:49  来源:igfitidea点击:

MySql stored procedures: How to select from procedure table?

mysqlstored-procedures

提问by Cambiata

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 吗?或者我应该使用返回表的函数来代替吗?

采纳答案by Cambiata

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.

重新表述了这个线程中的问题:Can a stored procedure/function return a table?. 显然,不使用临时表是不可能的。

回答by Dani

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

回答by RR.

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

SQL 服务器中,您可以执行以下操作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.

否则,您可以只在存储过程中 SELECT INTO #temporary 表。