写查询语句的时候增加一个自动编号字段,但表查询的表是没有自动编号字段的不知道可不可以实现

解决方案 »

  1.   

    如果表中没有可以用于排序来确定一条记录顺序的字段,用临时表:select identity(int,1,1) as rowid,* into # from 表select * from #drop table #
      

  2.   

    select id = identity(int,1,1),* into #tmp from tablename
    select * from #tmp
      

  3.   

    在SQL Server 2000中,不支持类似Oracle的rownum功能,只能借助临时表、表变量或者原表中可用参照列的方式来实现。
      

  4.   

    select id=identity(int,1,1),* into #t from tablenameselect * from #t
      

  5.   

    不用臨時表
    select *
    from
    (
    select 
    rowid=(select count(*) from tb  where id<=a.id )
    ,*
    from tb a
    )temp
    order by rowidid 為主鍵
      

  6.   

    select id=identity(int,1,1),* into #t from tablenameselect * from #t
      

  7.   

    --借助临时表处理select id=identity(int,1,1),* 
    into #t 
    from tablenameselect * from #t
      

  8.   

    如果查询本身已经很复杂,建议用临时表的方式,简单一些,但是会增大tempdb的开销。
      

  9.   

    假定有一个复杂的查询(select .... from a,b,(select ... from d) c where ...)如果要生成一个临时表,只需要(注意,复杂查询中不能输出两个或多个同名的列):select identity(int,1,1) as rowid,* into # from (复杂的查询) mselect * from #
      

  10.   

    --借助临时表处理select id=identity(int,1,1),* 
    into #t 
    from (你的查询) as Tselect * from #t
      

  11.   

    不要管什么复杂的查询语句,只要将它插入到#table中就可以了。
      

  12.   

    强烈支持,其中用临时表也不错。添加一个自增字段identity。如果仅仅查询,可以考试使用几个语句:
    declare @i int
    select @i = 0
    select @i=@i+1 ,* from table如果楼主觉得麻烦的话,可以考虑一下Sql server 2005,上面有许多排序专用函数,比如:rank ,dense_rank,row_number,ntile。
      

  13.   

    支持楼主 
      ◢ ██████◣      ◢████◣ 
     ◢◤      ◥◣    ◢◤     ◥◣ 
     ◤        ◥◣  ◢◤        █ 
     ▎   ◢█◣   ◥◣◢◤  ◢█    █ 
     ◣  ◢◤  ◥◣      ◢◣◥◣ ◢◤ 
     ◥██◤  ◢◤         ◥◣ 
           █ ●       ●  █ 
           █ 〃   ▄   〃 █ 
           ◥◣   ╚╩╝   ◢◤    ╰继续.o 
            ◥█▅▃▃ ▃▃▅█◤ 
              ◢◤   ◥◣           <支持支持>! 
              █     █     
             ◢◤▕   ▎◥◣ 
            ▕▃◣◢▅▅▅◣◢▃▕
      

  14.   

    谢谢mengmou()mengmou() ,
    declare @i int
    select @i = 0
    select @i=@i+1 ,* from table
    上面这样写是错误的,应该是更新的时候,才可以用,不好意思呀,汗!
    我基本是用临时表的identity来实现。而转用Sql server 2005后,就直接用row_number来实现顺序列,用rank、dense_rank来实现连续排名或者断开排名,或者和ntile来实现分组排序。