sql >> Base de Datos >  >> RDS >> Sqlserver

Actualización de la columna de identidad de una tabla con números consecutivos a través del procedimiento almacenado de SQL

--before running this make sure Foreign key constraints have been removed that reference the ID. 

--insert everything into a temp table
SELECT (ColumnList) --except identity column
INTO #tmpYourTable
FROM yourTable

--clear your table
DELETE FROM yourTable
-- reseed identity
DBCC CHECKIDENT('table', RESEED, new reseed value)
--insert back all the values 
INSERT INTO yourTable (ColumnList)
SELECT OtherCols FROM #tmpYourTable
--drop the temp table
DROP TABLE #tmpYourTable
GO