Sql 如果表的记录少于n条,则删除该表
我正在试用postgres,但无法使用这个简单的查询:Sql 如果表的记录少于n条,则删除该表,sql,postgresql,postgresql-9.4,drop-table,Sql,Postgresql,Postgresql 9.4,Drop Table,我正在试用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适用于此,请参见
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适用于此,请参见-
Postgres不支持DROP表上的WHERE或IF子句:。为什么要这样做?但要回答你的问题,请在if后面加上一滴。在网上询问陌生人之前,先阅读文档。
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;
$$