Sql 插入自定义主键

  在遇到数据库设计是自增的主键,且需要插入自定义的主键Id时,这个时候如果直接Insert的话,将会发生错误,错误提示信息:

当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'XXX' 中的标识列插入显式值。

需要手动设置 IDENTITY_INSERT 为 ON.

语法:

set identity_insert [TableName] on 
INSERT INTO [TableName] ('字段') VALUES('')
set identity_insert [TableName] off
原文地址:https://www.cnblogs.com/52XF/p/4744080.html