SQL Server中查询时如何显示行号(不用临时表)?

解决方案 »

  1.   

    SQL Server 2000不支持行号的功能,常用的变通方式就是使用带自增字段的临时表或者表变量。
      

  2.   

    sqlserver裡沒有那樣的語句在表中加一個列id號
    alter table tb add id intdeclare @i int
    set @i=0update tb set @i=@i+1,id=id+@iselect id,* from tb
    order by id
    這個id就是行號
      

  3.   

    update tb set @i=@i+1,id=isnull(id,0)+@i
      

  4.   

    hdhai9451(☆新人类☆) ( ) 信誉:100 
    ==
    学习了
      

  5.   

    update tb set @i=@i+1,id=@i
    就可以啦
      

  6.   

    select *,(select count(*) from tablename where key<=a.key and 查询条件) as 行号
    from tablename a
    where 查询条件
    order by key--条件,必须有排序字段组合,而且这个组合至少在你查询条件限制下是唯一的
      

  7.   

    还是先给@i赋值的好.
    用isnull写好像语句紧凑了一点,但是sql每次都要多做一次运算.