cc *_*ung 7 postgresql plpgsql
在postgresql plpgsql中,
create function f1( p_i int ) returns table( c1 int ) as $$
begin
-- wish to exit, do not wish to return anything
if p_i < 0 then
-- cannot RETURN - since can only return record!
end if;
-- continue processing
return query select c2 from t1 where c1 = p_i;
...
end;
$$ language plpgsql;
Run Code Online (Sandbox Code Playgroud)
根据doc,断开函数的唯一方法是RETURN.但这里的RETURN需要RETURN QUERY或RETURN NEXT - 似乎没有办法简单地退出该功能.
如果p_i < 0实际上是一个错误,那么你可以引发异常:
if p_i < 0 then
raise exception 'Don''t know what to do with %', p_i
end if;
Run Code Online (Sandbox Code Playgroud)
如果p_i < 0只是安静地返回任何东西,那么你可以做这样的事情:
create or replace function f1( p_i int ) returns table( c1 int ) as $$
begin
if p_i < 0 then
return;
end if;
return query select c2 from t1 where c1 = p_i;
end;
$$ language plpgsql;
Run Code Online (Sandbox Code Playgroud)
从精细手册:
39.6.1.2.RETURN NEXT和RETURN QUERY
[...]
要返回的各个项由一系列RETURN NEXT或多个RETURN QUERY命令指定,然后使用不带参数的最终RETURN命令来指示该函数已完成执行.
强调我的.因此,您可以使用您的return query方式返回查询,只需一个简单return;的拯救,而无需做任何事情.
例如,该return;版本给我这样的东西:
=> select * from f1(-1);
c1
----
(0 rows)
=> select * from f1(1);
c1
----
1
1
...
(15 rows)
Run Code Online (Sandbox Code Playgroud)
并且异常版本执行此操作:
=> select * from f1(-1);
ERROR: Don't know what to do with -1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9451 次 |
| 最近记录: |