如何将变量传递给存储过程中的语句 - MYSQL?

时间:2018-03-21 13:17:18

标签: mysql stored-procedures

我是新的存储过程当我查询单独的查询工作正常但我使用存储过程内部它会触发错误。

这不是错误,输出应该是NULL值。没有创建rowt值,我在第二个查询中使用了条件。由于rowt为null,整个查询会触发空值。

我得到的结果是购买和其他人或无效。

DELIMITER ;;

DROP PROCEDURE IF EXISTS sp_purchase;
Create Procedure sp_purchase(
 IN start_date INT,
 IN mail varchar(10000),
 IN amount INT,
 OUT purchase INT,
 OUT expa INT,
 OUT rowt INT)

BEGIN

DECLARE a INT;

SET a = 1;

select sum(s.purchase) into purchase from company c join employee e on c.cid=e.eid left join salary s on s.eid=e.eid where FIND_IN_SET(e.mail,mail); // firs query

IF purchase >= amount THEN

select s.purchase_time, (select sum(purchase) from salary as sp join employee as se on sp.eid=se.eid where FIND_IN_SET(se.mail,mail) and se.eid <= e.eid) into expa, rowt from company c join employee e on c.cid=e.eid left join salary s on s.eid=e.eid where FIND_IN_SET(e.mail,mail) group by c.cid having rowt >= amount order by e.eid ASC limit 1; //second query

ELSEIF purchase < amount THEN

select sum(s.purchase), count(*) into purchase, expa from company c join employee e on c.cid=e.eid left join salary s on s.eid=e.eid where c.createdtime >= start_date;

ENDIF;

END

;;

1 个答案:

答案 0 :(得分:0)

在MySQL中使用子查询来解决这个问题。

select x.purchase_time into expa from (select s.purchase_time, (select sum(purchase) from salary as sp join employee as se on sp.eid=se.eid where FIND_IN_SET(se.mail,mail) and se.eid <= e.eid) as rowt from company c join employee e on c.cid=e.eid left join salary s on s.eid=e.eid where FIND_IN_SET(e.mail,mail) group by c.cid having rowt >= amount order by e.eid ASC limit 1;)x //second query