删除表(如果存在于 PostgreSQL 数据库中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24488730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:31:26  来源:igfitidea点击:

Drop table if exists in PostgreSQL database

postgresql

提问by Meem

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.

我试图删除表,如果它存在于 PostgreSQL 的当前工作数据库中。为此,我正在尝试以下查询。

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.

但是在 附近出现错误IF

回答by harmic

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

execute执行 SQL 语句,而不是 PL/pgSQL 命令。IF 语句是一个 PL/pgSQL 结构。

In any case you can use

在任何情况下,您都可以使用

DROP TABLE IF EXISTS ...

(see the manual page for DROP).

(请参阅DROP 的手册页)。