Created
October 4, 2022 06:08
-
-
Save ghotz/00b11a1795e7137dbe4a2796ba8edbfc to your computer and use it in GitHub Desktop.
Demonstrate how to shuffle column values in SQL Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| USE AdventureWorks2017; | |
| GO | |
| SELECT * FROM Person.Person; | |
| BEGIN TRANSACTION; | |
| WITH cte AS | |
| ( | |
| SELECT | |
| ROW_NUMBER() OVER (ORDER BY BusinessEntityID) AS orig_rownum | |
| , ROW_NUMBER() OVER (ORDER BY NEWID()) AS new_rownum1 | |
| , ROW_NUMBER() OVER (ORDER BY NEWID()) AS new_rownum2 | |
| , LastName, FirstName | |
| FROM Person.Person | |
| ) | |
| UPDATE cte | |
| SET LastName = T2.LastName | |
| , FirstName = T3.FirstName | |
| FROM cte | |
| JOIN cte AS T2 ON cte.orig_rownum = T2.new_rownum1 | |
| JOIN cte AS T3 ON T2.new_rownum1 = T3.new_rownum2; | |
| SELECT * FROM Person.Person; | |
| IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment