说清楚点 原来的表示这样的:、 id name ischeck 1 jack 1 2 tony 2 3 amanda 3 4 xiao 4 5 ming 5插入数据后是这样的: id name ischeck 1 jack 1 2 tony 2 3 amanda 4 4 xiao 5 5 ming 6 6 tao 3 ID不一定是我上面的1开始,但是是自增长的, ischeck就是从1开始
--生成临时表,生成序号 select id,IDENTITY(INT,20,1) as oBy INTO #temp1 from table1 order by ischeck asc,id desc --修改序号为顺序 update table1 set ischeck=oBy from #temp1 a inner join table1 b on a.id=b.id --删除临时表 DROP TABLE #temp1
IDENTITY(INT,20,1) 20表示最小数从20开始,改成1就从1开始
declare @num int select @num = min(a.row_num) from (select *,row_number() over(order by ischeck) row_num from test) a left join test b on a.row_num = b.ischeck where b.id is null --添加 insert test(name,ischeck) values('aa',@num)这样就可以了
首先查找你要插入行的ischeck: select @ischeck =ischeck from tb where ID=@id接下来将其他行的编号更新掉: update tb set ischeck=ischeck+1 from tb where ischeck >=@ischeck接下来将要插入的行插进去: insert into tb(ischeck) vlaues(@ischeck)步骤有点多,有用就行
原来的表示这样的:、
id name ischeck
1 jack 1
2 tony 2
3 amanda 3
4 xiao 4
5 ming 5插入数据后是这样的:
id name ischeck
1 jack 1
2 tony 2
3 amanda 4
4 xiao 5
5 ming 6
6 tao 3
ID不一定是我上面的1开始,但是是自增长的, ischeck就是从1开始
--生成临时表,生成序号
select id,IDENTITY(INT,20,1) as oBy INTO #temp1 from table1 order by ischeck asc,id desc
--修改序号为顺序
update table1 set ischeck=oBy from #temp1 a inner join table1 b on a.id=b.id
--删除临时表
DROP TABLE #temp1
20表示最小数从20开始,改成1就从1开始
declare @num int
select @num = min(a.row_num) from (select *,row_number() over(order by ischeck) row_num from test) a left join test b
on a.row_num = b.ischeck where b.id is null
--添加
insert test(name,ischeck) values('aa',@num)这样就可以了
select @ischeck =ischeck from tb where ID=@id接下来将其他行的编号更新掉:
update tb set ischeck=ischeck+1 from tb where ischeck >=@ischeck接下来将要插入的行插进去:
insert into tb(ischeck) vlaues(@ischeck)步骤有点多,有用就行