Abd*_*rif 165 t-sql sql-server-2008
我有SQL Server数据库,我想更改标识列,因为它从一个大数字开始,10010它与另一个表相关,现在我有200条记录,我想在记录增加之前解决这个问题.
更改或重置此列的最佳方法是什么?
Sac*_*hin 244
与普通列不同,SQL Server不允许更新标识列的值.因此无法更新标识列.
虽然有一些替代方案可以满足类似的要求.其中之一是:
DBCC CHECKIDENT('tableName', RESEED, NEW_RESEED_VALUE)
Run Code Online (Sandbox Code Playgroud)
如果要更新现有记录的标识列的值,则需要进行设置
set identity_insert YourTable ON
Run Code Online (Sandbox Code Playgroud)
例
-- 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 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 prevoius track
SET IDENTITY_INSERT YourTable OFF
Run Code Online (Sandbox Code Playgroud)
Luv*_*Luv 55
如果你的问题得到了解决,你就想做点什么
update table
set identity_column_name = some value
Run Code Online (Sandbox Code Playgroud)
让我告诉你,这不是一个简单的过程,不建议使用它,因为它可能有一些foreign key相关联.
但是这里有一些步骤,请拿一张back-up桌子
步骤1-选择表的设计视图

步骤2-关闭标识列

现在您可以使用update查询.
现在redo执行步骤1和步骤2并打开标识列
小智 49
你需要
set identity_insert YourTable ON
Run Code Online (Sandbox Code Playgroud)
然后删除您的行并使用不同的标识重新插入它.
完成插入后,不要忘记关闭identity_insert
set identity_insert YourTable OFF
Run Code Online (Sandbox Code Playgroud)
kuk*_*lei 13
--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
Run Code Online (Sandbox Code Playgroud)
尝试使用DBCC CHECKIDENT:
DBCC CHECKIDENT ('YourTable', RESEED, 1);
Run Code Online (Sandbox Code Playgroud)
小智 7
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
Run Code Online (Sandbox Code Playgroud)
使用 Identity_Insert 时不要忘记包含列名,因为 sql 将不允许您在不指定列名的情况下插入
DBCC CHECKIDENT(table_name, RESEED, value)
Run Code Online (Sandbox Code Playgroud)
table_name = 给出要重置的表的值
值=初始值为零,以 1 开始标识列
小智 5
将您的表复制到一个没有标识列的新表。
select columns into newtable from yourtable
Run Code Online (Sandbox Code Playgroud)
使用新种子向 newtable 添加标识列并将其作为主键
ALTER TABLE tableName ADD id MEDIUMINT NOT NULL AUTO_INCREMENT KEY
Run Code Online (Sandbox Code Playgroud)
小智 5
正如我所见,有几种方法可以做到这一点,但我认为最好、更快的方法是以下一种:
标识列有一个计数器,不一定与注册的列相同,您可以使用以下 SQL 命令查看该计数器的值:
DBCC CHECKIDENT('tableName', NORESEED);
Run Code Online (Sandbox Code Playgroud)
然后,如果您想编辑身份列,您将无法,但我建议在将计数器重新播种到您需要的数字后进行新的寄存器。要重新设定计数器的种子,请使用以下命令:
DBCC CHECKIDENT('tableName', RESEED, desiredNumber);
Run Code Online (Sandbox Code Playgroud)