Ath*_*hul 2 sql sql-server stored-procedures sql-server-2008
从表中删除重复记录后,我想更新表的 Identity 列,其连续编号从 1 开始。这是我的表详细信息
id(identity(1,1)),
EmployeeID(int),
Punch_Time(datetime),
Deviceid(int)
Run Code Online (Sandbox Code Playgroud)

我需要通过存储过程执行此操作。当我在存储过程中尝试以下语句时
DECLARE @myVar int
SET @myVar = 0
set identity_insert TempTrans_Raw# ON
UPDATE TempTrans_Raw# SET @myvar = Id = @myVar + 1
set identity_insert TempTrans_Raw# off
Run Code Online (Sandbox Code Playgroud)
出现错误,例如...无法更新标识列“Id”任何人请建议如何使用从 1 开始的连续编号来更新该表的标识列。
--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
Run Code Online (Sandbox Code Playgroud)