SQL Server自增字段插入值的實(shí)際操作
在實(shí)際操作中我們經(jīng)常會(huì)遇到向SQL Server自增字段插入值的這一狀況,以下的文章主要向大家描述的是向SQL Server自增字段插入值的實(shí)際操作步驟,在一般的情況下,不能直接向 SQL Server 數(shù)據(jù)庫(kù)的自增字段插入值。
如果非要這么干的話,SQL Server 就會(huì)好不客氣地給你個(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。
上述的相關(guān)內(nèi)容就是對(duì)SQL Server自增字段插入值的描述,希望會(huì)給你帶來(lái)一些幫助在此方面。
以上的相關(guān)內(nèi)容就是對(duì)SQL Server自增字段插入值的介紹,望你能有所收獲。
【編輯推薦】
- 對(duì)SQL Server SQL語(yǔ)句進(jìn)行優(yōu)化的10個(gè)原則
- C#來(lái)對(duì)SQL Server存儲(chǔ)過(guò)程進(jìn)行創(chuàng)建
- SQL Server導(dǎo)入升級(jí)還有什么你沒(méi)做?
- SQL Server索引的使用誤區(qū)講述
- SQL Server索引實(shí)際結(jié)構(gòu)的理解