在只调用一次的存储函数中继续处理程序

时间:2017-07-17 14:19:17

标签: mysql function

存储过程声明表未找到异常的继续处理程序。发生异常时,将调用一次处理程序。我希望在调用函数时多次调用它。

如何为供应商 - 客户的每个组合运行处理程序内的代码?

drop function if exists continue_handler_surprise;

delimiter //;

drop table if exists logg;
create table if not exists logg (msg text);

create function continue_handler_surprise(
  customer_company_id integer,
  supplier_company_id integer,
  limit_old_catalogs integer)
  returns integer deterministic
  begin
    declare tmp_t varchar(90) default null;
    declare continue handler for sqlstate '42S02'
    begin
      select concat('cache_s',
                   supplier_company_id, '_c',
                   customer_company_id) into tmp_t;
      insert into logg select concat('create temp table?: ',
                   tmp_t, ' lim: ', limit_old_catalogs);
      create temporary table if not exists tmp_t (id int);
    end;
    do (select null from `tmp_t` limit 0);
    insert into logg  select concat('handled?: ',
                   coalesce(tmp_t, ' tmp_t=null '));
    return rand() * 4010;
  end //;
delimiter ;

set @lim = 33;

       select continue_handler_surprise(207,  2032, @lim) as item_id
union  select continue_handler_surprise(2543, 2032, @lim) as item_id
union  select continue_handler_surprise(2543, 2005, @lim) as item_id
union  select continue_handler_surprise(2543, 2006, @lim) as item_id;

select continue_handler_surprise(33, 44, 1) as "no-union 1";
select continue_handler_surprise(10, 20, 50) as "no-union 2";

select '========== log:' as '';
select msg from logg;

我认为它可能是一个联合的东西,但事实并非如此。

样本输出

$ mysql < continue-handler.sql 
item_id
2589
60
551
2576

no-union 1
3209
no-union 2
296

========== log:

msg
create temp table?: cache_s2032_c207 lim: 33
handled?: cache_s2032_c207
handled?:  tmp_t=null 
handled?:  tmp_t=null 
handled?:  tmp_t=null 
handled?:  tmp_t=null 
handled?:  tmp_t=null

注意tmp_t变量是如何在处理程序中初始化的。

1 个答案:

答案 0 :(得分:0)

Pebcak;

tmp_t初始化位置错误。应该是这样的:

select concat('cache_s', supplier_company_id, '_c', customer_company_id) into tmp_t;

do (select null from `tmp_t` limit 0);