日期:2014-05-17  浏览次数:20803 次

按照几率插入记录
表中有两个字段A,B。A是几率值,B默认值是0,含义0是显示 1隐藏
如果A为50,说明有50%的几率插入B的值为1
如果A为80,说明有80%的几率插入B的值为1.
这个该怎么写。。。用plsql的话

------解决方案--------------------
SQL code

declare 
  -- Local variables here
  random_num number;
begin
  -- Test statements here
  for rec in (select id,A,B from test)
  loop
        select trunc(dbms_random.value(1,10))
        into random_num
        from dual;
        
        if random_num <= rec.A
           then 
               update test t set t.B = 1 where t.id = rec.id; 
        end if;
  
  end loop;
  
end;