mysql 退出 sp 与 if

标签 mysql if-statement stored-procedures exit

在下面的 msql sp 中,如果一周中的某一天是星期五,我会尝试退出,但我无法让它工作,因为“END IF;”处存在语法错误。我就是看不到。

    CREATE PROCEDURE `bookFreeDay`()
    proc_label:BEGIN

  DECLARE bDone INT;
  DECLARE t1 VARCHAR(5);

  -- todo exclude sa/so
  IF DAYOFWEEK(SUBDATE(CURDATE(),1)) = 5 THEN
      LEAVE proc_label;
  END IF;

  DECLARE curs CURSOR FOR select durTime from freeDays where
    (year is not null && year= substring(CURDATE(),1,4) && start=substring(SUBDATE(CURDATE(),1), 6,5))
    || (year is null && start=substring(SUBDATE(CURDATE(),1), 6,5));

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;

  OPEN curs;

  SET bDone = 0;
  REPEAT
    FETCH curs INTO t1;

        select CONCAT(SUBDATE(CURDATE(),1), " 08:00:00") into @st;
        select addtime(@st, t1) into @en;

       insert into timeEntries
         (select
           null,
           idUsers,
           @st,
           @en,
           null,
           2
         from users
         where right(pensum,1)='%');

  UNTIL bDone END REPEAT;

  CLOSE curs;

END

最佳答案

14.6.6.2 Cursor DECLARE Syntax

...

Cursor declarations must appear before handler declarations and after variable and condition declarations.

...

...

/*
-- todo exclude sa/so
IF DAYOFWEEK(SUBDATE(CURDATE(),1)) = 5 THEN
  LEAVE proc_label;
END IF;
*/

DECLARE curs CURSOR FOR select durTime
                        from freeDays
                        where (year is not null &&
                               year= substring(CURDATE(),1,4) &&
                               start=substring(SUBDATE(CURDATE(),1), 6,5)) ||
                              (year is null &&
                               start=substring(SUBDATE(CURDATE(),1), 6,5));

DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;

-- todo exclude sa/so
IF DAYOFWEEK(SUBDATE(CURDATE(),1)) = 5 THEN
  LEAVE proc_label;
END IF;

...

关于mysql 退出 sp 与 if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41754719/

相关文章:

java - 如何通过 bcel 在 if 指令之前添加 if 指令

linux - bash 回文 grep 循环 if then else missing '

mysql - 处理 mysql sp 中的单引号和双引号

sql - 如果存在则更新行的存储过程

Mysql用日期条件对多个列值求和

python - 在 MySQL 数据库中存储和查询大量变长列表的最佳方法是什么?

mysql - 对传递的值求和,并从接收到的同名值中减去 mysql

mysql - 在 SQL select 语句中,如何计算一行的位置?

java - java中的ArrayList错误(无法使两个数字相等)

mysql - 如何创建触发器自引用触发器......是的