好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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如何实现从特定组合中随机读取值

标签:

查看更多关于Oracle如何实现从特定组合中随机读取值的详细内容...

  阅读:27次

上一篇: mysql启动过程

下一篇:Oracle sql trace