MySQL 如何在mysql中使用select语句调用存储过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32118033/
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 21:14:18  来源:igfitidea点击:

How to call a stored procedure using select statement in mysql

mysqlselectstored-procedures

提问by vineeth

I have call statement like

我有像这样的调用语句

 CALL report_procedure
('2013-02-01',now(),'2015-01-01','1');

and i want to use it in a select query. i have tried like

我想在选择查询中使用它。我试过

Select * from ( CALL report_procedure
    ('2013-02-01',now(),'2015-01-01','1'));

but error occurs. like

但发生错误。喜欢

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ( CALL report_procedure ('2013-02-01',now(),'2015-01-01','1') at line 3 0.297 sec

错误代码:1064。您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 3 行 0.297 秒附近使用的正确语法( CALL report_procedure ('2013-02-01',now(),'2015-01-01','1')

Can anyone suggest me a method to call stored procedure in Select statement in mysql??

谁能建议我在 mysql 的 Select 语句中调用存储过程的方法?

回答by Devart

It is not possible to use result set from procedure in FROM clause. MySQL does not allow doing this.

不能在 FROM 子句中使用过程的结果集。MySQL 不允许这样做。

You may populate another table (or temporary table) in your procedure, and after, use that table in SELECT commands -

您可以在过程中填充另一个表(或临时表),然后在 SELECT 命令中使用该表 -

CALL report_procedure ('2013-02-01',now(),'2015-01-01','1'); -- fill temp_table
SELECT * FROM temp_table;

回答by latsi

--Firstly your store procedure should look something like this:

--首先你的存储过程应该是这样的:

CREATE PROCEDURE report_procedure(
IN d1 DATE,
dnow DATE,
d2 DATE,
val INT
) 
BEGIN SELECT * 
FROM yourtablename
WHERE date1 = d1
AND datenow > dnow
AND date2 > d2
AND value = val;

END
--The store procedure contains the select statement.

-- then you can call the store procedure like that:
 CALL report_procedure('2013-02-01',now(),'2015-01-01','1');

--hope it helps