SQL Server UPDATE
statement to switch values between random rows in all the rows on the table.
I need to switch values from an specific column between rows for all the rows on the table. Which row change values with which other row on the table is totally Ramdow. I'm doing this on Oracle, SQL Server and DB2.
Example:
Before update:
ID - Name - LastName
--------------------
1 - Peter - Gonzalez
2 - Max - Green
...
4480 - Maximus - Magna
4495 - Martha - Potter
After update:
1- Martha - Gonzalez
2- William - Green
...
4480 - Peter - Magna
4495 - otherNameFromAnotherRow - Potter
I checked this related question:
I tried to combine the logic of doing a random order with the Update of a row with data of the next row, but I couldn't make it right:
UPDATE HR.PERSONS
SET (NAME) = SELECT NAME
FROM
(SELECT t1.NAME
FROM PERSONS t1
ORDER BY dbms_random.value)
WHERE t1.ROWID > PERSONS.ROWID
AND
UPDATE HR.PERSONAS
SET (NOMBRE) = (SELECT NOMBRE
FROM
(SELECT t1.NOMBRE
FROM PERSONAS t1
ORDER BY dbms_random.value)
WHERE ROWNUM = 1)
In this case, the subquery just get one of the values, and set that value to all the rows.
This examples are for Oracle (They doesn't work on oracle), I'm trying to make that query right and accomplish the same in SQL Server and DB2 too).