如何向自增列中插入数据(mssql)

如何向自增列中插入数据?


一、新建表

create table test(id int identity,a varchar(50))
create table test1(id int identity,a varchar(50))


二、初始化数据

insert into test (a)values(‘a’)
insert into test (a)values(‘b’)
insert into test (a)values(‘c’)
insert into test (a)values(‘d’)
insert into test (a)values(‘e’)
insert into test (a)values(‘f’)
insert into test (a)values(‘g’)
insert into test (a)values(‘h’)
insert into test (a)values(‘i’)
insert into test (a)values(‘j’)


三、检测数据情况

select * from test


四、向自增列表插入数据
{注意事项:插入时列名必须写入,不然会报错}

set IDENTITY_INSERT test1 on
insert into test1(id,a) select ID,a from test
set IDENTITY_INSERT test1 off


五、检测数据插入情况,再次向新表插入数据,检测自增列的增长情况

select * from test1

insert into test1(a)values(‘sdfsf’)

select * from test1