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 に答える