28

I am trying to drop table if it is exists in the present working database of PostgreSQL. For which I am trying the following query.

Example:

var1 := 'IF EXISTS (select * from INFORMATION_SCHEMA.TABLES WHERE name = ''Table_'|| Suffix ||''') then
      DROP TABLE Table_'||Suffix||'';

execute var1;

But getting error near IF.

3
  • 1
    Please add more context to your code, please post the full statement you trying to execute. Commented Jun 30, 2014 at 11:24
  • 1
    What's wrong with drop table if exists? Commented Jun 30, 2014 at 12:14
  • Show the exact text of the error message, and the surrounding code. Also, what's the PostgreSQL version? SELECT version(); Commented Jun 30, 2014 at 12:43

1 Answer 1

62

execute executes SQL statements, not PL/pgSQL commands. The IF statement is a PL/pgSQL construct.

In any case you can use

DROP TABLE IF EXISTS ...

(see the manual page for DROP).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.