修改序号

Newlychen 2013-04-29 09:36:57
表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

...全文
73 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶子 2013-04-29
  • 打赏
  • 举报
回复
如果要更新的话:

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
*/
叶子 2013-04-29
  • 打赏
  • 举报
回复

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
*/

hyrongg 2013-04-29
  • 打赏
  • 举报
回复
你这个还不如直接新增加一个字段是identity 的,这样会自动生成新的顺序

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧