重設 Identity 接下來遞增的值

USE tempdb;
GO
create table t(c1 int identity primary key)
go
insert t default values
go 20

delete t where c1 < 10
go

DBCC CHECKIDENT (‘t’, RESEED,1);
go

–加一筆 2
insert t default values
go
select * from t
GO

 

set identity_insert t on
go
insert t(c1) values(5)
select * from t
go

set identity_insert t off
go

–加一筆 6
insert t default values
select * from t
go

–違反 PK Error
insert t default values
go 20

訊息 2627,層級 14,狀態 1,行 34
違反 PRIMARY KEY 條件約束 ‘PK__t__3213663BA6D0B54B’。無法在物件 ‘dbo.t’ 中插入重複的索引鍵。重複的索引鍵值是 (10)。
陳述式已經結束。
** 執行批次時發現錯誤。正在繼續。

drop table t

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

%d 位部落客按了讚: