今天有同事问我一个关于 sql server自增字段值如何关闭自增,手动修改自增ID的问题。以前经常使用这样的SQL
SET IDENTITY_INSERT
允许将显式值插入表的标识列中。
语法
SET IDENTITY_INSERT [ database.[ owner.] ] { table } { ON | OFF }
Sample:
CREATE TABLE products (id int IDENTITY PRIMARY KEY, product varchar(40))
INSERT INTO products (id,product) VALUES (2,'screwdriver')
set IDENTITY_INSERT ub..products on
INSERT INTO products (id,product) VALUES (2,'screwdriver') // OK
set IDENTITY_INSERT ub..products off
INSERT INTO products (id,product) VALUES (4,'screwdriver')
INSERT INTO products (product) VALUES ('scrdsafdasdewdriver')
select * from products