简体   繁体   中英

MySQL stored procedure if statement not working

I have the following stored procedure in MySQL

CREATE DEFINER=`test_db`@`%` PROCEDURE `ADD_ATTENDANCE`(IN `programID` INT, IN `clientID` INT, IN `insDate` DATETIME, IN `updDate` DATETIME, IN `insUser` INT, IN `updUser` INT, IN `lessonDate` DATE, IN `lessonTime` TIME)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'Add attedance to my calendar'

BEGIN
    DECLARE max_sub, availability INT;
    DECLARE cursor_max_sub CURSOR FOR SELECT max_sub FROM app_lesson WHERE id = programID;
    DECLARE cursor_availability CURSOR FOR SELECT count(*) FROM attendance WHERE program_id = programID AND lesson_date = lessonDate AND lesson_time = lessonTime;
    OPEN cursor_max_sub;
    OPEN cursor_availability;

    read_loop: LOOP
        FETCH cursor_max_sub INTO max_sub;
        FETCH cursor_availability INTO availability;
        IF (availability < max_sub) THEN
            insert into attendance (program_id, client_id, ins_date, upd_date, ins_user, upd_user, lesson_date, lesson_time) 
            values (programID, clientID, insDate, updDate, insUser, updUser, lessonDate, lessonTime);
            LEAVE read_loop;
        ELSE
            insert into attendance_hold (program_id, client_id, ins_date, upd_date, ins_user, upd_user, lesson_date, lesson_time) 
            values (programID, clientID, insDate, updDate, insUser, updUser, lessonDate, lessonTime);
        END IF;
    END LOOP;
    CLOSE cursor_max_sub;
    CLOSE cursor_availability;
END;

Even though the cursor_max_sub is equal to 6 and the cursor_availability is equal to 4 my procedure always executes the else insert statement. Can you please help me out?

Thanks!!!

好的,这很棘手。由于某些原因,当我将max_sub变量更改为maxNumberOfSubscription一切都正常运行了… max_submax_sub某种保留关键字,还是复杂化,因为我的变量与返回的字段具有相同的名称选择语句?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM