2016-03-21 20 views
0

Ben postgres denemekte olduğum ve işe bu basit sorgu alınamıyor daha az varsa:Bırak tablo o n kayıtları

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

Bu veriyor hatası: Eğer değiştirmek nasıl

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

Belirli bir koşul için postgres/bırak tabloları?

+1

Postgres DAMLA table' 'üzerinde' WHERE' veya 'IF' maddesini desteklemez: http://www.postgresql.org /docs/current/static/sql-droptable.html. –

+1

Neden istiyorsun? Ama sorunuzu cevaplamak için '' '' '' '' '' 'd AF put put put put put put put put put put put koyun. İnternette yabancılara sormadan önce belgeleri okumaya başlayın. –

cevap

0

oluşturma dinamik SQL (answer bakınız) Bunun için çalışır -

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; 
$$