MySql嵌套存储过程调用

时间:2013-11-23 15:06:34

标签: mysql stored-procedures syntax

我在MySQL中有2个存储过程。我想从另一个调用一个,但我需要一些语法

的帮助

这是程序1 FindPreviousDate

DELIMITER $$ CREATE PROCEDURE `FindPreviousDate`(IN eventdate DATETIME, IN lookbackDays INT, IN symbol VARCHAR(20))
BEGIN
SELECT *
FROM Price a
WHERE a.eventDate between (eventDate - interval lookbackDays day) and (eventdate - interval 1 day) and a.symbol = symbol
ORDER BY a.eventDate DESC
LIMIT 1;
END

这是程序2 FindCloseEvent

CREATE PROCEDURE `FindCloseEvent`(IN startdate DATETIME, IN enddate DATETIME,IN symbol VARCHAR(20), IN cutoff DOUBLE)
BEGIN
SELECT *
FROM Price a
WHERE a.eventDate between startdate and enddate and
(SELECT COUNT(*) from Price b where b.eventDate = a.eventDate and b.closePrice < cutoff and a.symbol = b.symbol and
(SELECT COUNT(*) from Price c where c.eventDate = (b.eventDate - interval 1 day) and c.closePrice >= cutoff and b.symbol = c.symbol));
END

我想用过程1的结果替换过程2中的这段代码

(b.eventDate - interval 1 day)

我需要帮助才能正确理解语法。我甚至不确定mySql是否允许我要求的内容。

谢谢!

1 个答案:

答案 0 :(得分:0)

在程序上,您可以使用OUT变量,这些变量将被填充并在过程外可用。 OR
由于MySQL过程不返回值。您需要存储函数才能实现此目的。

使用下面链接中给出的示例来调用procedure2中的function1 http://www.java2s.com/Code/SQL/Procedure-Function/Callanotherfunction.htm

你甚至可以将function1的结果存储到procedure2的某个变量中,稍后在查询

中使用它