标识列自动生成,不需要插入数据,只有当IDENTITY_INSERT 设置为 ON 时,才能显示录入数据.
你都OFF了 关闭了 还能插入值?
--1. 会话中某个表已将此属性设置为ON,当为另一个表发出了SET IDENTITY_INSERT ON 句时将出错 --测试的表 CREATE TABLE ta(id int IDENTITY(1,1),col int) CREATE TABLE tb(id int IDENTITY(1,1),col int) GO--设置 IDENTITY_INSERT 属性 SET IDENTITY_INSERT ta ON SET IDENTITY_INSERT tb ON GO /*======================================================*/ --2. 如果插入记录的标识值大于表的当前标识值,则SQL Server自动将新插入值作为当前标识值使用 --测试的表 CREATE TABLE tb(id int IDENTITY(1,1),col int)--强制在表中插入标识值 SET IDENTITY_INSERT tb ON INSERT tb(id,col) VALUES(10,1) SET IDENTITY_INSERT tb OFFINSERT tb(col) VALUES(2) SELECT * FROM tb /*--结果 id col ----------------- ----------- 10 1 11 2 --*/ GO /*======================================================*/ --3. 如果插入记录的标识值小于表的当前标识值,则表的当前标识值不受新插入值的影响 --测试的表 CREATE TABLE tb(id int IDENTITY(1,1),col int) INSERT tb VALUES(1) INSERT tb VALUES(2)--强制在表中插入标识值 SET IDENTITY_INSERT tb ON INSERT tb(id,col) VALUES(1,11) SET IDENTITY_INSERT tb OFFINSERT tb(col) VALUES(3) SELECT * FROM tb /*--结果 id col ----------------- ----------- 1 1 2 2 1 11 3 3 --*/
你插入的数据行中,有一列是自动标识列,是不需要插入的 比如,你的表有如下字段 : ID int (是自增列) name varchar(20)那么你插入的时候 insert into 表名 values('名字') 插入一列就好, 不要用 insert into 表名 values(1,'名字')
--------------------------------- -- Author: htl258(Tony) -- Date : 2009-06-30 21:53:23 --------------------------------- --> 生成测试数据表-aif not object_id('tb') is null drop table tb Go Create table tb([id] int identity,[col] nvarchar(2)) Insert tb select 'a' union all select 'b' Go --> 查询插入结果select * from tb /* id col ----------- ---- 1 a 2 b(2 行受影响) */set identity_insert tb oninsert tb(id,col) --注意这里的括号,要显式,如果直接 insert tb select '2','c'就不行了. select '2','c'select * from tb /* id col ----------- ---- 1 a 2 b 2 c(3 行受影响) */
--测试的表
CREATE TABLE ta(id int IDENTITY(1,1),col int)
CREATE TABLE tb(id int IDENTITY(1,1),col int)
GO--设置 IDENTITY_INSERT 属性
SET IDENTITY_INSERT ta ON
SET IDENTITY_INSERT tb ON
GO
/*======================================================*/
--2. 如果插入记录的标识值大于表的当前标识值,则SQL Server自动将新插入值作为当前标识值使用
--测试的表
CREATE TABLE tb(id int IDENTITY(1,1),col int)--强制在表中插入标识值
SET IDENTITY_INSERT tb ON
INSERT tb(id,col) VALUES(10,1)
SET IDENTITY_INSERT tb OFFINSERT tb(col) VALUES(2)
SELECT * FROM tb
/*--结果
id col
----------------- -----------
10 1
11 2
--*/
GO
/*======================================================*/
--3. 如果插入记录的标识值小于表的当前标识值,则表的当前标识值不受新插入值的影响
--测试的表
CREATE TABLE tb(id int IDENTITY(1,1),col int)
INSERT tb VALUES(1)
INSERT tb VALUES(2)--强制在表中插入标识值
SET IDENTITY_INSERT tb ON
INSERT tb(id,col) VALUES(1,11)
SET IDENTITY_INSERT tb OFFINSERT tb(col) VALUES(3)
SELECT * FROM tb
/*--结果
id col
----------------- -----------
1 1
2 2
1 11
3 3
--*/
你的表里有自增列,在插入的时候不要指定
例如,有表
ta(a int identity(1,1),b char(5))
在插入的时候只要写
insert ta(b) values('c')
而a列用户不能指定
比如,你的表有如下字段
:
ID int (是自增列)
name varchar(20)那么你插入的时候
insert into 表名 values('名字')
插入一列就好,
不要用
insert into 表名 values(1,'名字')
---------------------------------
-- Author: htl258(Tony)
-- Date : 2009-06-30 21:53:23
---------------------------------
--> 生成测试数据表-aif not object_id('tb') is null
drop table tb
Go
Create table tb([id] int identity,[col] nvarchar(2))
Insert tb
select 'a' union all
select 'b'
Go
--> 查询插入结果select * from tb
/*
id col
----------- ----
1 a
2 b(2 行受影响)
*/set identity_insert tb oninsert tb(id,col) --注意这里的括号,要显式,如果直接 insert tb select '2','c'就不行了.
select '2','c'select * from tb
/*
id col
----------- ----
1 a
2 b
2 c(3 行受影响)
*/