In database table REF_VALUES, ID column acts as the primary key and ORIGINAL_VALUE is used as the original column, whose values are to be shuffled and updated to the empty SHUFFLED_VALUE column. Below is a selection from the table:
Databases differ in their solution to the problem. Here Oracle is used as the example:
create or replace procedure shuffle as
TYPE My_CurType IS REF CURSOR;
OPEN CUR_1 FOR select t1.id,t2.result from (
select rownum rn,id,ORIGINAL_VALUE from REF_VALUES order by ORIGINAL_VALUE) t1
select rownum rn,result from(
select ORIGINAL_VALUE result from REF_VALUES order by dbms_random.value()))t2
fetch CUR_1 INTO V_id,V_result ;
EXIT WHEN CUR_1%NOTFOUND;
v_sql:=’update REF_VALUES set SHUFFLED_VALUE=”’||V_result||”’ where id=’||V_id;
EXECUTE immediate v_sql;
You need to write multilayered nested subqueries to get the shuffling result (as shown by the first half of the above code), then you need cursors (or temporary tables) and the stored procedure to insert it to the target table. The code will be rather tedious.
The esProc approach allows us to dispense with the complex nested subqueries and applies to various types of databases. The code is as follows:
|1||=myDB1.query(“select id,ORIGINAL_VALUE from REF_VALUES”)|
|4||=myDB1.update@u(A3,REF_VALUES,id:_1,SHUFFLED_VALUE:_2 ; id)|
A1: Execute the SQL statement to get data from ID column and ORIGINAL_VALUE column.
A2: Shuffle the values in ORIGINAL_VALUE column.
A3: Join A1’s ID column with A2’s ORIGINAL_VALUE column to create a two-dimensional table, as shown below:
A4：Update REF_VALUES table with A3’s table. @u option means only generating the UPDATE statement. The updated REF_VALUES table is as follows: