在mysql存储过程的while循环中使用cursor

ffscu2ro  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(221)

问题是,而loop没有按照我的预期工作。我期待这个结果…2018-05-01 2018-05-02 2018-05-03 2018-05-06 2018-05-07 2018-05-08 2018-05-09 2018-05-10 2018-05-13 2018-05-14 2018-05-15 2018-05-16 2018-05-17 2018-05-20 2018-05-21 2018-05-22 2018-05-23 2018-05-24 2018-05-27 2018-05-28 2018-05-29 2018-05-30 2018-05-31

BEGIN
  declare varDay date;
  DECLARE v_finished INT DEFAULT 0;
  DECLARE weekday VARCHAR(20);
  DECLARE weekend_cursor CURSOR FOR SELECT weekendDay FROM weekend;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET  v_finished=1;

  DROP TEMPORARY TABLE IF EXISTS daterange;
     CREATE TEMPORARY TABLE daterange(
     id INT NOT NULL AUTO_INCREMENT,
      day DATE,
     PRIMARY KEY(id)
      ); 

    WHILE(sdate <= enddate) DO
     OPEN weekend_cursor;
     get_weekend:LOOP
       FETCH weekend_cursor INTO weekday;
        IF v_finished =1 THEN 
         CLOSE weekend_cursor;  
          LEAVE get_weekend;

          ELSEIF DAYNAME(sdate)!=weekday THEN
           INSERT INTO daterange (day) values (sdate);
          END IF;
        END LOOP  get_weekend;

    SET sdate = DATE_ADD(sdate, INTERVAL 1 DAY);
    SET varDay = sdate;
   END WHILE;

   SELECT * FROM daterange;
   END

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题