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
.
Description. The DROP TABLE statement deletes the specified table, and any data associated with it, from the database. The IF EXISTS clause allows the statement to succeed even if the specified tables does not exist.
The CASCADE option allows you to remove the table and its dependent objects. The RESTRICT option rejects the removal if there is any object depends on the table. The RESTRICT option is the default if you don't explicitly specify it in the DROP TABLE statement.
Open your pgAdmin and then go the object tree where we will go to the database and then move to the public section under schemas and then select the Employee table which we want to delete or drop. The drop table popup window will appear on the screen, where we will click on the Yes button to drop the Employee table.
However, to drop a table that is referenced by a view or a foreign-key constraint of another table, CASCADE must be specified. ( CASCADE will remove a dependent view entirely, but in the foreign-key case it will only remove the foreign-key constraint, not the other table entirely.)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With