Update IDs in identity table in SQL server
- Create a temporary table with the same schema as the identity table.
- Copy the data from the identity table into the temporary table.
- Update the IDs in the temporary table to the new values.
- Truncate the identity table.
- Copy the data in the temporary table back into the identity table.
- 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.