22,209
社区成员
发帖
与我相关
我的任务
分享
if OBJECT_ID('tb') is not null drop table tb
go
create table tb(name nvarchar(10),num int)
insert tb
select
'a', 10 union all select
'b' , 20 union all select
'c' , 3 union all select
'd' , 8
--2005
select *,sn=ROW_NUMBER() over(order by num) from tb
--2000
select *,sn=(select COUNT(1) from tb where num>=a.num) from tb a
order by sn
/*
name num sn
b 20 1
a 10 2
d 8 3
c 3 4*/
--> 测试数据:#TB
--> 我的淘宝:http://shop36766744.taobao.com/
if object_id('tempdb.dbo.TB') is not null drop table #TB
create table TB([name] varchar(1),[num] int)
insert TB
select 'a',10 union all
select 'b',20 union all
select 'c',3 union all
select 'd',8
--2000
select *,ID=(select count(1) from TB where [name]<=t.[name]) from TB t
--2005
select *,ID=Row_number() over(order by [name]) from TB
/*
name num ID
---- ----------- -----------
a 10 1
b 20 2
c 3 3
d 8 4
(4 行受影响)
*/
drop table TB
select name, num, identity(int) as id into #tb from table1
select * from #tb
select name, num, identity(1) as id into #tb from table1
select * from #tb
select name,num,ID=identity(int) into #t from table1
select * from #t
alter table tb add id int identity(1,1)