我正在试验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中的表?
答案 0 :(得分:0)
创建动态SQL适用于此(请参阅answer) -
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;
$$