SQL Server手工插入識別欄位

來源:互聯網
上載者:User

如果我們在識別欄位中插入值,例如: 

insert member(id,username) values(10,'admin') 

則在查詢分析器裡面會返回錯誤資訊: 
 引用內容 伺服器: 訊息 544,層級 16,狀態 1,行 1 當 IDENTITY_Insert 設定為 OFF 時,不能向表 'member' 中的識別欄位插入顯式值。 

有的情況我們需要手動插入識別欄位的值,例如刪除了一些記錄後,識別欄位並不連續,而我們又想把它補齊。我們利用一個開關可以讓願望變成現實: 

SET IDENTITY_Insert [TableName] ON 

在查詢分析器裡面這樣寫: 
SET IDENTITY_Insert member ON insert member(id,username) values(1,'admin') SET IDENTITY_Insert member OFF 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.