我正在试验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 中的表?
我正在试验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 中的表?
为此创建动态 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;
$$