DBMS_RANDOM使用
DBMS_RANDOM使用
2006-10-18 11:44:22| 分類: ORACLE知識 |字號
1.dbms_random.random
--它生成一個10位的隨機整數。
declare
l_num number;
counter number;
begin
counter:=1;
while counter<=10
loop
l_num:=abs((dbms_random.random mod 100))+1;
dbms_output.put_line(l_num);
counter:=counter+1;
end loop;
end;
2. dbms_random.string
--oracle裡建立隨機數字和字串
Do you know how to auto generate random numbers or strings in Oracle? Generating random numbers is required when there is a need to create a lot of data for testing purposes, or when we simply need to use a number to temporarily tag a process. It may also be necessary to generate random password strings of a fixed size--a very common requirement for websites that create and maintain logins for users.
Whatever the need, the fact is that Oracle provides us with a random number generator. This option is faster than writing your own random generation logic in PL/SQL as Oracle's internal processing logic is used. In addition, it can also be used to generate both character and alphanumeric strings.
DBMS_RANDOM package
The
DBMS_RANDOM package will generate random data in character, numeric or
alphanumeric formats. The size and the range from which to pickup the
random values can also be specified. This package is created by the
script. dbmsrand.sql available in the
The following functions present in the package can be used to serve the purpose of generating random numbers and strings. RANDOM - generate random numbers.
VALUE - generate random numbers from the range provided. The range will be taken as 0-1 if none is provided.
STRING - generate strings in upper case, lower case or alphanumeric format.
The first parameter takes the string type to be generated, the following values can be provided in upper or lower case.
'u','U' : upper case alpha characters only
'l','L' : lower case alpha characters only
'a','A' : alpha characters only (mixed case)
'x','X' : any alpha-numeric characters (upper)
'p','P' : any printable characters
Providing any other character will return the output in upper case only.
The size of the string should also be provided as the second parameter.
Oracle documentation says that it is necessary to initialize the package before using the random number generator. Oracle by default initializes the package with the seed value as the current user name, current time down to the second and the current session id.
INITIALIZE - Initialize the package to proceed with the number generation.
Provide a number (seed) as input to the routine.
SEED - Used to change the seed value. It is used in the internal algorithm to generate values. Setting this will
generate the random numbers in an order that will be similar in multiple sessions. Refer to the example below.
TERMINATE - Close the process of random number generation.
Examples:
Below are some examples of using the package.
E.g.: Generating a random number (positive or negative)
SQL> select dbms_random.random from dual;
RANDOM
_____________
1393936551
E.g.: Generating a random number between 0 and 1.
SQL> select dbms_random.value from dual;
VALUE
_____________
1
E.g.: Generating a random number from a range, between 1 to 1000.
SQL> select dbms_random.value(1,1000) num from dual;
NUM
_____________
611
E.g.: Generating a 12 digit random number.
SQL> select dbms_random.value(100000000000, 999999999999) num from dual;
NUM
_____________
175055628780
E.g.: Generating an upper case string of 20 characters
SQL> select dbms_random.string('U', 20) str from dual;
STR
_______________________
VUOQOSTLHCKIPIADIZTD
E.g.: Generating a lower case string of 20 characters
SQL> select dbms_random.string('L', 20) str from dual;
STR
____________________
xpoovuspmehvcptdtzcz
E.g.: Generating an alphanumeric string of 20 characters. There is a bug in Oracle 8i that results in special (non-alphanumeric) characters such as ']' in the string. This is resolved in Oracle 9i.
SQL> select dbms_random.string('A', 20) str from dual;
STR
__________________
sTjERojjL^OlTaIc]PLB
E.g.: Generating an upper case alphanumeric string of 20 characters
SQL> select dbms_random.string('X', 20) str from dual;
STR
________________________
SQ3E3B3NRBIP:GOGAKSC
E.g.: Generating a string of printable 20 characters. This will output a string of all characters that could possibly be printed.
SQL> select dbms_random.string('P', 20) str from dual;
STR
___________________
*Yw>IKzsj\uI8K[IQPag
E.g.: Example for calling the dbms_random package and setting the seed for generating the same set of random numbers in different sessions. Please note that the same random numbers are generated in different sessions. Though I have found this to work on most accounts, in some cases, the first number generated was different in different sessions and the remaining were same. I recommend not using this option in any of production code until it is properly document by Oracle.
jaJA>declare
2 l_num number;
3 begin
4 l_num := dbms_random.random;
5 dbms_output.put_line(l_num);
6 dbms_random.seed('amar testing 67890');
7 l_num := dbms_random.random;
8 dbms_output.put_line(l_num);
9 end;
10 /
483791552
478774329
PL/SQL procedure successfully completed.
Conclusion
DBMS_RANDOM
is a good utility and will find its way into lot of development
projects, especially web based ones. However, this Package is not
exhaustively documented. One should not use it just for the sake of it
being there. Make sure that there is a true requirement or a necessity
of random values before making use of this package. If you already have a
custom code meant for the same purpose, check out the benefits that are
available when using this package compared to your application.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27042095/viewspace-736105/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【DBMS_RANDOM】使用 DBMS_RANDOM包生成隨機字串random隨機字串
- dbms_randomrandom
- 使用DBMS_RANDOM過程引發的問題random
- Oracle系統包dbms_randomOraclerandom
- oracle隨機數 — dbms_randomOracle隨機random
- 【DBMS_RANDOM】從 DBMS_RANDOM建立指令碼獲得隨機資訊的生成方法random指令碼隨機
- oracle系統包——dbms_random用法Oraclerandom
- dbms_random在Oracle 10.2.0.4下的bugrandomOracle
- dbms_random包呼叫隨機數的方法:random隨機
- laravel使用EasyWeChat 使用Laravel
- 使用FTP限制使用者FTP
- 配置vsftpd匿名使用服務,個人使用者使用以及虛擬使用者使用配置細節!FTP
- Laravel passport 多端使用者使用LaravelPassport
- 使用 CSS 追蹤使用者CSS
- mongodb使用者與角色使用MongoDB
- RecyclerView使用指南(四)—— 使用ItemDecorationView
- RecyclerView使用指南(一)—— 基本使用View
- 使用dwebsocket在Django中使用WebsocketWebDjango
- 限制使用者使用session數Session
- 使用Index提示 強制使用索引Index索引
- ImageJ使用教程(一):開始使用
- winscp使用教程多使用者,winscp使用教程多使用者,教程詳情
- vi/vim使用進階: 在VIM中使用GDB除錯 – 使用vimgdb除錯
- 使用jquery和使用框架的區別jQuery框架
- Docker 使用者操作使用說明Docker
- 儘量使用 useReducer,不要使用 useStateuseReducer
- PyCharm使用技巧(六):Regullar Expressions的使用PyCharmExpress
- 使用Bootstrap tab頁切換的使用boot
- Urllib庫的使用一---基本使用
- 使用PyCharm引入需要使用的包PyCharm
- 使用 JWT 認證使用者身份JWT
- 使用者授權,策略的使用
- 使用普通使用者執行 dockerDocker
- 熟練使用使用jQuery Promise (Deferred)jQueryPromise
- mongoDB使用詳解(在node中使用)MongoDB
- 使用Git管理專案 使用總結Git
- Scrapy框架的使用之Scrapyrt的使用框架
- openfire使用自定義使用者表