繁体   English   中英

MySQL-在存储过程中的游标中使用SELECT INTO语句

[英]MySQL - Using SELECT INTO statement inside cursor in stored procedure

我正在使用游标在记录上迭代在MySQL数据库中编写存储过程。 我可以在FETCH语句中指定的变量中提取记录,但是当我在游标中使用SELECT时,我无法将结果存储在变量中。 我知道它的可能,但找不到为什么它不能在下面的MySQL代码中工作。

CREATE DEFINER=`root`@`localhost` PROCEDURE `add_monthly_leave_entitlement`
(IN `emp` INT(10), OUT `experience` INT(10), OUT `entitlement` DECIMAL(10,4)) DETERMINISTIC
BEGIN 

DECLARE emp_no INT(10);
DECLARE current_month_exp INT(10);
DECLARE previous_month_end_exp INT(10);
DECLARE emp_status INT(10);

DECLARE done INT DEFAULT FALSE;
DECLARE monthly_entitlement decimal(8,4) DEFAULT 0;

DECLARE emp_cursor CURSOR FOR
SELECT e.`emp_number` AS emp_no, 
TIMESTAMPDIFF( YEAR, e.`joined_date` , NOW( ) ) AS month_start_exp, 
TIMESTAMPDIFF( YEAR, e.`joined_date`, LAST_DAY(NOW()- INTERVAL 1 MONTH) ) AS last_month_end_exp, e.`emp_status` AS emp_status 
FROM `hs_hr_employee` AS e 
WHERE e.`termination_id` is null AND e.`emp_number`=emp;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN emp_cursor;

read_loop : LOOP
FETCH emp_cursor INTO emp_no, current_month_exp, previous_month_end_exp, emp_status;
    IF done THEN
        LEAVE read_loop;
    END IF;

    /*Monthly entitlement as per experience*/       
    SELECT `monthly_entitlement` INTO monthly_entitlement 
    FROM `ohrm_leave_entitlement_conf` 
    WHERE `year_completion` = IF(previous_month_end_exp >4 , 5, previous_month_end_exp) 
    AND `leave_type_id` = 4;

   SET experience = previous_month_end_exp;
   SET entitlement = monthly_entitlement;

END LOOP;

CLOSE emp_cursor;
END

无法在monthly_entitlement权利中获取值,该值始终为0.0000 我试图在单独的过程中运行每月的权利查询,它返回正确的值。 我有匹配的数据类型的表列和变量。

有人可以帮助我了解这里的问题吗?

避免将局部变量命名为列名,请参见第2章对存储程序的限制::在存储例程中的名称冲突 尝试更改以下内容:

...
-- DECLARE monthly_entitlement decimal(8,4) DEFAULT 0;
DECLARE _monthly_entitlement decimal(8,4) DEFAULT 0;
...
/*Monthly entitlement as per experience*/
-- SELECT `monthly_entitlement` INTO monthly_entitlement
SELECT `monthly_entitlement` INTO _monthly_entitlement
...
-- SET entitlement = monthly_entitlement;
SET entitlement = _monthly_entitlement;
...

正如其他人所建议的那样,局部变量的名称和列名称应该不同,因此这里的更改

CREATE DEFINER=`root`@`localhost` PROCEDURE `add_monthly_leave_entitlement`
(IN `emp` INT(10), OUT `experience` INT(10), OUT `entitlement` DECIMAL(10,4)) DETERMINISTIC
BEGIN 

DECLARE v_emp_no INT(10);
DECLARE v_current_month_exp INT(10);
DECLARE v_previous_month_end_exp INT(10);
DECLARE v_emp_status INT(10);

DECLARE done INT DEFAULT FALSE;
DECLARE v_monthly_entitlement decimal(8,4) DEFAULT 0;

DECLARE emp_cursor CURSOR FOR
SELECT e.`emp_number` AS emp_no, 
TIMESTAMPDIFF( YEAR, e.`joined_date` , NOW( ) ) AS month_start_exp, 
TIMESTAMPDIFF( YEAR, e.`joined_date`, LAST_DAY(NOW()- INTERVAL 1 MONTH) ) AS last_month_end_exp, e.`emp_status` AS emp_status 
FROM `hs_hr_employee` AS e 
WHERE e.`termination_id` is null AND e.`emp_number`=emp;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN emp_cursor;

read_loop : LOOP
FETCH emp_cursor INTO v_emp_no, v_current_month_exp, v_previous_month_end_exp, v_emp_status;
    IF done THEN
        LEAVE read_loop;
    END IF;

    /*Monthly entitlement as per experience*/       
    SELECT `monthly_entitlement` INTO v_monthly_entitlement 
    FROM `ohrm_leave_entitlement_conf` 
    WHERE `year_completion` = IF(v_previous_month_end_exp >4 , 5, v_previous_month_end_exp) 
    AND `leave_type_id` = 4;

   SET experience = v_previous_month_end_exp;
   SET entitlement = v_monthly_entitlement;

END LOOP;

CLOSE emp_cursor;
END

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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