当前位置:Gxlcms > 数据库问题 > Oracle如何实现从特定组合中随机读取值

Oracle如何实现从特定组合中随机读取值

时间:2021-07-01 10:21:17 帮助过:4人阅读

create table test(loc varchar2(2 char)); Table created.

构造能随机读取元素的SQL语句

select case mod(abs(dbms_random.random),5)
       when 1 then 北京
       when 2 then 上海
       when 3 then 广州
       when 4 then 深圳
       else 武汉 end "LOC"
from dual;

大批量填充test表的loc字段

begin
  for i in 1..10 loop
     insert into test values(case mod(abs(dbms_random.random),5)
          when 1 then 北京
          when 2 then 上海
          when 3 then 广州
          when 4 then 深圳
          else 武汉 end );
  end loop; 
end;

最后生成的结果如下:

SQL> select * from test;

LOC
--------
武汉
广州
上海
北京
上海
武汉
北京
上海
武汉
深圳

10 rows selected.

 

Oracle如何实现从特定组合中随机读取值

标签:

人气教程排行