在你的工作中是否会为了某个活动要随机取出一些符合条件的EMAIL或者手机号码用户,来颁发获奖通知或其它消息?
如果是的话,可以用oracle里生成随机数的PL/SQL, 目录文件名在:/ORACLE_HOME/rdbms/admin/dbmsrand.sql。
用之前先要在sys用户下编译:
SQL>@/ORACLE_HOME/rdbms/admin/dbmsrand.sql
它实际是在sys用户下生成一个dbms_random程序包,同时生成公有同义词,并授权给所有数据库用户有执行的权限。
使用dbms_random程序包, 取出随机数据的方法:
1. 先创建一个唯一增长的序列号tmp_id
create sequence tmp_id increment by 1 start with 1 maxvalue 9999999 nocycle nocache;
2. 然后创建一个临时表tmp_1,把符合本次活动条件的记录全部取出来。
create table tmp_1 as select tmp_id.nextval as id,email,mobileno from 表名 where 条件;
找到最大的id号:
select max(id) from tmp_1;
假设为5000
3. 设定一个生成随机数的种子
execute dbms_random.seed(12345678);
或者
execute dbms_random.seed(TO_CHAR(SYSDATE,'MM-DD-YYYY HH24:MI:SS'));
4. 调用随机数生成函数dbms_random.value生成临时表tmp_2
假设随机取200个
create table tmp_2 as select trunc(dbms_random.value(1,5000)) as id from tmp_1 where rownum<201;
[ 说明:dbms_random.value(1,5000)是取1到5000间的随机数,会有小数,
trunc函数对随机数字取整,才能和临时表的整数ID字段相对应。
注意:如果tmp_1记录比较多(10万条以上),也可以找一个约大于两百行的表(假如是tmp_3)来生成tmp_2
create table tmp_2 as select trunc(dbms_random.value(1,5000)) as id from tmp_3 where rownum<201; ]
5. tmp_1和tmp_2相关联取得符合条件的200用户
select t1.mobileno,t1.email from tmp_1 t1,tmp_2 t2 where t1.id=t2.id;
[ 注意:如果tmp_1记录比较多(10万条以上),需要在id字段上建索引。]
也可以输出到文本文件:
set pagesize 300;
spool /tmp/200.txt;
select t1.mobileno,t1.email from tmp_1 t1,tmp_2 t2 where t1.id=t2.id order by t1.mobileno;
spool off;
6. 用完后,删除临时表tmp_1、tmp_2和序列号tmp_id。ORACLE的PL/SQL提供了生成随机数和随机字符串的多种方式,罗列如下:
1、小数( 0 ~ 1)
select dbms_random.value from dual
2、指定范围内的小数 ( 0 ~ 100 )
select dbms_random.value(0,100) from dual3、指定范围内的整数 ( 0 ~ 100 )
select trunc(dbms_random.value(0,100)) from dual
4、长度为20的随机数字串
select substr(cast(dbms_random.value as varchar2(38)),3,20) from dual
5、正态分布的随机数
select dbms_random.normal from dual
6、随机字符串
select dbms_random.string(opt, length) from dual
opt可取值如下:
'u','U' : 大写字母
'l','L' : 小写字母
'a','A' : 大、小写字母
'x','X' : 数字、大写字母
'p','P' : 可打印字符7、随机日期
select to_date(2454084+TRUNC(DBMS_RANDOM.VALUE(0,365)),'J') from dual
通过下面的语句获得指定日期的基数
select to_char(sysdate,'J') from dual
8、生成GUID
select sys_guid() from dual
--生成带分隔符(-)的GUID的自定义函数
create or replace function my_guid
return varchar2
is
guid varchar(36);
temp varchar(32);
begin
temp:=sys_guid();
guid:= substr(temp,1,8) || '-'
||substr(temp,9,4) || '-'
||substr(temp,13,4)|| '-'
||substr(temp,17,4)|| '-'
||substr(temp,21,12);
return guid;
end;--生成n位随机数的方法
--select locationServiceNew.dbo.getRandom(10)
--比较郁闷..好麻烦
alter function GetRandom(@nLength int=4)
returns int
as
begin
declare @nStart int,@vchLength varchar(50)
set @nstart = 1
set @vchLength = '1'
if @nLength >9 set @nLength = 9 --如果随机数位数大于9那么将其修改为9
if @nLength <1 set @nLength = 1 --如果随机数位数小于1那么将其修改为1
while @nStart <= @nLength
begin
set @vchLength = @vchLength+'0'
set @nStart = @nStart+1
end declare @floatNum float,@intNum int
--set @num=cast(left(rand(),8) as float)*1000000
select @floatNum = RandomValue from uv_GetRandom
set @intNum = cast(@floatNum*cast(@vchLength as int) as int)
return (@intNum)
end
go--测试
select locationServiceNew.dbo.getRandom(7)