我试图根据条件在启动时删除一个表:
IF NOT EXISTS (select * from pg_class where relname = 'mytable' and relpersistence = 'u') DROP TABLE IF EXISTS mytable
解决方法
IF不能在sql中使用,这仅对PL / pgsql有效.
您需要在匿名PL / pgsql块中使用动态sql执行此操作.就像是:
do $$ declare l_count integer; begin select count(*) into l_count from pg_class c join pg_namespace nsp on c.relnamespace = nsp.oid where c.relname = 'mytable' and c.relpersistence = 'u' and nsp.nspname = 'public'; if l_count = 1 then execute 'drop table mytable'; end if; end; $$
您可能应该将select语句扩展为针对pg_namespace加入,并在where条件中包含模式名称,以确保您不会意外地从错误的模式中删除表.