如何给每行数据加上序号??

wxylvmnn 2016-07-27 01:34:32
我现在有个百万级的表,
表里有个字段ROWNUM,INT型,默认是0

现在根据条件,进行筛选,
比如说,满足条件的数据有20W件。

问题,
如何快速的,对这20W条数据进行加编号?
就是对ROWNUM字段进行更新。

1
2
3
4
...
200000
这样的,对20W条进行更新。


现在我的做法是游标。。。效率有点。。。。

...全文
894 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
卖水果的net 版主 2016-07-27
  • 打赏
  • 举报
回复
引用 3 楼 wxylvmnn 的回复:
弱弱问句,把这个* 改成ROWNUM,没问题吧?
可以的;
wxylvmnn 2016-07-27
  • 打赏
  • 举报
回复
引用 2 楼 u013189255 的回复:
列名都叫rownum了,都不知道row_bumber() over()函数吗
造物主在胃中 2016-07-27
  • 打赏
  • 举报
回复
over 里面order by 不用任何排序的时候可以 order by (select 1)
wxylvmnn 2016-07-27
  • 打赏
  • 举报
回复
引用 1 楼 wmxcn2000 的回复:

create table test(id int, name varchar(30))
go
insert into test(name) select top 10 name from sysobjects 
go
select * from test 
go
with m as (
select ROW_NUMBER() over(order by getdate()) rn , * from test 
)
update m set id = rn 
go
select * from test 
go
drop table test 
go

(10 行受影响)
id          name
----------- ------------------------------
NULL        sysrscols
NULL        sysrowsets
NULL        sysallocunits
NULL        sysfiles1
NULL        syspriorities
NULL        sysfgfrag
NULL        sysphfg
NULL        sysprufiles
NULL        sysftinds
NULL        sysowners

(10 行受影响)


(10 行受影响)
id          name
----------- ------------------------------
1           sysrscols
2           sysrowsets
3           sysallocunits
4           sysfiles1
5           syspriorities
6           sysfgfrag
7           sysphfg
8           sysprufiles
9           sysftinds
10          sysowners

(10 行受影响)


弱弱问句,把这个* 改成ROWNUM,没问题吧?
造物主在胃中 2016-07-27
  • 打赏
  • 举报
回复
列名都叫rownum了,都不知道row_bumber() over()函数吗
卖水果的net 版主 2016-07-27
  • 打赏
  • 举报
回复

create table test(id int, name varchar(30))
go
insert into test(name) select top 10 name from sysobjects 
go
select * from test 
go
with m as (
select ROW_NUMBER() over(order by getdate()) rn , * from test 
)
update m set id = rn 
go
select * from test 
go
drop table test 
go

(10 行受影响)
id          name
----------- ------------------------------
NULL        sysrscols
NULL        sysrowsets
NULL        sysallocunits
NULL        sysfiles1
NULL        syspriorities
NULL        sysfgfrag
NULL        sysphfg
NULL        sysprufiles
NULL        sysftinds
NULL        sysowners

(10 行受影响)


(10 行受影响)
id          name
----------- ------------------------------
1           sysrscols
2           sysrowsets
3           sysallocunits
4           sysfiles1
5           syspriorities
6           sysfgfrag
7           sysphfg
8           sysprufiles
9           sysftinds
10          sysowners

(10 行受影响)


34,576

社区成员

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

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