SQL Server數(shù)據(jù)庫自增字段正確的插入值的描述
我們今天主要向大家講述的是SQL Server數(shù)據(jù)庫之向SQL Server自增字段正確的插入值的實際操作步驟,在一般的情況下,我們不能向 SQL Server 數(shù)據(jù)庫自增字段中插入值,如果非要這么干的話,SQL Server 就會好不客氣地給你個錯誤警告:
- Server: Msg 544, Level 16, State 1, Line 1
- Cannot insert explicit value for identity column in table 't' when identity_insert is set to OFF.
這個錯誤消息提示我們,如果向 SQL Server 數(shù)據(jù)庫自增字段插入值,需要設(shè)置 identity_insert 選項為 on。
- set identity_insert on
看具體的一個例子:
- create table dbo.t
- (
- id int identity(1,1) not null,
- name varchar(50)
- )
- set identity_insert t on
- insert into t (id, name) values(1, 'sqlstudy')
- set identity_insert t off
注意的是,自增字段插入值后,要及時把 identity_insert 設(shè)置為 off。
上述的相關(guān)內(nèi)容就是對SQL Server數(shù)據(jù)庫之向SQL Server自增字段插入值的描述,希望會給你帶來一些幫助在此方面。
【編輯推薦】