读书人

批改序号

发布时间: 2013-06-19 10:26:41 作者: rapoo

修改序号
表A
将下列值
a b c
1 ff gg
4 bb cc
7 aa hh

修改为
a b c
1 ff gg
2 bb cc
3 aa hh


[解决办法]
你这个还不如直接新增加一个字段是identity 的,这样会自动生成新的顺序
[解决办法]


declare @表A table (a int,b varchar(2),c varchar(2))
insert into @表A
select 1,'ff','gg' union all
select 4,'bb','cc' union all
select 7,'aa','hh'

;with maco as
(
select row_number() over (order by a) as id,* from @表A
)
select * from maco
/*
id a b c
-------------------- ----------- ---- ----
1 1 ff gg
2 4 bb cc
3 7 aa hh
*/


[解决办法]
如果要更新的话:

declare @表A table (a int,b varchar(2),c varchar(2))
insert into @表A
select 1,'ff','gg' union all
select 4,'bb','cc' union all
select 7,'aa','hh'

;with maco as
(
select row_number() over (order by a) as id,* from @表A
)
update maco set a=id
select * from @表A
/*
a b c
----------- ---- ----
1 ff gg
2 bb cc
3 aa hh
*/

读书人网 >SQL Server

热点推荐