|
在PL/SQL中使用随机数和GUID
2006-12-14 15:43
ORACLE的PL/SQL提供了生成随机数和随机字符串的多种方式,罗列如下:
1、小数( 0 ~ 1) select dbms_random.value from dual 2、指定范围内的小数 ( 0 ~ 100 ) select dbms_random.value(0,100) from dual 3、指定范围内的整数 ( 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; |