Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Aktualisieren der Identitätsspalte einer Tabelle mit fortlaufenden Nummern über eine gespeicherte SQL-Prozedur

--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