samedi 19 octobre 2019

How to shuffle rows in random order in SQL and save results in new table?

So I have a table called Customers

                     id  | name
                    ------------
                     1     Tom
                     2     Bob
                     3     Sam
                     4     Angel
                     5     Joe

I want to select the rows in the 'name' column in a random order, and insert those rows into a new table, I tried this

       select Name into TestTable from Customer order by NEWID() 
       select * from TestTable

But it doesn't work? The rows in TestTable are in the same order as they were in the original table.

                         Tom
                         Bob
                         Sam
                         Angel
                         Joe

I tried executing multiple times but the result is the same every time. It seems that the rows are being inserted into the new table before the randomization happens. Any ideas?




Aucun commentaire:

Enregistrer un commentaire