【发布时间】:2013-11-15 10:34:30
【问题描述】:
我有一个关于 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) -- 名称:% ...
在此脚本的先前版本中,我只返回了异常和错误:查询没有结果数据的目的地。现在它回来了 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
【问题讨论】:
-
您应该将您的答案添加为实际答案。
-
然后你可以在一天后回来接受你自己的答案。
-
您能否通过table-qualifying它们来澄清代码中哪个列属于哪个表?这是一个很好的做法,我怀疑那里仍然存在小故障..
标签: sql postgresql exception-handling plpgsql