我有以下存储过程:
DELIMITER $$
USE `my_database`$$
DROP PROCEDURE IF EXISTS `integrateBilling`$$
CREATE DEFINER=`user`@`server` PROCEDURE `integrateBilling`(IN bill_run_date DATE, IN bill_run_number INT(11))
BLOCK1:BEGIN
DECLARE LOOP1_EOF, LOOP2_EOF BOOLEAN DEFAULT FALSE;
DECLARE billing_profile_id VARCHAR(36);
DECLARE billing_profile_name VARCHAR(254);
DECLARE billing_profile_date_entered VARCHAR(254);
DECLARE billing_profile_number VARCHAR(254);
DECLARE next_bill_date DATE;
DECLARE memberid VARCHAR(36);
DECLARE account_number VARCHAR(36);
DECLARE date_entered DATETIME;
DECLARE date_modified DATETIME;
DECLARE modified_user_id VARCHAR(36);
DECLARE created_by VARCHAR(36);
DECLARE sleep1 INT(11);
DECLARE linkguid VARCHAR(36);
DECLARE sequence VARCHAR(254);
DECLARE price_list VARCHAR(254);
DECLARE CUR1 CURSOR FOR
SELECT DISTINCT
ap.id AS billing_profile_id,
ap.name AS billing_profile_name,
DATE(ap.date_entered) AS billing_profile_date_entered,
apc.billing_profile_number_c AS billing_profile_number,
apc.next_bill_date_c AS next_bill_date,
a.id AS memberid,
ac.account_number_c AS account_number,
ap.date_entered AS date_entered,
ap.date_modified AS date_modified,
ap.modified_user_id AS modified_user_id,
ap.created_by AS created_by,
SLEEP(0.2) AS sleep1,
UUID() AS linkguid,
CONCAT("INV_", getInvoiceNumber()) AS sequence
FROM
apsop_profile ap
JOIN
apsop_profile_cstm apc
ON ap.id = apc.id_c
JOIN
accounts_apsop_profile_1_c aap1c
ON aap1c.accounts_apsop_profile_1apsop_profile_idb = ap.id
JOIN
accounts a
ON aap1c.accounts_apsop_profile_1accounts_ida = a.id
JOIN
accounts_cstm ac
ON ac.id_c = a.id
WHERE
DATE(apc.next_bill_date_c) <= bill_run_date
AND
a.deleted = 0
AND
ac.memberstatus_c = "Active"
AND
apc.status_c = 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET LOOP1_EOF = TRUE;
OPEN CUR1;
LOOP1: LOOP
FETCH CUR1 INTO
billing_profile_id,
billing_profile_name,
billing_profile_date_entered,
billing_profile_number,
next_bill_date,
memberid,
account_number,
date_entered,
date_modified,
modified_user_id,
created_by,
sleep1,
linkguid,
sequence;
-- INSERT INVOICE HEADER
INSERT INTO apsob_apso_member_invoices(id, `name`, date_entered, date_modified, modified_user_id, created_by) VALUES (billing_profile_id, bill_run_date, NOW(), NOW(), modified_user_id, created_by);
INSERT INTO apsob_apso_member_invoices_cstm(id_c, apso_billing_id_c, invoice_run_date_c, billrun_id_c, document_number_c) VALUES (billing_profile_id, account_number, next_bill_date, bill_run_number, sequence);
INSERT INTO accounts_apsob_apso_member_invoices_1_c (id, accounts_apsob_apso_member_invoices_1accounts_ida, accounts_a684envoices_idb) VALUES (linkguid, memberid, billing_profile_id);
UPDATE apsop_profile_cstm SET next_bill_date_c = DATE_ADD(next_bill_date, INTERVAL 1 YEAR) WHERE id_c = billing_profile_id;
IF LOOP1_EOF THEN
LEAVE LOOP1;
END IF;
BLOCK2:BEGIN
-- NOW SELECT AND INSERT DETAIL ITEMS
DECLARE detailguid VARCHAR(36);
DECLARE sleep2 INT(11);
DECLARE invoicelinkguid VARCHAR(36);
DECLARE SLEEP3 INT(11);
DECLARE line_item_price INT(11);
DECLARE line_item_id VARCHAR(254);
DECLARE price_list VARCHAR(254);
DECLARE price_list_name VARCHAR(254);
DECLARE line_item_name VARCHAR(254);
DECLARE line_item_description TEXT;
DECLARE pastel_gl_number VARCHAR(36);
DECLARE line_item_number VARCHAR(36);
DECLARE quantity INT(11);
DECLARE invoice_date VARCHAR(36);
DECLARE invoice_total INT(11);
DECLARE CUR2 CURSOR FOR
SELECT DISTINCT
UUID() AS detailguid,
SLEEP(0.2) AS sleep2,
UUID() AS invoicelinkguid,
SLEEP(0.2) AS sleep3,
baplic.price_c AS line_item_price,
aambc.line_item_c AS line_item_id,
aambc.price_list_c AS price_list,
bapl.name AS price_list_name,
bali.name AS line_item_name,
bali.description AS line_item_description,
bali.pastel_gl_number,
bali.line_item_number,
aambc.quantity_c AS quantity,
DATE_FORMAT(DATE(NOW()), '%d-%m-%Y') AS invoice_date
FROM
accounts a
JOIN
accounts_cstm ac
ON a.id= ac.id_c
JOIN
accounts_apsop_profile_1_c aap1c
ON a.id = aap1c.accounts_apsop_profile_1accounts_ida
JOIN
apsop_profile ap
ON ap.id = aap1c.accounts_apsop_profile_1apsop_profile_idb
JOIN
apsop_profile_cstm apc
ON ap.id = apc.id_c
JOIN
apsop_profile_apsob_apso_member_billing_1_c apaamb1c
ON apaamb1c.apsop_profile_apsob_apso_member_billing_1apsop_profile_ida = ap.id
JOIN
apsob_apso_member_billing aamb
ON apaamb1c.apsop_prof8828billing_idb = aamb.id
JOIN
apsob_apso_member_billing_cstm aambc
ON aamb.id = aambc.id_c
JOIN bills_apso_price_lists bapl
ON aambc.price_list_c = bapl.id
JOIN
bills_apso_price_list_items bapli
ON aambc.line_item_c = bapli.id
JOIN
bills_apso_price_list_items_cstm baplic
ON bapli.id = baplic.id_c
JOIN
bills_apso_line_items bali
ON baplic.bills_apso_line_items_id_c = bali.id
WHERE
bapl.deleted = 0
AND
bali.deleted = 0
AND
DATE(apc.next_bill_date_c) <= bill_run_date;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET LOOP2_EOF = TRUE;
OPEN CUR2;
LOOP2: LOOP
FETCH CUR2 INTO
detailguid,
sleep2,
invoicelinkguid,
sleep3,
line_item_price,
line_item_id,
price_list,
price_list_name,
line_item_name,
line_item_description,
pastel_gl_number,
line_item_number,
quantity,
invoice_date;
-- INSERT INVOICE DETAILS
INSERT INTO apsob_apso_member_billing_invoice_items (id, NAME, date_entered, date_modified, description)
VALUES (detailguid, line_item_name, NOW(), NOW(), line_item_description);
INSERT INTO apsob_apso_member_billing_invoice_items_cstm (id_c, line_item_number_c, line_item_id_c, pastel_gl_no_c, quantity_c, price_c, invoice_number_c, invoice_id_c, price_list_id_c, price_list_description_c, price_list_item_id_c)
VALUES (detailguid, line_item_number, line_item_id, pastel_gl_number, quantity, line_item_price, sequence, billing_profile_id, price_list, price_list_name, line_item_id);
INSERT INTO apsob_apso05e0e_items_1_c (id, date_modified, apsob_apso3cd5nvoices_ida, apsob_apsof0c4e_items_idb)
VALUES (invoicelinkguid, NOW(), billing_profile_id, detailguid);
-- UPDATE TOTAL FOR INVOICE HEADER
UPDATE apsob_apso_member_invoices_cstm SET invoice_total_c = (SELECT SUM(price_c) AS invoice_total FROM apsob_apso_member_billing_invoice_items_cstm WHERE id_c = detailguid) WHERE id_c = billing_profile_id;
IF LOOP2_EOF THEN CLOSE CUR1;
LEAVE LOOP2;
END IF;
END LOOP LOOP2;
CLOSE CUR2;
END BLOCK2;
END LOOP LOOP1;
CLOSE CUR1;
END BLOCK1$$
DELIMITER ;
问题是,当我在SQL客户端中运行第一个查询(针对cur1)时,我得到了预期的两行。
但是当它应该插入两行时,它仅将一行插入到apsob_apso_member_invoices中,这使我相信它要么过早退出循环,要么根本不正确。另外,对于插入发票明细的cur2,它仅插入第一个循环的明细行。有人可以在这里发现我在做什么错吗?