0

我正在试验postgres并且无法使这个简单的查询起作用:

drop table mytable if (select count(*) from mytable)<50 ;

这给出了错误:

ERROR:  syntax error at or near "if"
LINE 1: drop table tablename if (select count(*) from mytable)<50 ;

对于给定的条件,您如何更改/删除 postgres 中的表?

4

1 回答 1

0

为此创建动态 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 nsp.nspname = 'public';
  if l_count < 50 then 
    execute 'drop table mytable';
  end if;
end;
$$
于 2016-03-21T18:57:12.607 回答