简单查询基于计数逻辑删除表中的记录

时间:2012-12-18 11:01:59

标签: sql plsql

一个包含pk和status列的表,其值为'Y','N','NULL'

查询:

获取状态栏为“Y”的记录计数,如果此计数超过记录总数的1%则不删除,否则删除表中的记录。

我试过这个

Declare   
  v_count Number;   
  v_count1 Number;   
BEGIN   
  v_count := select count(*) from temp; 
  v_count1 := select count(*) from temp where status = 'Y' ;

  v_count := v_count + ((0.1) * (v_count))

  if (v_count1 > v_count)
  {
    insert into  temp1 values(pk,status)
  } 
  else
  {   
    Delete from temp ;
  }
END;

1 个答案:

答案 0 :(得分:0)

Declare   
v_count Number;   
v_count1 Number;   
BEGIN   
select count(*) into v_count from temp; 
select count(*) into v_count1 from temp where status = 'Y' ;

v_count := v_count + ((0.01) * (v_count))

if (v_count1 > v_count)
{
  insert into  temp1 values(pk,status)
} 
else
{   
  Delete from temp ;
}
END;

更正你的函数这样的SQL查询.......享受