Sarfaraz Makandar
Sarfaraz Makandar

Reputation: 6743

PostgreSQL: Exception when relation does not exist

Here I am trying to display a error message when relation does not exist then exception occurred.

Example:

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';
     ...

ERROR: unrecognized exception condition "does_not_exist"

Upvotes: 1

Views: 6744

Answers (2)

dijkstra
dijkstra

Reputation: 1078

You can handle with "undefined_table".

EXCEPTION WHEN undefined_table THEN
     RAISE NOTICE '%; SQLSTATE: %', SQLERRM, SQLSTATE; 

Upvotes: 6

double_word_disruptor
double_word_disruptor

Reputation: 189

The condition "does_not_exist" does not exist. Refer to http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html

For testing use code like this ...

EXCEPTION
  WHEN others THEN
    RAISE NOTICE '%; SQLSTATE: %', SQLERRM, SQLSTATE; 

If you want to truncate more tables I suggest to use a function.

Upvotes: 7

Related Questions