How Oracle implements random reading of values from a specific combination

Source: Internet
Author: User

Here, we will use the Dbms_random package and case when statement, the idea is as follows:

First, the use of dbms_random. The random function randomly generates values and then models the values, and if we want to read them randomly in 10 elements, we need to take a modulo of 10.

Secondly, the value after modulo is associated with the element using the case when statement.

For example, I have a combination of "Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Wuhan" Five elements that want to randomly read values from these five elements to populate a field of the table.

First, create a test table

SQL>createtablevarchar2(2char)); Table created.

Constructs an SQL statement that can read elements randomly

Select  CaseMoDABS(Dbms_random.random),5)        when 1  Then 'Beijing'        when 2  Then 'Shanghai'        when 3  Then 'Guangzhou'        when 4  Then 'Shenzhen'       Else 'Wuhan' End"LOC" fromDual

Loc field for bulk fill test table

begin   forIinch 1..TenLoopInsert  intoTestValues( CaseMoDABS(Dbms_random.random),5)           when 1  Then 'Beijing'           when 2  Then 'Shanghai'           when 3  Then 'Guangzhou'           when 4  Then 'Shenzhen'          Else 'Wuhan' End ); EndLoop;End;

The results of the final generation are as follows:

SQL>Select* from test; LOC-------- Wuhan Guangzhou Shanghai Beijing Shanghai Wuhan Beijing Shanghai Wuhan Shenzhen  the rows selected.

How Oracle implements random reading of values from a specific combination

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.