I'm trying to write a query that deletes/drops a table, if another table exists:
DO
$do$
BEGIN
IF EXISTS (SELECT FROM table1) THEN
DELETE FROM table2;
END IF;
END
$do$;
but all that it is doing is deleting rows from table1 if table2 exists rather than the table itself.
Can you use a function similar to the one above or should I use drop table if exists?
That would be:
Rationale:
select
ing from a table is not the right way to check if it exists - if it doesn't, an error is raised. Instead, you can query the information schema.to remove a table, you want
drop table
;delete
, on the other hand, removes the content of the table, not the table itself.