Postgresql 函数不返回任何值且不引发异常

Postgresql 函数不返回任何值且不引发异常,postgresql,Postgresql,下面的函数既不返回值也不引发异常 create or replace function get_custid(p_customerNum varchar2) RETURNS text AS $$ DECLARE cust_id customer.customer_num%TYPE; begin raise notice '%', message_text; select customer_num into cust_id from customer where customer_num = p

下面的函数既不返回值也不引发异常

create or replace function get_custid(p_customerNum varchar2) 
RETURNS text AS $$
DECLARE
cust_id customer.customer_num%TYPE;
begin
raise notice '%', message_text;
select customer_num into cust_id
from customer
where customer_num = p_customerNum;
return cust_id;
exception
when OTHERS then
raise notice '%', message_text;
raise;
end $$ language plpgsql;


select get_custid('Ab12345') from dual;
-客户编号已存在,但未返回任何行

select get_custid('DDDDDDD') from dual;

-客户编号不存在,但不会进入异常块

我认为您真的在使用postgresql,这段代码更有可能是您需要的或正在运行的代码

create table customer (cust_id int, customer_num varchar);
insert into customer values (1, 'Ab12345');

drop function get_custid(varchar);
create or replace function get_custid(p_customerNum varchar) 
RETURNS int AS $$
DECLARE
out_cust_id int;
begin
    --raise notice '%', message_text;

    select cust_id into out_cust_id
    from customer
    where customer_num = p_customerNum;

    if out_cust_id is null
    then raise exception 'your exception';
    end if;
    return out_cust_id;

end $$ language plpgsql;

select get_custid('Ab12345');
在中,如果指定了STRICT,则SELECT INTO仅在错误的行数上引发异常

create or replace function get_custid(p_customerNum varchar) 
RETURNS text AS $$
DECLARE
cust_id customer.customer_num%TYPE;
begin
raise notice '%', 'message_text';
select customer_num into strict cust_id
from customer
where customer_num = p_customerNum;
return cust_id;
exception
when OTHERS then
raise notice '%', 'message_text';
raise;
end $$ language plpgsql;

它没有理由进入异常块,因为代码中没有任何东西会生成异常。如果它真的不返回行,vs NULL,那么什么是对偶?SELECT*FROM dual是否返回任何行?如果没有,那么您的查询也不会得到任何结果,因为您的函数从未执行过。选择*from dual-不返回任何行。我已将select get_custid'Ab12345'作为其返回行执行。其中,选择get_custid'ddddd'时,不存在'ddddd'的数据,但不会引发返回NULL的异常。请帮帮我,“来自双重”这个词是甲骨文的。也许我错了,但我不认为这是存在于postgresql…为什么双重废话?嗨,艾修斯,谢谢你的代码。我已经执行了你的代码。但是如果我执行这个查询,请选择get_custid'test';-返回NULL。它不会引发异常。我想提出例外。请帮助我。如果答案适合您的需要,请不要忘记验证答案;