日期:2014-05-17  浏览次数:20744 次

修改序号
表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
*/