9 个解决方案
#1
麻烦你给出表结构,数据和结果。
#2
declare @i int
set @i = 0
update 表名 set 序号=@i , @i=@i+1
select * from 表名
#3
select row_number() over(order by newid()) as 编号,* from sysobjects
#4
alter table t
add id int identity(1,1)
--这样?
#5
select top 20 name,id=row_number() over(order by name) from sysobjects
order by name
/*
name id
-------------------------------------------------------------------------------------------------------------------------------- ------------
base_user 1
DigitCount 2
EventNotificationErrorsQueue 3
fn_diagramobjects 4
Now 5
PK__sysdiagrams__719CDDE7 6
PK__test__0519C6AF 7
QueryNotificationErrorsQueue 8
queue_messages_1977058079 9
queue_messages_2009058193 10
queue_messages_2041058307 11
ServiceBrokerQueue 12
sp_alterdiagram 13
sp_creatediagram 14
sp_dropdiagram 15
sp_helpdiagramdefinition 16
sp_helpdiagrams 17
sp_renamediagram 18
sp_test 19
sp_upgraddiagrams 20
(20 row(s) affected)
*/
#6
row_number() over ()
ntile() over()
ntile() over()
#7
更新数据库
declare @i int
set @i = 0
update 表名 set 序号=@i , @i=@i+1 from 表名 where 条件
-------------
不更新数据库,只是查询
select 序号=(select count(1) from 表名 where id<a.id)+1,* from 表名 a
#8
2005
row_number()
2000
臨時表
row_number()
2000
臨時表
#9
2000
alter table t
add id int identity
alter table t
add id int identity
#1
麻烦你给出表结构,数据和结果。
#2
declare @i int
set @i = 0
update 表名 set 序号=@i , @i=@i+1
select * from 表名
#3
select row_number() over(order by newid()) as 编号,* from sysobjects
#4
alter table t
add id int identity(1,1)
--这样?
#5
select top 20 name,id=row_number() over(order by name) from sysobjects
order by name
/*
name id
-------------------------------------------------------------------------------------------------------------------------------- ------------
base_user 1
DigitCount 2
EventNotificationErrorsQueue 3
fn_diagramobjects 4
Now 5
PK__sysdiagrams__719CDDE7 6
PK__test__0519C6AF 7
QueryNotificationErrorsQueue 8
queue_messages_1977058079 9
queue_messages_2009058193 10
queue_messages_2041058307 11
ServiceBrokerQueue 12
sp_alterdiagram 13
sp_creatediagram 14
sp_dropdiagram 15
sp_helpdiagramdefinition 16
sp_helpdiagrams 17
sp_renamediagram 18
sp_test 19
sp_upgraddiagrams 20
(20 row(s) affected)
*/
#6
row_number() over ()
ntile() over()
ntile() over()
#7
更新数据库
declare @i int
set @i = 0
update 表名 set 序号=@i , @i=@i+1 from 表名 where 条件
-------------
不更新数据库,只是查询
select 序号=(select count(1) from 表名 where id<a.id)+1,* from 表名 a
#8
2005
row_number()
2000
臨時表
row_number()
2000
臨時表
#9
2000
alter table t
add id int identity
alter table t
add id int identity