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/*结果
iddt
------------------------------------------
12006-09-01 00:00:00.000
22006-09-03 00:00:00.000
32006-09-04 00:00:00.000
42006-09-06 00:00:00.000
52006-09-08 00:00:00.000
*/
在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/*结果
iddt
------------------------------------------
12006-09-01 00:00:00.000
22006-09-03 00:00:00.000
32006-09-04 00:00:00.000
42006-09-06 00:00:00.000
52006-09-08 00:00:00.000
*/
解决方案 »
- 求解!!vc++与SQL SERVER EXPRESS版的连接问题!
- 怎么讲多个excel表格中的内容汇总到一个excel文件中?请高手指点
- powerdesigner 外键中的格式怎么定义的?正确马上结贴
- 为什么用远程的MSSQL速度会很慢??用同样的程序,本地的就飞快
- 求Oracle 与SQL server 语法、应用、性能和.NET交互等诸多方面的区别
- 如何找出数据库中所占空间大于100M 的表
- 小弟100分求广告系统数据库结构解决方案
- sql语句,怎样判断一个字段中是否存在某一个值
- 为什么从别的数据库导入数据时主键和自增长字段属性丢失
- 怎样在存贮过程中捕捉异常,并继续执行后面的代码?
- 没分帖,求一个数据插入,删除,反复几次的存储过程例子
- 使用临时表的问题
--可以借助临时表处理。create table #t(num int )insert #t select 20
insert #t select 26
insert #t select 45select * from #tselect identity(int,1,1) as id,*
into #tp
from #tselect * from #tpdrop table #t,#tp
select *,ROW_NUMBER() Over(order by id) as rowNum from table_info