MySQL 在函数内调用存储过程
MySQL Call a Stored Procedure inside a Function
所以我一直在到处寻找,试图了解如何在存储过程中调用函数,我在这里找到了这个例子:http://forums.mysql.com/read.php?98,175470,175476#msg-175476.
这帮助我构建了调用存储过程的函数,但是我一直收到此错误:
16:17:51 select regionUtilization(1,2) LIMIT 0, 1000 Error Code: 1415. Not allowed to return a result set from a function 0.000 sec
我要做的是调用以下存储过程并访问 OUT 变量。然后将其与输入的比较整数进行比较。
drop procedure if exists select_employeesByRegion_proc;
delimiter //
create procedure select_employeesByRegion_proc
(in search int, out result int)
begin
select t1.emp_id from (
select employees.emp_id from branch
inner join department
on department.br_id = branch.br_id
inner join employees
on employees.dep_id = department.dep_id
where branch.reg_id = search) as t1;
set result:=FOUND_ROWS();
end //
delimiter ;
以下是我目前拥有的功能
drop function if exists regionUtilization;
delimiter //
create function regionUtilization(search int, compare int)
returns boolean
begin
DECLARE temp int;
call select_employeesByRegion_proc(search, temp);
if temp >= compare then
return true;
else
return false;
end if;
end //
delimiter ;
我还考虑过将存储过程的两个方面分离成单独的过程,方法是一个计数和另一个 return 结果,但是这仍然首先需要过程 select 一些数据会导致与我已经收到的错误相同的错误。
关于如何解决结果集错误的任何建议?我没有 returning 结果集,我只是使用该结果集来选择在我的函数中 return 是真还是假。提前致谢!
谢谢@Barmar 的回答。是的,我需要在我的过程中使用游标来适当地声明我的函数。
drop procedure if exists build_regionUtil_proc;
delimiter //
create procedure build_regionUtil_proc(in search int, inout result int)
begin
declare v_finished integer default 0;
declare v_list int default 0;
declare region_cursor cursor for
select t1.emp_id from (
select employees.emp_id from branch
inner join department
on department.br_id = branch.br_id
inner join employees
on employees.dep_id = department.dep_id
where branch.reg_id = search) as t1;
declare continue handler
for not found set v_finished = 1;
open region_cursor;
get_results: loop
fetch region_cursor into v_list;
if v_finished = 1 then leave get_results;
end if;
set result = result + 1;
end loop get_results;
close region_cursor;
end //
delimiter ;
所以我一直在到处寻找,试图了解如何在存储过程中调用函数,我在这里找到了这个例子:http://forums.mysql.com/read.php?98,175470,175476#msg-175476.
这帮助我构建了调用存储过程的函数,但是我一直收到此错误:
16:17:51 select regionUtilization(1,2) LIMIT 0, 1000 Error Code: 1415. Not allowed to return a result set from a function 0.000 sec
我要做的是调用以下存储过程并访问 OUT 变量。然后将其与输入的比较整数进行比较。
drop procedure if exists select_employeesByRegion_proc;
delimiter //
create procedure select_employeesByRegion_proc
(in search int, out result int)
begin
select t1.emp_id from (
select employees.emp_id from branch
inner join department
on department.br_id = branch.br_id
inner join employees
on employees.dep_id = department.dep_id
where branch.reg_id = search) as t1;
set result:=FOUND_ROWS();
end //
delimiter ;
以下是我目前拥有的功能
drop function if exists regionUtilization;
delimiter //
create function regionUtilization(search int, compare int)
returns boolean
begin
DECLARE temp int;
call select_employeesByRegion_proc(search, temp);
if temp >= compare then
return true;
else
return false;
end if;
end //
delimiter ;
我还考虑过将存储过程的两个方面分离成单独的过程,方法是一个计数和另一个 return 结果,但是这仍然首先需要过程 select 一些数据会导致与我已经收到的错误相同的错误。
关于如何解决结果集错误的任何建议?我没有 returning 结果集,我只是使用该结果集来选择在我的函数中 return 是真还是假。提前致谢!
谢谢@Barmar 的回答。是的,我需要在我的过程中使用游标来适当地声明我的函数。
drop procedure if exists build_regionUtil_proc;
delimiter //
create procedure build_regionUtil_proc(in search int, inout result int)
begin
declare v_finished integer default 0;
declare v_list int default 0;
declare region_cursor cursor for
select t1.emp_id from (
select employees.emp_id from branch
inner join department
on department.br_id = branch.br_id
inner join employees
on employees.dep_id = department.dep_id
where branch.reg_id = search) as t1;
declare continue handler
for not found set v_finished = 1;
open region_cursor;
get_results: loop
fetch region_cursor into v_list;
if v_finished = 1 then leave get_results;
end if;
set result = result + 1;
end loop get_results;
close region_cursor;
end //
delimiter ;