我有两个功能。 set_credit_values和do_credit。我将从第一个内部调用第二个。然后它出现错误1 errors were found during analysis.Unexpected character. (near ":" at position 983)
,#1415 - Not allowed to return a result set from a function
。我的代码如下
DROP FUNCTION IF EXISTS do_credit;
DELIMITER $$;
CREATE FUNCTION do_credit (ltp_id int, cr_count tinyint, cr_on tinyint, next_cr date, cr_period tinyint)
RETURNS INT DETERMINISTIC
BEGIN
INSERT IGNORE INTO aaa_test(aaa_ltp_id, aaa_cr_count, aaa_cr_on, aaa_next_cr, aaa_cr_period) VALUES
(ltp_id, cr_count, cr_on, next_cr, cr_period);
RETURN LAST_INSERT_ID();
END $$;
DELIMITER ;
DROP FUNCTION IF EXISTS set_credit_values;
DELIMITER $$;
CREATE FUNCTION set_credit_values (emp_id bigint, lpc_id int, elp_date date) RETURNS boolean DETERMINISTIC
BEGIN
DECLARE ltp_id INT(11) DEFAULT 0;
DECLARE cr_count tinyint(4) DEFAULT 0;
DECLARE cr_on tinyint(1) DEFAULT 0;
DECLARE next_cr DATE DEFAULT NULL;
DECLARE cr_period tinyint(4) DEFAULT 0;
DECLARE v_finished INT(11) DEFAULT 0;
DEClARE cur_set_credit CURSOR FOR
SELECT lpb_fk_leave_type, lpb_cr_count, lpb_cr_on, lpb_next_cr, lpb_cr_period FROM erp_leave_policy_body
WHERE lpb_fk_leave_policy = lpc_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
OPEN cur_set_credit;
get_cr: LOOP
FETCH cur_set_credit INTO ltp_id, cr_count, cr_on, next_cr, cr_period;
IF v_finished = 1 THEN
LEAVE get_cr;
END IF;
SELECT do_credit(ltp_id, cr_count, cr_on, next_cr, cr_period) FROM DUAL;
END LOOP get_cr;
CLOSE cur_set_credit;
RETURN 1;
END $$;
DELIMITER ;
如果我在set_credit_values函数内部编写插入查询而不调用do_credit函数,它正在成功地工作我
DROP FUNCTION IF EXISTS set_credit_values;
DELIMITER $$;
CREATE FUNCTION set_credit_values (emp_id bigint, lpc_id int, elp_date date) RETURNS boolean DETERMINISTIC
BEGIN
DECLARE ltp_id INT(11) DEFAULT 0;
DECLARE cr_count tinyint(4) DEFAULT 0;
DECLARE cr_on tinyint(1) DEFAULT 0;
DECLARE next_cr DATE DEFAULT NULL;
DECLARE cr_period tinyint(4) DEFAULT 0;
DECLARE v_finished INT(11) DEFAULT 0;
DEClARE cur_set_credit CURSOR FOR
SELECT lpb_fk_leave_type, lpb_cr_count, lpb_cr_on, lpb_next_cr, lpb_cr_period FROM erp_leave_policy_body
WHERE lpb_fk_leave_policy = lpc_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
OPEN cur_set_credit;
get_cr: LOOP
FETCH cur_set_credit INTO ltp_id, cr_count, cr_on, next_cr, cr_period;
IF v_finished = 1 THEN
LEAVE get_cr;
END IF;
INSERT IGNORE INTO aaa_test(aaa_ltp_id, aaa_cr_count, aaa_cr_on, aaa_next_cr, aaa_cr_period) VALUES
(ltp_id, cr_count, cr_on, next_cr, cr_period);
END LOOP get_cr;
CLOSE cur_set_credit;
RETURN 1;
END $$;
DELIMITER ;
但我需要在do_credit函数中执行它,因为我需要对此进行一些其他查询。所以请任何人帮忙
答案 0 :(得分:0)
如下面的代码所示,请DECLARE one VARIABLE
并在调用函数后使用INTO
关键字在该变量中获取返回值。
希望这会对你有所帮助。
DROP FUNCTION IF EXISTS do_credit;
DELIMITER $$;
CREATE FUNCTION do_credit (ltp_id int, cr_count tinyint, cr_on tinyint, next_cr date, cr_period tinyint)
RETURNS INT DETERMINISTIC
BEGIN
INSERT IGNORE INTO aaa_test(aaa_ltp_id, aaa_cr_count, aaa_cr_on, aaa_next_cr, aaa_cr_period) VALUES
(ltp_id, cr_count, cr_on, next_cr, cr_period);
RETURN LAST_INSERT_ID();
END $$;
DELIMITER ;
DROP FUNCTION IF EXISTS set_credit_values;
DELIMITER $$;
CREATE FUNCTION set_credit_values (emp_id bigint, lpc_id int, elp_date date) RETURNS boolean DETERMINISTIC
BEGIN
DECLARE ltp_id INT(11) DEFAULT 0;
DECLARE var_id INT(11) DEFAULT 0; <--Declare this variable
DECLARE cr_count tinyint(4) DEFAULT 0;
DECLARE cr_on tinyint(1) DEFAULT 0;
DECLARE next_cr DATE DEFAULT NULL;
DECLARE cr_period tinyint(4) DEFAULT 0;
DECLARE v_finished INT(11) DEFAULT 0;
DEClARE cur_set_credit CURSOR FOR
SELECT lpb_fk_leave_type, lpb_cr_count, lpb_cr_on, lpb_next_cr, lpb_cr_period FROM erp_leave_policy_body
WHERE lpb_fk_leave_policy = lpc_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
OPEN cur_set_credit;
get_cr: LOOP
FETCH cur_set_credit INTO ltp_id, cr_count, cr_on, next_cr, cr_period;
IF v_finished = 1 THEN
LEAVE get_cr;
END IF;
SELECT do_credit(ltp_id, cr_count, cr_on, next_cr, cr_period) into var_id FROM DUAL; <--get return value in declared variable>
END LOOP get_cr;
CLOSE cur_set_credit;
RETURN 1;
END $$;
DELIMITER ;