Deprecated. Use the methods in the DBMS_CRYPTO built-in package,這個包已經不建議使用了
附,dbms_random幾個參數的介紹: function value return number,返回一個[0,1)之間的隨機數,精度為38位(Gets a random number, greater than or equal to 0 and less than 1, with decimal 38 digits) function value(low IN NUMVBER,high IN NUMBER) return number,返回一個[low,high)之間的隨機數 function normal return number,return random numbers in a standard normal distribution,返回服從正態分布的一組數,標準偏差為1,期望值為0,返回值中68%介于+1 和 -1 之間,95%介于 +2 和 -2 之間,99%介于+3 和 -3之間。 function random return BINARY_INTEGER, (Generate Random Numeric Values), function string(opt char,length Number) return varchar2(the maximum is 60),返回一個指定長度的字符串( Create Random Strings),opt seed values: 'a','A'n
問:我工作中的問題:主管讓我為了某個活動要隨機取出一些符合條件的EMAIL或者手機號碼用戶,來頒發獲獎通知或其它消息,我們公司用的Oracle 9i 請問這個如何實現? 答:可以用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 rownum201; [ 說明: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 rownum201; ] 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。