在存储过程中调用函数时出错

时间:2017-02-28 07:07:59

标签: php mysql codeigniter

我正在编写一个存储过程来管理公司员工的离职。休假应定期记入每位员工。

我的代码如下

DROP FUNCTION IF EXISTS inserter;

DELIMITER $$;
CREATE FUNCTION inserter(emp_id bigint, lpc_id int) RETURNS boolean DETERMINISTIC
BEGIN
    INSERT INTO aaa_test (aaa_emp_id,aaa_lpc_id) 
    VALUES (emp_id,lpc_id);   

    RETURN 1;
END $$;
DELIMITER ;

DROP PROCEDURE IF EXISTS start_credit_test;

DELIMITER $$;

CREATE PROCEDURE start_credit_test()
BEGIN
    DECLARE v_finished INT(11) DEFAULT 0;
    DECLARE my_lpc_id INT(11) DEFAULT 0;
    DECLARE my_emp_id BIGINT(20) DEFAULT 0;

    DECLARE emp_cursor CURSOR FOR 
        SELECT lpc_id, emp_id  
        FROM erp_leave_policy, erp_employees, erp_clients, erp_employee_leave_policy 
        WHERE emp_status = 1 
          AND cli_status = 1 
          AND lpc_status = 1 
          AND emp_id = elp_fk_employees 
          AND lpc_id = elp_fk_leave_policy 
          AND cli_id = emp_fk_clients;

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;

    OPEN emp_cursor;

    get_emp: LOOP

        FETCH emp_cursor INTO my_lpc_id,my_emp_id;

        IF v_finished = 1 THEN 
            LEAVE get_emp;
        END IF;

        call inserter(my_emp_id,my_lpc_id);  

   END LOOP get_emp;

   CLOSE emp_cursor;

END $$;
DELIMITER ; 

我正在使用codeigniter。然后当我做

$this->db->query("CALL start_credit_test();");

我收到错误:

  

程序abn_erp.inserter不存在

程序内的查询

SELECT lpc_id, emp_id  
FROM erp_leave_policy, erp_employees, erp_clients, erp_employee_leave_policy 
WHERE emp_status = 1 
  AND cli_status = 1 
  AND lpc_status = 1 
  AND emp_id = elp_fk_employees 
  AND lpc_id = elp_fk_leave_policy 
  AND cli_id = emp_fk_clients;

返回输出:

Click Here

然后我再次插入函数

            DELIMITER $$;
            CREATE FUNCTION inserter(emp_id bigint, lpc_id int) RETURNS boolean DETERMINISTIC
            BEGIN

                INSERT INTO aaa_test (aaa_emp_id,aaa_lpc_id) VALUES (emp_id,lpc_id);   

                RETURN 1;
            END $$;
            DELIMITER ;

但它显示错误#1304 - FUNCTION inserter already exists

1 个答案:

答案 0 :(得分:0)

请使用inseterLeave重命名,如下所示。

DELIMITER $$;
CREATE FUNCTION inseterLeave (emp_id bigint, lpc_id int) RETURNS boolean DETERMINISTIC
BEGIN
    INSERT INTO aaa_test (aaa_emp_id,aaa_lpc_id) VALUES (emp_id,lpc_id);   
    RETURN 1;
END $$;
DELIMITER ;

并使用以下代码重写程序。

call call function的{​​{1}}关键字不需要procedure。我已根据它进行了更改。

       DROP PROCEDURE IF EXISTS start_credit_test;

            DELIMITER $$;
            CREATE PROCEDURE start_credit_test()
                BEGIN
                    DECLARE v_finished INT(11) DEFAULT 0;
                    DECLARE my_lpc_id INT(11) DEFAULT 0;
                    DECLARE my_emp_id BIGINT(20) DEFAULT 0;


                    DEClARE emp_cursor CURSOR FOR 

                    SELECT lpc_id,emp_id  FROM erp_leave_policy,erp_employees,erp_clients,erp_employee_leave_policy 
                            WHERE emp_status=1 AND cli_status=1 AND lpc_status=1 AND 
                            emp_id = elp_fk_employees AND lpc_id = elp_fk_leave_policy AND cli_id = emp_fk_clients;

                    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;

                    OPEN emp_cursor;

                        get_emp: LOOP

                            FETCH emp_cursor INTO my_lpc_id,my_emp_id;

                            IF v_finished = 1 THEN 
                            LEAVE get_emp;
                            END IF;
 SELECT inseterLeave(my_emp_id,my_lpc_id) FROM DUAL; <-- Please remove call keyword from this line.Function did not require call keyword!>


                        END LOOP get_emp;

                    CLOSE emp_cursor;

                    END $$;
                    DELIMITER ; 

我认为inseter内置functionFunction同名already exist。希望这会对您有帮助。