我对 PL/pgSQL 中的异常有一个小问题。我的任务是编写一个函数来查找具有一定长度的水库。
我的代码:
create or replace function
info_about_reservoir(num_of_letters int)
returns int as
$$
declare
res_name varchar(50);
res_type varchar(50);
res_area decimal(10,0);
counter int := 1;
begin
select r_name,t_name,r_area into strict res_name,res_type,res_area
from
reservoirs right outer join reservoirs_types
on t_id=r_t_id
where char_length(r_nazwa)=$1;
raise notice 'Name: %, type: %, area: %',res_name,res_type,res_area;
exception
when no_data_found then
raise notice 'No reservoir with name lenght %',$1;
when too_many_rows then
raise notice 'Too much reservoirs with name lenght %',$1;
return counter;
end;
$$ language plpgsql;
对于 num_of_letters 必须返回异常: --SELECT info_about_reservoir(7) -- no_data_found --SELECT info_about_reservoir(8) -- too_many_rows --SELECT info_about_reservoir(9) -- Name: % ...
在此脚本的先前版本中,我只返回了异常和错误:查询没有结果数据的目的地。现在它返回 7:名称:... 8:名称:来自某些行查询的第一行 ... 对于 9:名称:来自一个行查询的行 ...
很抱歉造成混乱,我有一个答案:
create or replace function
info_about_reservoir(num_of_letters int)
returns int as
$$
declare
res_name varchar(50);
res_type varchar(50);
res_area int;
counter int := 1;
begin
select r_name,t_name,r_area into strict res_name,res_type,res_area
from
reservoirs right outer join reservoirs_types
on t_id=a_t_id
where char_length(r_name)=$1;
raise notice 'Name: %, type: %, area: %',res_name,res_type,res_area;
return counter;
exception
when no_data_found then
raise notice 'No reservoir with name lenght %',$1;
return counter;
when too_many_rows then
raise notice 'Too much reservoirs with name lenght %',$1;
return counter;
end;
$$ language plpgsql;
现在它起作用了。:D