id是自动增长          
    id name 
1   1   一
2   3   三
3   4   四
4   7   七
5   8   八
6   9   九
    我想在后面在写一个临时查询列名。结果如下
    id name  Dataid
1   1   一      1  
2   3   三      2
3   4   四      3
4   7   七      4
5   8   八      5
6   9   九      6

解决方案 »

  1.   

    select id,name,(select count(*) from tb where id<=t.id) as dataid
    from tb t
      

  2.   

    select id,name DataId=(select count(1) from [Table] where id<=a.id)
    from [Table] a
      

  3.   

    select a.id,a.name,Dataid=count(b.id) from 表 a,表 b where a.id>=b.id group by a.id,a.name
      

  4.   

    --2005或以上
    select id,name,row_number() over(order by id) as dataid
    from tb
      

  5.   


    select
      a.id,
      a.name,
      dataid =(select count(*) from table where id <= a.id)
    from table a
      

  6.   


    select id,name,id as Dataid from table
      

  7.   

    select *,identity(int,1,1)Dataid into # from tb
    select * from #
    drop table #