如题 我想查出的结果如下
序号 名称
1 a
2 b
3 c
4 d
5 e
-------------------------------------------
其中名称列是原表中有的,序号列是在查询时生成
问题:这个序列怎样在select中生成目前我只想到这个办法
Select no=Identity(int,1,1),* Into #temptable From TableName
Select * From #temptable
Drop Table #temptable请问还有别的招吗?
序号 名称
1 a
2 b
3 c
4 d
5 e
-------------------------------------------
其中名称列是原表中有的,序号列是在查询时生成
问题:这个序列怎样在select中生成目前我只想到这个办法
Select no=Identity(int,1,1),* Into #temptable From TableName
Select * From #temptable
Drop Table #temptable请问还有别的招吗?
insert into @b select 'a' union all
select 'b' union all
select 'c' union all
select 'd' union all
select 'e'
select 序号=(select count(*) from @b a where b.名称>=a.名称),名称 from @b bresult:
序号 名称
----------- ----------
1 a
2 b
3 c
4 d
5 e (所影响的行数为 5 行)
如果记录很多的话,效率是很低的!
那试试:
select no=0,* into #tmptb from tablename
delcare @i int
set @i=0
update #tmptb no=@i,@i=@i+1
select * from #tmptb--其实效率肯定比identity差不少
select (select count(*) from 表 where 名称<=t.名称) as id,t.* from 表 t