如何在查询结果前增加一列序号?

无尽空虚 2003-12-09 11:54:04
这个select怎么写?
...全文
65 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-12-09
  • 打赏
  • 举报
回复
如果没有的话,就只能用临时表.

select 序号=IDENTITY(int, 1,1),* into #t from 表
select * from #t
zjcxc 元老 2003-12-09
  • 打赏
  • 举报
回复
--如果你的表中有主键,可以用:

select 序号=(select sum(1) from 表 where 主键<=a.主键)
,*
from 表 a
order by 主键



wcnh20 2003-12-09
  • 打赏
  • 举报
回复
select IDENTITY(int, 1,1) AS ID_Num,* into #temp from 表
select * from #temp
dlpseeyou 2003-12-09
  • 打赏
  • 举报
回复
select id=identity(int,1,1) ,* into #ccc from 表名
select * from #ccc
pengdali 2003-12-09
  • 打赏
  • 举报
回复
select IDENTITY(int, 1,1) AS ID_Num,* into #temp from 表
select * from #temp
victorycyz 2003-12-09
  • 打赏
  • 举报
回复
select identity(int,1,1) as id ,* into #t from table
select * from #t

drop table #t
txlicenhe 2003-12-09
  • 打赏
  • 举报
回复
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

34,838

社区成员

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

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