I have the following table in my database:
So I want to generate Unique-identifier and Update the UniqueID column as shown below:
If values of UniqueID are the same, then they must have the same Uniqueidentifier. I have tried to write a CTE as below:
;WITH Duplicates AS ( SELECT *, RowNum = ROW_NUMBER() OVER (PARTITION BY UniqueID ORDER BY TypeID) FROM ##Table1 ) UPDATE ##Table1 SET UniqueID = NEWID() FROM Duplicates d
But am not getting what am looking for. Very knew to the world of CTEs. What is the best way to get the results am looking for?
Advertisement
Answer
You can try below – if I understand correctly then you need a update join
WITH Duplicates AS ( SELECT distinct UniqueID,NEWID() as nid FROM ##Table1 ) UPDATE a SET UniqueID = nid from ##Table1 a join Duplicates d on a.UniqueID=d.UniqueID