SQL Server自增字段插入值的步驟
我們今天主要與大家一起分享的是SQL Server數(shù)據(jù)庫之向SQL Server自增字段中插入值的正確操作步驟,以及對其實(shí)際操作中要用到的實(shí)際操作代碼的描述,以下就是文章的詳細(xì)內(nèi)容的具體描述。
通常情況下,不能向 SQL Server 自增字段插入值,如果非要這么干的話,SQL Server 就會好不客氣地給你個(gè)錯(cuò)誤警告:
- 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.
這個(gè)錯(cuò)誤消息提示我們,如果向 SQL Server 自增字段插入值,需要設(shè)置 identity_insert 選項(xiàng)為 on。
- set identity_insert on
看具體的一個(gè)例子:
- 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
注意的是,自增字段插入值后,要及時(shí)把 identity_insert 設(shè)置為 off。
【編輯推薦】