所有表都不能执行INSERT或UPDATE

时间:2017-03-09 20:08:47

标签: mysql stored-procedures innodb mariadb

我使用了一个存储过程,它使用游标循环并在第一次调用该过程后在Mariadb 10.1数据库上处理出勤数据表,数据库上的所有表都失去了执行INSERT INTO或UPDATE语句的能力,除非目标表首先被截断,任何人都可以告诉我出了什么问题以及如何修复它 导致问题的程序:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `settle_attendance`()
    MODIFIES SQL DATA
BEGIN
DECLARE trans_done BOOLEAN DEFAULT FALSE;
DECLARE punchid BIGINT(20);
DECLARE timein DATETIME;
DECLARE utctimein DATETIME;
DECLARE timeout DATETIME;
DECLARE utctimeout DATETIME;
DECLARE inday DATE;
DECLARE outday DATE;
DECLARE todaysdate DATE;

DECLARE attendcur CURSOR FOR
     SELECT id, punch_in_utc_time, punch_in_user_time,
                punch_out_utc_time, punch_out_user_time
         FROM ohrm_attendance_record
         ORDER BY id ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET trans_done = TRUE;

OPEN attendcur;
edit_loop: LOOP
SET todaysdate = CURRENT_DATE();
FETCH attendcur INTO punchid, utctimein, timein, utctimeout, timeout;

IF trans_done THEN
        CLOSE attendcur;
        LEAVE edit_loop;
END IF;

SET inday = DATE(timein);
SET outday = DATE(timeout);
SET todaysdate = CURRENT_DATE();


IF (inday < todaysdate) OR (outday < todaysdate) THEN
    CASE 
        WHEN (timein IS NULL OR timein = '')
          OR (utctimein IS NULL OR utctimein = '') THEN
            UPDATE ohrm_attendance_record
               SET punch_in_utc_time = utctimeout,
                   punch_in_user_time = timeout,
                   state = 'PUNCHED OUT'
               WHERE punchid = id;
        ELSE BEGIN END;
    END CASE;

    CASE 
        WHEN (timeout IS NULL OR timeout = '')
          OR (utctimeout IS NULL OR utctimeout = '') THEN
            UPDATE ohrm_attendance_record
               SET punch_out_utc_time = utctimein,
                   punch_out_user_time = timein,
                   state = 'PUNCHED OUT'
               WHERE punchid = id;
        ELSE BEGIN END;
    END CASE;
END IF;

END LOOP edit_loop;
END $$
DELIMITER ;

1 个答案:

答案 0 :(得分:0)

我选择避免你问的问题。相反,让我们通过摆脱讨厌的CURSOR来尝试以10倍的速度执行查询。整个存储过程可以在2 UPDATEs没有循环中完成:

UPDATE  ohrm_attendance_record
    SET punch_in_utc_time = utctimeout,
        punch_in_user_time = timeout,
        state = 'PUNCHED OUT'
    WHERE  ( timein < CURDATE()  OR  timeout < CURDATE() )
      AND  ( ( timein IS NULL     OR  timein = '' )
          OR ( utctimein IS NULL  OR  utctimein = '' )
           );

UPDATE  ohrm_attendance_record
    SET punch_out_utc_time = utctimein,
        punch_out_user_time = timein,
        state = 'PUNCHED OUT'
    WHERE  ( timein < CURDATE()  OR  timeout < CURDATE() )
      AND  ( ( timeout IS NULL    OR  timeout = '' )
          OR ( utctimeout IS NULL OR  utctimeout = '' )
           );

但是,我对您对timeintimeout的测试持怀疑态度。

如果您在NULL''找到错失的时间,则查询会更容易阅读。

如果仅在TIMESTAMP中存储UTC值,则可以让用户的时区负责转换到当地时间 - 这将消除相当多的列并简化UPDATEs

我会抓住这个问题...... SHOW CREATE PROCEDURE settle_attendance;,你可能发现CHARACTER SETCOLLATION与你的内容不一致认为应该是。