2017-06-17 7 views
1

、PostgreSQLの/ plpgsqlが例外の種類

drop function if exists f(float); 
create function f(x float) 
    returns float 
    language plpgsql 
    as $$ 
    begin 
     return 1/x; 
    exception 
     when others then 
     raise notice 'oops'; 
     return 0::float; 
    end; 
    $$; 

このPL/pgSQLの機能を考えると、select f(0);コード22012例外、タイプdivision_by_zeroにつながることは明らかです。これを知ると、exception句のセレクタをwhen division_by_zero then ...に絞り込むことができます。

ただし、任意の関数については、どのようにエラータイプを取得できますか? raise notice error.codeのようなものはありますか?

答えて

2

使用sqlstate、例:

drop function if exists f(float); 
create function f(x float) 
    returns float 
    language plpgsql 
    as $$ 
    begin 
     return 1/x; 
    exception 
     when others then 
     raise notice 'oops %', sqlstate; 
     return 0::float; 
    end; 
$$; 

select f(0); 

NOTICE: oops 22012 
f 
--- 
0 
(1 row) 

Errors and MessagesTrapping Errors.

についてもっと読みます
関連する問題