簡體   English   中英

如何打印代碼,PostgreSQL / plpgsql類型異常

[英]How to print code, type of PostgreSQL / plpgsql exception

給定這個pl / pgSQL函數

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;
  $$;

顯然, select f( 0 ); 將導致代碼22012異常,類型為division_by_zero 知道這一點后,我可以將exception子句的選擇器縮小到when division_by_zero then ...

但是,對於任意函數,如何獲取錯誤類型? 有沒有類似raise notice error.code

使用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) 

閱讀有關錯誤和消息以及陷阱錯誤的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM