lundi 4 juin 2018

Execute select statement IF table exists

I'm trying to execute a PSQL (9.6) statement only if certain table exists in schema, but it always respons with a syntax error each time I try to use conditional IF.

My query is something like...

IF EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = 'users') THEN
  SELECT * FROM users;
END IF;

And the output is...

ERROR:  syntax error at or near "IF"
LINE 1: IF EXISTS(SELECT 1 FROM information_schema.tables WHERE tabl...
        ^

Aucun commentaire:

Enregistrer un commentaire