查询时能否得到每一行的行号(标识第几行)?

eNicYao 2003-11-24 11:39:02
如题~谢谢~
...全文
50 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-11-24
  • 打赏
  • 举报
回复
select (select count(*) from 表 where 你的可以标识一条记录的主键<=tem.你的可以标识一条记录的主键) rowid,* from 表 tem
pengdali 2003-11-24
  • 打赏
  • 举报
回复
select (select count(*) from 表 where id<=tem.id) rowid,* from 表 tem
pengdali 2003-11-24
  • 打赏
  • 举报
回复
内存中:
declare @表变量(iid int identity,其他列..)
insert @表变量 (其他列) select * 你的表

select * from @表变量
pbsql 2003-11-24
  • 打赏
  • 举报
回复
必须插入临时表
eNicYao 2003-11-24
  • 打赏
  • 举报
回复
谢谢,必须插入临时表吗?直接在内存中可以实现吗?
friendliu 2003-11-24
  • 打赏
  • 举报
回复
select identity(int,1,1) id,* into #temp from tablename
select * froom @temp where id=??
friendliu 2003-11-24
  • 打赏
  • 举报
回复
如果你的行有自增长的IDENTITY型的数据就可以得到。
如果没有的话,就得增加这样的一个字段才能得到第几行

txlicenhe 2003-11-24
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2437/2437014.xml?temp=.67857
交流]自增号

1: 自增列 类型为:int identity(1,1) 当然也可以是bigint,smallint
eg: create table tbName(id int identity(1,1),description varchar(20))
或在用企业管理器设计表字段时,将字段设为int,将标识设为是,其它用默认即可

2: 查询时加序号:
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
shuiniu 2003-11-24
  • 打赏
  • 举报
回复
select identity(int) 行号,* into #temp from 表

select * from #temp
pbsql 2003-11-24
  • 打赏
  • 举报
回复
Select identity(int,1,1) id,* into #tem from table1

select * from #tem

drop table #tem

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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