SQLSERVER2000中没有楼主想要的功能,听说SQLSERVER2005里有这样的功能. 在SQLSERVER2000中要实现这样的效果,可以这样试试: 1.将查询结果插入到临时表中,并生成自动增量标识列(即IDENTITY列).语法为: select id = identity(int,1,1),* into #tmp from table id就是#tmp表中的IDENTITY列.然后select * from #tmp. 2.根据查询结果某列中行之间的递增关系来生成序号.注意:此种方法必须保证该列不能有重复值.----创建测试数据 declare @t table(dt datetime) insert @t select '2006-09-01' union all select '2006-09-03' union all select '2006-09-04' union all select '2006-09-06' union all select '2006-09-08' ---方法一 select id = identity(int,1,1),*into #tmp from @t select * from #tmp drop table #tmp ----方法二 select id = (select count(*) from @t where dt <= a.dt), dt from @t a/*结果 id dt ------------------------------------------ 1 2006-09-01 00:00:00.000 2 2006-09-03 00:00:00.000 3 2006-09-04 00:00:00.000 4 2006-09-06 00:00:00.000 5 2006-09-08 00:00:00.000 */
好难啊,麻烦大家再帮我想想还有简单的办法么,谢谢hellowork也谢谢大家
select id = identity(int,1,1),*into #tmp from @t select * from #tmp drop table #tmp
select id = (select count(*) from @t where dt <= a.dt), dt from @t a 不是很难的,你仔细看一下就明白了。
二楼的方法很好,但这种更标准:(也是SQL 2005特有的) select * from (select *,num=row_number() over([order by] column_name) from table_name) as a ) order by 是可选的,用来对column_name 排序,(column 必须是primary or unique)
在SQLSERVER2000中要实现这样的效果,可以这样试试:
1.将查询结果插入到临时表中,并生成自动增量标识列(即IDENTITY列).语法为:
select id = identity(int,1,1),* into #tmp from table
id就是#tmp表中的IDENTITY列.然后select * from #tmp.
2.根据查询结果某列中行之间的递增关系来生成序号.注意:此种方法必须保证该列不能有重复值.----创建测试数据
declare @t table(dt datetime)
insert @t
select '2006-09-01' union all
select '2006-09-03' union all
select '2006-09-04' union all
select '2006-09-06' union all
select '2006-09-08'
---方法一
select id = identity(int,1,1),*into #tmp from @t
select * from #tmp
drop table #tmp
----方法二
select
id = (select count(*) from @t where dt <= a.dt),
dt
from @t a/*结果
id dt
------------------------------------------
1 2006-09-01 00:00:00.000
2 2006-09-03 00:00:00.000
3 2006-09-04 00:00:00.000
4 2006-09-06 00:00:00.000
5 2006-09-08 00:00:00.000
*/
select * from #tmp
drop table #tmp
select
id = (select count(*) from @t where dt <= a.dt),
dt
from @t a
不是很难的,你仔细看一下就明白了。
select * from
(select *,num=row_number() over([order by] column_name)
from table_name) as a
)
order by 是可选的,用来对column_name 排序,(column 必须是primary or unique)
Order by 是一定要有的,上面说错了