【问题标题】:MySQL Call a Stored Procedure inside a FunctionMySQL 在函数内调用存储过程
【发布时间】:2016-04-11 20:37:57
【问题描述】:

所以我一直在到处寻找,试图了解如何在存储过程中调用函数,我在这里找到了这个示例: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 ;

我还研究了通过计算一个计数和另一个返回结果将存储过程的两个方面分离为单独的过程,但是这仍然首先需要过程选择一些会导致相同错误的数据我已经收到了。

关于如何解决结果集错误的任何建议?我没有返回结果集,我只是使用该结果集来选择在我的函数中是返回 true 还是 false。提前致谢!

【问题讨论】:

  • 该过程应该使用游标来处理查询结果,这样它就不会返回查询结果。

标签: mysql stored-procedures stored-functions


【解决方案1】:

感谢@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 ;

【讨论】:

    猜你喜欢
    • 2017-04-23
    • 2014-10-09
    • 2021-10-08
    • 2020-05-10
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    相关资源
    最近更新 更多