如题 我想查出的结果如下
 序号    名称 
  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.   

    如果名称不重复:select (select count(*) from 表 where 名称<=t.名称) as id,t.* from 表 t
      

  2.   

    自增长的貌似也就这样了,或者:declare @b table(名称 char(10))
    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 行)
      

  3.   

    libin_ftsafe(子陌红尘:当libin告别ftsafe) ( ) ----
    如果记录很多的话,效率是很低的!
      

  4.   

    你用的SQL的什么版本,SQL2005新增row_number()函数,可以显示序号
      

  5.   

    其实identity很好的啦
    那试试:
    select no=0,* into #tmptb from tablename
    delcare @i int
    set @i=0
    update #tmptb no=@i,@i=@i+1
    select * from #tmptb--其实效率肯定比identity差不少
      

  6.   

    排名:
    select (select count(*) from 表 where 名称<=t.名称) as id,t.* from 表 t