我尝试将一个简单的嵌套循环编码为mysql块。服务器给我一个语法错误,但我找不到它。
有人看到了吗?
错误消息是: 查询:END LOOP get_invoice
错误代码:1064 您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便使用“END LOOP get_invoice”#39;在第1行
BEGIN
DECLARE v_id INT DEFAULT 0;
DECLARE v_finished1 INT DEFAULT 0;
DECLARE invoices_cursor CURSOR FOR
SELECT id_invoice FROM invoice i WHERE invoice_type = 'RE' AND EXISTS (SELECT 1 FROM invoice ma WHERE invoice_type = 'MA' AND ma.origin = i.id_invoice);
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished1 = 1;
OPEN invoices_cursor;
get_invoice: LOOP
FETCH invoices_cursor INTO v_id;
IF v_finished1 = 1 THEN
CLOSE invoices_cursor;
LEAVE get_invoice;
END IF;
DECLARE v_finished2 INT DEFAULT 0;
DECLARE v_dunning_level INT DEFAULT 1;
DECLARE dunning_cursor CURSOR FOR
SELECT id_invoice FROM invoice ma WHERE invoice_type = 'MA' AND ma.origin = v_id ORDER BY `create` ASC;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished2 = 1;
OPEN dunning_cursor;
get_dunnings: LOOP
DECLARE v_id_dun BIGINT DEFAULT NULL;
FETCH dunning_cursor INTO v_id_dun;
IF v_finished = 1 THEN
LEAVE get_dunnings;
END IF;
UPDATE invoice SET dunning_level = v_dunning_level WHERE invoice_type = 'MA' AND id_invoice = v_id_dun;
SET v_dunning_level = v_dunning_level + 1;
END LOOP get_dunnings ;
CLOSE dunning_cursor;
END LOOP get_invoice ;
END;