--(1)当一个会话启动的时候只能有一个identity_insert #tb on,否则会报错 create table #ta ( id int identity(1,1), col1 int ) create table #tb ( id int identity(1,1), col1 int ) set identity_insert #ta off set identity_insert #tb on --(2)当设置identity_insert选项值为 ON的时候,可以插入id. create table #te ( id int identity(1,1), col1 varchar(10) ) insert into #te values ('aa') insert into #te values ('bb') select * from #te set identity_insert #te on insert into #te(id,col1) values (10,'ff') set identity_insert #te off insert into #te values ('hh') --(3)当使用identity_insert插入一个最小的id,之后在在插入新的值,不会以那个小的值进行递增的 create table #TF ( id int identity(1,1), col1 int ) insert into #TF select 1 union all select 2 union all select 3 set identity_insert #TF on insert into #TF (id,col1) select 1,10 set identity_insert #TF off insert into #TF select 4 select * from #TF --(4)identity列与普通列的相互转换 ---<1>indetity列转换为普通列 create table #tg ( id int identity(1,1), col1 int ) insert into #tg select 1 union all select 2 union all select 3 alter table #tg add col2 int update #tg set col2=id select * from #tg alter table #tg drop column id select * from #tg ---<2>将普通列转换为identity列 create table #TY ( col1 int, col2 int ) insert into #TY select 1,1 union all select 2,2 union all select 13,53 union all select 24,44 --铺助表 create table #TT ( id int identity(1,1), col1 int ) set identity_insert #TT off insert into #TT (id,col1) select col1,col2 from #TY select * from #TT insert into #TT select 46