select identity(int,1,1) as 序号, * into #temp from yourtableselect * from #temp

解决方案 »

  1.   

    用identity列或者你在表上加
    alter table tablename add id int identity(1,1)
      

  2.   

    首先谢谢两位
    saucer(思归, MS .NET MVP)的答案能解决我的问题,但第2次执行时会出现临时表已存在的提示,请问怎么解决? happydreamer(小黑-不懂的太多)可能没理解我的意图,我是想在查询出来的结果中出现1,2,3...的序号,如果用id类型的列的话,出来的不一定是从1开始的连续序号
      

  3.   

    select (select sum(1) from yourtable where name<=tem.name) as no,name from yourtable as tem
      

  4.   

    select (select sum(1) from yourtable where name<=tem.name) as no,* from yourtable as tem
      

  5.   

    select identity(int,1,1) as 序号, * into #temp from yourtableselect * from #tempdrop table #temp--如果是在存储过程里不要这句也可以,临时表会被自动删除
      

  6.   

    select @i=0
    select @i=@i+1,* from tablename
      

  7.   

    若有自增字段id
    select (select count(*) from yourtable where id<=tem.id) as no,* from yourtable as tem
      

  8.   

    drop table #temp
    select identity(int,1,1) as 序号, * into #temp from yourtable
    select * from #temp