Skip to content
Advertisement

Update duplicate data with same unique-identifier

I have the following table in my database:

enter image description here

So I want to generate Unique-identifier and Update the UniqueID column as shown below:

enter image description here

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
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement