如何将查询结果集编上序列号?

bonami 2003-11-10 02:45:34
使用SQL2K,希望能在查询结果中添加一个自增长的字段。有没有不使用临时表的方法?
...全文
72 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvltt 2003-11-10
  • 打赏
  • 举报
回复
学习
老宛 2003-11-10
  • 打赏
  • 举报
回复
非常感谢大家,我又学到了一个东西
pengdali 2003-11-10
  • 打赏
  • 举报
回复
select (select count(*) from 表 where 主键<=tem.主键) 行号,* from 表 tem

说明“主键”必须是有顺序的,并且是可以唯一标识一条记录的。
pengdali 2003-11-10
  • 打赏
  • 举报
回复
select (select count(*) from 表 where 主键<=tem.主键) 行号,* from 表 tem
pengdali 2003-11-10
  • 打赏
  • 举报
回复
select IDENTITY(int, 1,1) AS ID_Num,* into #temp from 表
select * from #temp
txlicenhe 2003-11-10
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2437/2437014.xml?temp=.67857
交流]自增号

查询时加序号:
a:没有主键的情形:
Select identity(int,1,1) as iid,* into #tmp from TableName
Select * from #tmp
Drop table #tmp
b:有主键的情形:
Select (Select sum(1) from TableName where KeyField <= a.KeyField) as iid,* from TableName a

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧