Skip to content

Instantly share code, notes, and snippets.

@ghotz
Created October 4, 2022 06:08
Show Gist options
  • Select an option

  • Save ghotz/00b11a1795e7137dbe4a2796ba8edbfc to your computer and use it in GitHub Desktop.

Select an option

Save ghotz/00b11a1795e7137dbe4a2796ba8edbfc to your computer and use it in GitHub Desktop.
Demonstrate how to shuffle column values in SQL Server
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