我正在尝试编码存储的先验来管理公司的叶子,因为叶子应该定期记入每个员工。我正在使用表格
erp_leave_policy => Describing Leave policy,
erp_employees => Describing Employees,
erp_clients => Describing Employees are belonged to which Client,
erp_employee_leave_policy => Describing leave policy of employees.
我在存储过程中使用以下查询
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;
它没有问题,工作正常。
但是在我的存储过程中,此查询无效。我正在使用codeigniter。我的代码如下。
$this->db->query("DROP FUNCTION IF EXISTS inserter;");
$q = " CREATE FUNCTION inserter(emp_id bigint,lpc_id int) RETURNS boolean DETERMINISTIC
BEGIN
INSERT INTO aaa_test (aaa_lpc_id,aaa_emp_id) VALUES (lpc_id, emp_id);
RETURN 1;
END;";
$this->db->query($q);
$this->db->query("DROP PROCEDURE IF EXISTS start_credit_test;");
$q = " CREATE PROCEDURE start_credit_test()
BEGIN
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE lpc_id INT(11) DEFAULT 0;
DECLARE 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 lpc_id,emp_id;
IF v_finished = 1 THEN
LEAVE get_emp;
END IF;
inserter(emp_id,lpc_id);
END LOOP get_emp;
CLOSE emp_cursor;
END ";
$this->db->query($q);
$query = $this->db->query("CALL start_credit_test();");
错误如下
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(emp_id,lpc_id); END LOOP get_emp; CLO' at line 27
CREATE PROCEDURE start_credit_test() BEGIN DECLARE v_finished INTEGER DEFAULT 0; DECLARE lpc_id INT(11) DEFAULT 0; DECLARE 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 lpc_id,emp_id; IF v_finished = 1 THEN LEAVE get_emp; END IF; inserter(emp_id,lpc_id); END LOOP get_emp; CLOSE emp_cursor; END
我尝试使用
替换inserter(emp_id,lpc_id);
而不调用函数inserter()来尝试此代码
INSERT INTO aaa_test (aaa_lpc_id,aaa_emp_id) VALUES (lpc_id, emp_id);
现在它没有显示错误。但没有插入表aaa_test。
此外,在DELIMITER //
之前使用CREATE PROCEDURE start_credit_test()
时,以及当我致电$this->db->query()
时,它也会显示错误。例如
$q = " DELIMITER //
CREATE PROCEDURE start_credit_test()
BEGIN
.........
.............";
$this->db->query($q);
错误如下
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER // CREATE PROCEDURE start_credit_test() BEGI' at line 1
你能帮忙吗
答案 0 :(得分:0)
关于分隔符的问题
$q = " DELIMITER $$;
CREATE PROCEDURE start_credit_test()
BEGIN
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE lpc_id INT(11) DEFAULT 0;
DECLARE 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 lpc_id,emp_id;
IF v_finished = 1 THEN
LEAVE get_emp;
END IF;
inserter(emp_id,lpc_id);
END LOOP get_emp;
CLOSE emp_cursor;
END$$
DELIMITER ; ";
答案 1 :(得分:0)
调用inserter
过程附近的语法错误,您应该使用CALL命令 -
...
CALL inserter(emp_id,lpc_id);
...
另一个,我认为你可以简化你的任务:
有INSERT...SELECT statement在一个命令中插入来自另一个数据集的记录,例如 -
INSERT INTO aaa_test (aaa_lpc_id, aaa_emp_id)
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;
答案 2 :(得分:0)
错误可能包含在:DECLARE v_finished INTEGER DEFAULT 0;
更改为:DECLARE v_finished INT(11) DEFAULT 0;
答案 3 :(得分:0)
谢谢大家。
我按照你的建议纠正了我的代码。并更改了我使用存储过程声明的变量的名称。那就是我改变了
DECLARE lpc_id INT(11) DEFAULT 0;
DECLARE emp_id BIGINT(20) DEFAULT 0;
如
DECLARE my_lpc_id INT(11) DEFAULT 0;
DECLARE my_emp_id BIGINT(20) DEFAULT 0;
然后我改变了
call inserter(emp_id,lpc_id);
如
INSERT INTO aaa_test (aaa_emp_id,aaa_lpc_id) VALUES (my_emp_id,my_lpc_id);
现在我的总代码为
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;
INSERT INTO aaa_test (aaa_emp_id,aaa_lpc_id) VALUES (my_emp_id,my_lpc_id);
END LOOP get_emp;
CLOSE emp_cursor;
END$$;
DELIMITER ;
它会将值aaa_test插入为 Click Here
但是当我调用函数插入器时,它又出现了错误
PROCEDURE abn_erp.inserter does not exist
我将整个代码放在
之下 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 ;
但是当我调用start_credit_test()时
$this->db->query("CALL start_credit_test();")
收到错误PROCEDURE abn_erp.inserter does not exist