尽管肯定没有数据,但我无法以任何方式在函数中找到异常。编码:
create table t
(
id int,
val int
);
insert into t
values
(1, 1);
-- select * from t;
-- Запрос не возвращает запись
select * from t
where id = 100;
create or replace function f() returns int as
$$
declare
cr integer;
begin
select val into cr
from t
where id = 100;
return cr;
exception
when no_data_found then
return -1;
end
$$
language plpgsql;
select f();
返回 null,而不是 -1。难道我做错了什么?
正如可以在文档中阐明的那样,
select val into cr不会产生错误。这是正常情况。没有错误——在错误处理中没有什么可捕捉的。在严格模式下,会产生错误。
我不建议
pl/pgsql滥用异常。在幕后,它们依赖的保存点不是很自由,最重要的是,不仅在出现错误时免费,而且总是减慢工作速度。你可以写,例如,