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

时间:2019-01-30 09:43:36

标签: mysql stored-procedures cursor

我正在使用游标在记录上迭代在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 entitlement 中获得价值,该价值始终为 0.0000 。 我试图在单独的过程中运行每月的权利查询,它返回正确的值。我具有表列和变量的匹配数据类型。

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

2 个答案:

答案 0 :(得分:0)

避免将局部变量命名为列名,请参见Chapter 2 Restrictions on Stored Programs :: Name Conflicts within Stored Routines。尝试更改以下内容:

...
-- 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;
...

答案 1 :(得分:0)

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

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