mysql 游标的嵌套使用示例

时间:2022-09-08 12:07:34

 

 

BEGIN
DECLARE trainee_no_value BIGINT (20); -- 学员编号
DECLARE pay_no_value BIGINT (20); -- 交费序号
DECLARE deposit_value BIGINT (20); -- 预定金
DECLARE pay_type_value CHAR (2); -- 交费性质


DECLARE done INT DEFAULT FALSE;

DECLARE startSynctime datetime DEFAULT NOW();
-- 异常信息变量
DECLARE eerrorcode CHAR(5);
DECLARE msg TEXT;

DECLARE trainee_no_cur CURSOR FOR
SELECT TRAINEE_NO,PAY_NO,DEPOSIT,PAY_TYPE FROM trainee_pay_info
WHERE
DELETE_KBN = '0'
AND PAY_AMOUNT > 0
AND PAY_KBN = '02'
AND DEPOSIT > 0
AND TRUE_FINANCE_TIME IS NOT NULL
AND PAY_TYPE IN ('01','03','04')
ORDER BY TRAINEE_NO ASC,PAY_NO DESC;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- 异常检测和信息的存储
DECLARE EXIT HANDLER FOR SQLEXCEPTION

BEGIN
ROLLBACK;

GET DIAGNOSTICS CONDITION 1 eerrorcode = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;
INSERT INTO renshi.syncrecord_history(record_result,record_time,record_type,start_time)
VALUES(CONCAT('更新预定金学费序号发生了错误。错误原因是:',msg,'。','错误代号为:',eerrorcode),NOW(),'12',startSynctime);
END;

-- 打开事务
START TRANSACTION;

-- 循环游标
-- 打开游标
OPEN trainee_no_cur;
DELETE FROM trainee_pay_info1;
-- 声明结束的时候
myLoop: LOOP

FETCH trainee_no_cur INTO trainee_no_value,pay_no_value,deposit_value,pay_type_value;
IF done THEN
INSERT INTO renshi.syncrecord_history(record_result,record_time,record_type,start_time)
VALUES('更新预定金学费序号成功',NOW(),'12',startSynctime);
LEAVE myLoop;
END IF;
IF pay_type_value <> '01' THEN
BEGIN
DECLARE pay_no_value_02 BIGINT (20);-- 交费序号
DECLARE pay_amount_value_02 BIGINT (20); -- 交费金额
DECLARE trainee_no_cur_02 CURSOR FOR
SELECT MAX(PAY_NO) AS PAY_NO,PAY_AMOUNT FROM trainee_pay_info
WHERE
DELETE_KBN = '0'
AND PAY_KBN = '01'
AND HANDIN_KBN = '03'
AND TRAINEE_NO = trainee_no_value
AND PAY_NO < pay_no_value
AND HANDIN_STYLE_KBN = '02'
AND HANDIN_PAY_NO IS NULL;
OPEN trainee_no_cur_02;
FETCH trainee_no_cur_02 INTO pay_no_value_02,pay_amount_value_02;
IF pay_amount_value_02 = deposit_value THEN
UPDATE trainee_pay_info
SET trainee_pay_info.HANDIN_PAY_NO = pay_no_value,trainee_pay_info.UPDATE_TIME = NOW()
WHERE TRAINEE_NO = trainee_no_value AND PAY_NO = pay_no_value_02;
ELSE
INSERT INTO renshi.syncrecord_history
SELECT TRAINEE_NO,PAY_NO,DELETE_KBN,CREATE_TIME,UPDATE_TIME FROM trainee_pay_info
WHERE TRAINEE_NO = trainee_no_value AND PAY_NO = pay_no_value;
END IF;
CLOSE trainee_no_cur_02;
END;
ELSE
BEGIN
DECLARE pay_no_value_01 BIGINT (20);-- 交费序号
DECLARE pay_amount_value_01 BIGINT (20); -- 交费金额
DECLARE trainee_no_cur_01 CURSOR FOR
SELECT MAX(PAY_NO) AS PAY_NO,PAY_AMOUNT FROM trainee_pay_info
WHERE
DELETE_KBN = '0'
AND PAY_KBN = '01'
AND HANDIN_KBN = '03'
AND TRAINEE_NO = trainee_no_value
AND PAY_NO < pay_no_value
AND HANDIN_STYLE_KBN = '01'
AND HANDIN_PAY_NO IS NULL;
OPEN trainee_no_cur_01;
FETCH trainee_no_cur_01 INTO pay_no_value_01,pay_amount_value_01;
IF pay_amount_value_01 = deposit_value THEN
UPDATE trainee_pay_info
SET trainee_pay_info.HANDIN_PAY_NO = pay_no_value,trainee_pay_info.UPDATE_TIME = NOW()
WHERE TRAINEE_NO = trainee_no_value AND PAY_NO = pay_no_value_01;
ELSE
INSERT INTO trainee_pay_info1
SELECT TRAINEE_NO,PAY_NO,DELETE_KBN,CREATE_TIME,UPDATE_TIME FROM trainee_pay_info
WHERE TRAINEE_NO = trainee_no_value AND PAY_NO = pay_no_value;
END IF;
CLOSE trainee_no_cur_01;
END;
END IF;
END LOOP;
CLOSE trainee_no_cur;
COMMIT;
END