sqlserver 自增ID插入指定数据

时间:2014-11-12 20:56:38   收藏:0   阅读:171

 

set identity_insert 表名 ON --允许对自增列Id插入指定数据
insert into table_name(Id,Name) values(1,‘test‘)
set identity_insert 表名 OFF --关闭对自增列Id插入指定数据

注意:

1.set identity_insert只对当前会话生效。

2.set identity_insert 表名 ON 设置后,必须显示指定Id,否则插入错误。如insert into table_name values(‘111‘)将报错。

   向自增ID插入指定值。 报错:“当 IDENTITY_INSERT 设置为 OFF 时,不能为表 ‘ ‘ 中的标识列插入显式值”。

   插入语句未显示指定ID。 报错:“仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表‘ ‘中的标识列指定显式值”。

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!