首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql 平添序号方法

2012-08-22 
sql 添加序号方法create table test(id int identity (1,1),orderno int,name varchar(10))truncate table

sql 添加序号方法

create table test(id int identity (1,1),orderno int,name varchar(10))truncate table testinsert into test(name) values ('张三1')insert into test(name) values ('张三2')insert into test(name) values ('张三3')insert into test(name) values ('张三4')insert into test(name) values ('张三5')insert into test(name) values ('张三1')insert into test(name) values ('张三2')insert into test(name) values ('张三3')insert into test(name) values ('张三4')insert into test(name) values ('张三5')delete from test where id in (3,5,6)


1 NULL 张三1
2 NULL 张三2
4 NULL 张三4
7 NULL 张三2
8 NULL 张三3
9 NULL 张三4
10 NULL 张三5

 

update test set orderno =(select aa from ( select  ROW_NUMBER() over (order by id ) as aa ,id  from test c ) b where test.id= b.id) 


1 1 张三1
2 2 张三2
4 3 张三4
7 4 张三2
8 5 张三3
9 6 张三4
10 7 张三5

 

如果数据库不支持row_number 则创建临时表(自增id,   test.id)  然后Update

 

另一个方法 这个自己看着做

select IDENTITY(int,1,1)as xh , name  into #c from test

 

 

 

 

 

 

 

2楼bonlog5天前 14:20
改成自增不成吗?
1楼liyangfd5天前 16:39
[quote=bonlog]改成自增不成吗?[/quote]n前面的ID已经是自增了n这只是其中小的一部分n如果按照某个分类的话自增也解决不了问题n例如班级1 班级2 n每个班的学生排序 ,如果走一个重新排序

热点排行