求一个更新语句

jmu_xiaocai 2010-08-30 11:21:13
396028 0
396028 0
396028 0
214014 0
396028 0

Uid SortIndex

------------------要把这个表更新为

396028 1
396028 2
396028 3
214014 1
396028 4

-
就是uid相同的SortIndex递增
...全文
42 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
百年树人 2010-08-30
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([Uid] int,[SortIndex] int)
insert [tb]
select 396028,0 union all
select 396028,0 union all
select 396028,0 union all
select 214014,0 union all
select 396028,0
go

alter table tb add tmpid int identity(1,1);
go

update t
set SortIndex=(select count(1)+1 from tb where uid=t.uid and tmpid<t.tmpid)
from tb t
go

alter table tb drop column tmpid;
go

select * from tb
/**
Uid SortIndex
----------- -----------
396028 1
396028 2
396028 3
214014 1
396028 4

(5 行受影响)
**/

34,590

社区成员

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

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