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