且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改现有表中的Identity列Sql Server

更新时间:2023-12-01 10:14:04

请根据您的要求修改此脚本,以便在现有表中添加标识列.

please modified this script as per your requirement to add identity column to you existing table.

/* To prevent any potential data loss issues, you should review this script in detail 
before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_PERSON
	(
	ID int NOT NULL IDENTITY (1, 1),
	NAME varchar(25) NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_PERSON SET (LOCK_ESCALATION = TABLE)
GO
SET IDENTITY_INSERT dbo.Tmp_PERSON ON
GO
IF EXISTS(SELECT * FROM dbo.PERSON)
	 EXEC('INSERT INTO dbo.Tmp_PERSON (ID, NAME)
		SELECT ID, NAME FROM dbo.PERSON WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_PERSON OFF
GO
DROP TABLE dbo.PERSON
GO
EXECUTE sp_rename N'dbo.Tmp_PERSON', N'PERSON', 'OBJECT' 
GO
COMMIT



有关更多信息,请查看此链接
SQL Server添加删除身份 [ ^ ]

Sql Server添加删除身份2 [



for more information please review this link
SQL Server Add Remove Identity[^]
OR
Sql Server Add Remove Identity 2[^]


尝试一下:
DBCC CHECKIDENT(表名",RESEED,10);
Try this:
DBCC CHECKIDENT ("tablename", RESEED, 10);