对不起,下面的才是对的,试过了: SELECT ID=ROW_NUMBER() OVER (order BY GETDATE()),* FROM 表名
2000版的: select id=(select count(*) from 表 where 主键 <=a.主键),* from 表 a
子查询吧SELECT *,(SELECT COUNT(1) FROM TB WHERE ID<=T.ID) FROM TB T
没ID字段呀 我要 as 个虚拟的往后排序
2000用临时表算了 select identity(int,1,1) as id,* into #tb from tb
if object_id('[tb]') is not null drop table [tb] go create table [tb]([col] varchar(1)) insert [tb] select 'a' union all select 'b' union all select 'c' union all select 'd' union all select 'e' union all select 'f'--子查询 select id=(select count(1)+1 from tb where col<t.col),* from tb t /** id col ----------- ---- 1 a 2 b 3 c 4 d 5 e 6 f(6 行受影响) **/--临时表 select id=identity(int,1,1),* into # from tb order by col go select * from # /** id col ----------- ---- 1 a 2 b 3 c 4 d 5 e 6 f(6 行受影响) **/ drop table #
SELECT ID=ROW_NUMBER() OVER (order BY GETDATE()),* FROM 表名
select id=(select count(*) from 表 where 主键 <=a.主键),* from 表 a
没ID字段呀
我要 as 个虚拟的往后排序
select identity(int,1,1) as id,* into #tb from tb
go
create table [tb]([col] varchar(1))
insert [tb]
select 'a' union all
select 'b' union all
select 'c' union all
select 'd' union all
select 'e' union all
select 'f'--子查询
select id=(select count(1)+1 from tb where col<t.col),*
from tb t
/**
id col
----------- ----
1 a
2 b
3 c
4 d
5 e
6 f(6 行受影响)
**/--临时表
select id=identity(int,1,1),* into # from tb order by col
go
select * from #
/**
id col
----------- ----
1 a
2 b
3 c
4 d
5 e
6 f(6 行受影响)
**/
drop table #