Update IDs in identity table in SQL server

  1. Create a temporary table with the same schema as the identity table.
  2. Copy the data from the identity table into the temporary table.
  3. Update the IDs in the temporary table to the new values.
  4. Truncate the identity table.
  5. Copy the data in the temporary table back into the identity table.
  6. Drop the temporary table.

CREATE TABLE #TempTable (
ID INT IDENTITY,
Column1 VARCHAR(50),
Column2 VARCHAR(50)
);

INSERT INTO #TempTable
SELECT * FROM IdentityTable;

UPDATE #TempTable
SET ID = NewValue
WHERE ID = OldValue;

TRUNCATE TABLE IdentityTable;

INSERT INTO IdentityTable
SELECT * FROM #TempTable;

DROP TABLE #TempTable;

No comments yet.

Leave a comment

Request a Free SEO Quote