The shown output seems to correspond to select * from #Temp order by ID
.
Maybe you actually need this:
select * from #Temp order by NEWID()
-- or
update t
set ID = i
from (select *, row_number() over (order by NEWID()) i from #Temp ) t
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Am trying to update the same table by shuffling the column data, no condition to rearrange. I just want to make sure that the ID does not match with same car name again.
I tried my best with self join, Lead and LAG function still no luck.
any help greatly appreciated.
Create table #Temp
(
ID INT,
Carname Varchar(10)
)
Insert into #Temp Values (3,'AAAA')
Insert into #Temp Values (4,'BBBB')
Insert into #Temp Values (1,'CCCC')
Insert into #Temp Values (11,'DDDD')
Insert into #Temp Values (6,'EEEE')
Insert into #Temp Values (10,'FFFF')
Insert into #Temp Values (8,'GGGG')
Insert into #Temp Values (2,'HHHH')
Insert into #Temp Values (3,'IIII')
Insert into #Temp Values (7,'JJJJ')
Insert into #Temp Values (9,'KKKK')
Expecting results (shuffled data)
1 CCCC
2 HHHH
3 IIII
3 AAAA
4 BBBB
6 EEEE
7 JJJJ
8 GGGG
9 KKKK
10 FFFF
11 DDDD
The shown output seems to correspond to select * from #Temp order by ID
.
Maybe you actually need this:
select * from #Temp order by NEWID()
-- or
update t
set ID = i
from (select *, row_number() over (order by NEWID()) i from #Temp ) t