sqlserver 自增ID插入指定数据

 

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 时,才能为表' '中的标识列指定显式值”。

posted @ 2014-11-12 20:33  蒹葭白露  阅读(49384)  评论(2编辑  收藏  举报