PostgreSQL:关系不存在时的异常

Mee*_*eem 1 postgresql

当关系不存在然后发生异常时,我试图在这里显示一条错误消息。

范例

Create or replace function fun_test() returns void as
$$ 
Begin
     Truncate testtb;
     Exception 
     When does_not_exist then /* When testtb does not exist*/
          raise info 'Relation does not exists';
     ...
Run Code Online (Sandbox Code Playgroud)

错误:无法识别的异常情况“ does_not_exist”

dou*_*tor 6

条件“does_not_exist”不存在。参考http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html

对于测试使用这样的代码......

EXCEPTION
  WHEN others THEN
    RAISE NOTICE '%; SQLSTATE: %', SQLERRM, SQLSTATE; 
Run Code Online (Sandbox Code Playgroud)

如果你想截断更多的表,我建议使用一个函数。


dij*_*tra 5

您可以使用“ undefined_table”进行处理。

EXCEPTION WHEN undefined_table THEN
     RAISE NOTICE '%; SQLSTATE: %', SQLERRM, SQLSTATE; 
Run Code Online (Sandbox Code Playgroud)