开发者

Generate Random Number in Oracle

开发者 https://www.devze.com 2023-03-30 22:00 出处:网络
In one of my project i have to generate a random number from 1 to 20开发者_StackOverflow00 and store them into table.I have to store 2000 value in the table.I have to store different value in the tabl

In one of my project i have to generate a random number from 1 to 20开发者_StackOverflow00 and store them into table.I have to store 2000 value in the table.I have to store different value in the table. Hoe to do this in oracle.I am using Oracle 11g .


If you have to store 2000 unique random integers between 1 and 2000 you can use the following code:

declare 
i pls_integer;
begin
for i in 1..2000 loop
  insert into my_table (my_column) values (i);
end loop;
end;
/

(There is only one set of 2000 unique integers between 1 and 2000 and that is the set of every number from 1 to 2000.)

If you want to generate random numbers I recommend using DBMS_CRYPTO.RANDOMINTEGER.

To insert random numbers between 1 and 2000 (duplicates allowed), exchange the insert above with:

insert into my_table (my_column) values (MOD(DBMS_CRYPTO.RANDOMINTEGER, 2000) + 1);

In earlier versions (Oracle 10 and earlier) use the DBMS_RANDOM package.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号