我正在创建一个过程,该过程在循环中调用另一个过程。另一个过程也有一些条件定义是否在循环中调用子过程。我有一个子过程的问题不在循环中调用。但是主要过程在循环中工作正常。
主要步骤
CREATE PROCEDURE `sale_batch`(IN invno INT)
BEGIN
/* Varibales */
declare itemcode int;
declare inv_no int;
declare sdate datetime;
declare itemrate double;
declare qty double;
declare c_code double;
declare finished integer default 0;
/* Result set */
DECLARE sale_inv CURSOR FOR SELECT salesdtl.itemcode,sales.inv_no,sales.date,salesdtl.itemrate,salesdtl.totalno,sales.customers_code FROM sales Join salesdtl on (sales.inv_no=salesdtl.inv_no) WHERE sales.inv_no=invno;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN sale_inv;
get_records: LOOP
FETCH sale_inv INTO itemcode,inv_no,sdate,itemrate,qty,c_code;
IF finished = 1 THEN
CLOSE sale_inv;
LEAVE get_records;
ELSE
CALL sale_batch_h(itemcode,qty,inv_no,sdate,itemrate,c_code);
END IF;
END LOOP;
END
子过程
CREATE `sale_batch_h`(in stockcode INT,in qty double,in invno int,in sdate datetime,in itemrate double,in c_code int)
BEGIN
/* Varibales */
declare grn_no int;
declare prate double;
declare balqty double;
declare sold double;
declare newsold double;
declare finished integer default 0;
/* Result set */
DECLARE sale_inv_h CURSOR FOR SELECT pinvoiceno,itemrate,(itemqty-soldqty) as balqty,soldqty FROM stockbatch WHERE itemcode=stockcode and (itemqty-soldqty)>0 ORDER BY date LIMIT 0,1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN sale_inv_h;
FETCH sale_inv_h INTO grn_no,prate,balqty,sold;
IF finished = 1 THEN
CLOSE sale_inv_h;
ELSE
IF balqty >= qty THEN
INSERT INTO stockbatch_details (pinvoiceno,sinvoiceno,sdate,sqty,srate,ccode,prate,itemcode) values (grn_no,invno,sdate,qty,itemrate,c_code,prate,stockcode);
SET newsold = sold+qty;
UPDATE stockbatch SET SoldQty=newsold where pinvoiceno=grn_no;
INSERT INTO `logs`(command) values ('Single');
ELSE
SET newsold = sold+balqty;
INSERT INTO stockbatch_details (pinvoiceno,sinvoiceno,sdate,sqty,srate,ccode,prate,itemcode) values (grn_no,invno,sdate,qty,itemrate,c_code,prate,stockcode);
UPDATE stockbatch SET SoldQty=newsold where pinvoiceno=grn_no;
SET qty = qty-balqty;
INSERT INTO `logs`(command) values ('Mutliple Calls');
CALL sale_batch_h(stockcode,qty,invno,sdate,itemrate,c_code);
END IF;
END IF;
END