游标内带有 INTO 的 MySQL/MariaDB 设置值的迭代不正确

Posted

技术标签:

【中文标题】游标内带有 INTO 的 MySQL/MariaDB 设置值的迭代不正确【英文标题】:MySQL/MariaDB setting values with INTO inside cursor has incorrect iterations 【发布时间】:2018-03-05 13:55:50 【问题描述】:

如果我运行这段代码,谁能解释我为什么:

    BEGIN
        -- Declare loop constructs --
        DECLARE done INT DEFAULT FALSE; 

        -- Declare Person variables --
        DECLARE s_trnsf_id INT(11) DEFAULT NULL;
    DECLARE s_trnsf_trnsp_id INT(11) DEFAULT NULL;
    DECLARE s_trnsf_bacc_id INT(11) DEFAULT NULL;
    DECLARE s_trnsf_cus_id INT(11) DEFAULT NULL;
    DECLARE s_trnsf_bacc_amnt DECIMAL(18,4) DEFAULT NULL;
    DECLARE s_trnsf_recidue_amnt DECIMAL(18,4) DEFAULT NULL;

    DECLARE t_trnsf_id INT(11) DEFAULT NULL;
    DECLARE t_trnsf_recidue_amnt DECIMAL(18,4) DEFAULT NULL;

    DECLARE c1 INT(11) DEFAULT 0;

        -- Declare Cursor --
        DECLARE member_cursor CURSOR FOR 
            SELECT trnsf_id, trnsf_trnsp_id, trnsf_bacc_id, trnsf_cus_id, trnsf_bacc_amnt,trnsf_recidue_amnt
    FROM d_transfers
    WHERE 
    trnsf_bacc_amnt < 0
    AND trnsf_recidue_amnt <> 0
    ORDER BY #trnsf_trnsp_id ASC, trnsf_bacc_id ASC, trnsf_cus_id ASC, 
    trnsf_transfer_ts ASC;

        -- Declare Continue Handler --
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

        OPEN member_cursor;

        read_loop: LOOP

    SET c1 = c1+1;

            -- Fetch data from cursor --
            FETCH member_cursor 
            INTO s_trnsf_id,
    s_trnsf_trnsp_id,
    s_trnsf_bacc_id,
    s_trnsf_cus_id,
    s_trnsf_bacc_amnt,
    s_trnsf_recidue_amnt;

            -- Exit loop if finished --
            IF done THEN
                LEAVE read_loop;
            END IF;

    ##MARKED_BEGIN######################  
    SELECT trnsf_id#,trnsf_recidue_amnt 
    into t_trnsf_id#,t_trnsf_recidue_amnt 
    from d_transfers 
    WHERE trnsf_bacc_amnt > 0
    AND trnsf_recidue_amnt > 0
    AND trnsf_trnsp_id = s_trnsf_trnsp_id
    AND trnsf_bacc_id = s_trnsf_bacc_id
    AND trnsf_cus_id = s_trnsf_cus_id 
    ORDER BY trnsf_transfer_ts ASC limit 1;
    ##MARKED_END######################  


    END LOOP read_loop;

        CLOSE member_cursor;

    SELECT c1;

    END

返回:

+-------+
| c1    |
+-------+
| 138 |
+-------+

但如果我将 MARKED_BEGIN 和 MARKED_END 之间的代码替换为代码:

SET t_trnsf_id = (
SELECT trnsf_id#,trnsf_recidue_amnt 
FROM d_transfers 
WHERE trnsf_bacc_amnt > 0
AND trnsf_recidue_amnt > 0
AND trnsf_trnsp_id = s_trnsf_trnsp_id
AND trnsf_bacc_id = s_trnsf_bacc_id
AND trnsf_cus_id = s_trnsf_cus_id 
ORDER BY trnsf_transfer_ts ASC limit 1 
); 

我明白了:

+-------+
| c1    |
+-------+
| 54742 |
+-------+

我认为它应该返回相同的结果!

如果我排除

AND trnsf_trnsp_id = s_trnsf_trnsp_id
AND trnsf_bacc_id = s_trnsf_bacc_id
AND trnsf_cus_id = s_trnsf_cus_id

从上面的代码中它返回正确的迭代(选择语句中的行数)

我在 MacOS 上使用 MariaDB 10.1.10

【问题讨论】:

如果您当前的 into-query 返回零结果,则 continue 处理程序也将被调用(因此将结束您的循环),但如果您用 set t_trnsf_id = 替换该部分则不会,这不会有into。您可能需要为此添加额外的检查,例如如果您的值不能为空,请尝试if t_trnsf_id is null then leave ... 【参考方案1】:

@Solarflare,谢谢你的提示!添加 CONTINUE HANDLER 可以解决问题。

BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = FALSE;
  BEGIN
    -- body of handler

    ##MARKED_BEGIN######################  
    SELECT trnsf_id,trnsf_recidue_amnt 
    into t_trnsf_id,t_trnsf_recidue_amnt 
    from d_transfers 
    WHERE trnsf_bacc_amnt > 0
    AND trnsf_recidue_amnt > 0
    AND trnsf_trnsp_id = s_trnsf_trnsp_id
    AND trnsf_bacc_id = s_trnsf_bacc_id
    AND trnsf_cus_id = s_trnsf_cus_id 
    ORDER BY trnsf_transfer_ts ASC limit 1;
    ##MARKED_END######################  

  END;
END;

【讨论】:

以上是关于游标内带有 INTO 的 MySQL/MariaDB 设置值的迭代不正确的主要内容,如果未能解决你的问题,请参考以下文章

PLSQL 在变量中存储具有未知数据类型的游标(使用 FETCH INTO)

使用INTO内部游标的MySQL / MariaDB设置值具有不正确的迭代

带有异步操作的猫鼬游标

INSERT INTO with CURSOR 在 TRIGGER 内

MySql数据库的存储过程能返回游标么?

在预期以下情况之一时遇到符号“INTO”