我不太明白。。我的功能是:
create or replace function myfunc(integer, varchar(25), varchar(25), integer, integer) returns numeric as $$
declare var_return numeric;
begin
select sum(a+ b) / sum(a + b + c)
from mytable
where col1 = $1
and col2 = $2
and col3 = $3
and col4 between $4 AND $5
into var_return;
exception when division_by_zero then return 0.0;
return coalesce(var_return, 0.0);
end;
$$ language plpgsql;
但当我看到:
ERROR: control reached end of function without RETURN
CONTEXT: PL/pgSQL function "myfunc"
为什么会这样?显然,我想在
select myfunc(123, 'foo', 'bar', 1, 10);
语句遇到select
等于0并返回0的情况时捕获。 最佳答案
EXCEPTION子句适用于整个BEGIN块,并一直运行到结束。也就是说,Postgres知道你写了这个:
create or replace function myfunc(...) returns ... as $$
declare var_return numeric;
begin
select ...
into var_return; -- but no RETURN statement in this block
exception when division_by_zero then
return 0.0;
return coalesce(var_return, 0.0); -- this is part of the exception handler
end;
$$ language plpgsql;
将“RETURN COALESCE…”移到异常行上方,然后重试。
关于postgresql - PostgreSQL&division_by_zero异常(exception),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8810884/