IDENTITY_INSERT用於對表中的標識列進行顯式插入操作時的設置。格式如下:
set identity_insert TABLE_NAME ON/OFF
如果需要對表中定義為IDENTITY屬性的列進行插入操作,需要首先將identity_insert打開,才可對其進行插入操作,insert into TEST (id,product) values (1,'abc')
另外值得注意的是,如果identity_insert被打開,則后續在插入一行時必須要對該標識列進行插入操作才行,而不能省略identity列,否則會報錯。
insert into TEST (product) values ('dddd')
在執行以上語句時,省略了id列,所以會報錯。或者將set identity_insert TABLE_NAME OFF,關閉對標識列的插入,則可以不用插入標識列的值了!