簡體   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