我有一个SQL Server数据库,想要更改自增列的起始值,因为它从 10010
开始,并且与另一个表相关联。现在我有200条记录,希望在记录数量增加之前解决这个问题。
最好的方式是如何更改或重置此列?
我有一个SQL Server数据库,想要更改自增列的起始值,因为它从 10010
开始,并且与另一个表相关联。现在我有200条记录,希望在记录数量增加之前解决这个问题。
最好的方式是如何更改或重置此列?
无法更新标识列。
与其他列可以使用更新语句不同,SQL Server 不允许更新标识列。
尽管有一些替代方法可实现类似的需求。
使用DBCC CHECKIDENT,检查表的当前标识值,如果需要,则更改标识值。
DBCC CHECKIDENT('tableName', RESEED, NEW_RESEED_VALUE)
使用IDENTITY_INSERT,它允许将明确的值插入到表的标识列中。
SET IDENTITY_INSERT YourTable {ON|OFF}
例子:
-- Set Identity insert on so that value can be inserted into this column
SET IDENTITY_INSERT YourTable ON
GO
-- Insert the record which you want to update with new value in the identity column
INSERT INTO YourTable(IdentityCol, otherCol) VALUES(13,'myValue')
GO
-- Delete the old row of which you have inserted a copy (above) (make sure about FK's)
DELETE FROM YourTable WHERE ID=3
GO
--Now set the idenetity_insert OFF to back to the previous track
SET IDENTITY_INSERT YourTable OFF
update table
set identity_column_name = some value
让我告诉你,这不是一个简单的过程,也不建议使用它,因为可能会有一些与之关联的外键
。
但以下是操作步骤,请先备份表格:
步骤1- 选择表格的设计视图
步骤2- 关闭自增列
现在您可以使用update
查询。
完成后请重复步骤1和步骤2并打开自增列。
set identity_insert YourTable ON
然后删除您的行并使用不同的标识重新插入它。
一旦完成插入,请不要忘记关闭identity_insert。
set identity_insert YourTable OFF
--before running this make sure Foreign key constraints have been removed that reference the ID.
--set table to allow identity to be inserted
SET IDENTITY_INSERT yourTable ON;
GO
--insert everything into a temp table
SELECT *
INTO #tmpYourTable
FROM yourTable
--clear your table
DELETE FROM yourTable
--insert back all the values with the updated ID column
INSERT INTO yourTable (IDCol, OtherCols)
SELECT ID+1 as updatedID --put any other update logic to the ID here
, OtherCols FROM #tmpYourTable
--drop the temp table
DROP TABLE #tmpYourTable
--put identity back to normal
SET IDENTITY_INSERT yourTable OFF;
GO
尝试使用DBCC CHECKIDENT
命令:
DBCC CHECKIDENT ('YourTable', RESEED, 1);
SET IDENTITY_INSERT dbo.TableName ON
INSERT INTO dbo.TableName
(
TableId, ColumnName1, ColumnName2, ColumnName3
)
VALUES
(
TableId_Value, ColumnName1_Value, ColumnName2_Value, ColumnName3_Value
)
SET IDENTITY_INSERT dbo.TableName OFF
我曾看到过几种方法来完成这个任务,但在我看来最好且更快的方法如下:
身份列有一个计数器,它不一定与已注册的列相同。您可以使用以下SQL命令查看此计数器的值:
DBCC CHECKIDENT('tableName', NORESEED);
如果您想编辑身份列,那么您将无法进行操作,但我建议在重置计数器后创建一个新记录,以达到您所需的编号。要重置计数器,请使用以下命令:
DBCC CHECKIDENT('tableName', RESEED, desiredNumber);
我曾经遇到过类似的问题,我需要更新一些ID。我所做的是(我需要将它们增加10k):
set identity_insert YourTable ON
INSERT INTO YourTable
([ID]
,[something1]
,[something2]
,[something3])
SELECT
([ID] + 10000)
,[something1]
,[something2]
,[something3])
FROM YourTable
WHERE something1 = 'needs updeted id'
AND something2 = 'some other condition'
set identity_insert YourTable OFF
DELETE FROM YourTable
WHERE ID >= 'your old ID From'
AND ID <= 'Your old ID To'
就是这样。希望您理解这个逻辑,在我这里还涉及到与其他表的PK-FK键连接,这意味着在从'YourTable'
中删除原始行之前,我必须先更新它们。
我知道已经有答案了,我只是想留下SQL查询作为示例。
DBCC CHECKIDENT(table_name, RESEED, value)
= 指定需要重置值的表名
= 初始值为0,用于将标识列从1开始
UPDATE YourTable SET IdentityCol = 13
)。SET IDENTITY_INSERT YourTable ON
只允许插入而不是更新。 - Ian Boyd