MySQL存储过程,FETCH变量数量不正确

时间:2018-06-12 12:53:06

标签: mysql stored-procedures

我试图在MySQL存储过程的帮助下计算两个日期之间约会的总和,但我得到一个错误,但我无法弄清楚原因。如果最后两个参数为0,则它​​们不包含在查询的WHERE子句中。

我正在获取两个参数,但是我收到以下错误:

错误代码:1328 FETCH变量数不正确

有人可以帮助我吗?

我的代码:

DELIMITER $$

DROP PROCEDURE IF EXISTS `cli_users_schedule_appointments_get_in_minutes`$$

CREATE PROCEDURE `cli_users_schedule_appointments_get_in_minutes`(
    IN P_sys_clientid INT,
    IN P_cli_workstationid INT,
    IN P_cli_userid INT,
    IN P_date_from DATE,
    IN P_date_to DATE,
    IN P_status INT,
    IN P_interv_type INT,
    OUT p_minutes_inside TIME,
    OUT p_minutes_outside TIME
) MODIFIES SQL DATA
    DETERMINISTIC
BEGIN

    DECLARE VAR_minutes_inside,VAR_minutes_outside TIME DEFAULT '00:00:00';
    DECLARE VAR_i INT(11) DEFAULT 0;
    DECLARE VAR_done INT(1) DEFAULT 0;
    DECLARE VAR_from,VAR_to DATETIME;
    DECLARE VAR_where VARCHAR(500) DEFAULT "";
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET VAR_done = 1;

    IF (P_status>0) THEN
        SET VAR_where = CONCAT(VAR_where," AND status=",P_status);
    END IF;
    IF (P_interv_type>0) THEN
        SET VAR_where = CONCAT(VAR_where," AND intervention_type=",P_interv_type);
    END IF;

    SET @app_query = CONCAT('SELECT `date_from`,`date_to` FROM cli_users_appointments WHERE sys_clientid=',P_sys_clientid,
                            ' AND cli_workstationid=',P_cli_workstationid,' AND cli_userid=',P_cli_userid,' AND DATE(date_from)>="',
                            DATE(P_date_from),'" AND DATE(date_to)<="',DATE(P_date_to),'" AND appointment_type=1 ', VAR_where);
    helper_cursor:BEGIN
        DECLARE app_cursor CURSOR FOR SELECT @app_query; 
        OPEN app_cursor;
            app_cursor_loop: LOOP
                SET VAR_done = 0;
                SET VAR_i = VAR_i + 1;
                FETCH app_cursor INTO VAR_from,VAR_to;
                IF (VAR_done = 1 OR VAR_i>3650) THEN LEAVE app_cursor_loop; END IF;
                /* I WILL DO THE MAGIC HERE */
            END LOOP app_cursor_loop;
        CLOSE app_cursor;
    END helper_cursor;

    SET p_minutes_inside    = VAR_minutes_inside;
    SET p_minutes_outside   = VAR_minutes_outside; 

END$$

DELIMITER ;

调用存储过程:

CALL cli_users_schedule_appointments_get_in_minutes('1','1','2','2018-06-01','2018-06-30',0,0,@P_minutes_inside,@P_minutes_outside);

SELECT @P_minutes_inside AS minutes_inside,@P_minutes_outside AS minutes_outside;

0 个答案:

没有答案