现在数据表中没有ID这个字段,想自己建个虚拟字段,按照1递增往后排序,应该怎么办

解决方案 »

  1.   

    select newid(),* from 表名
      

  2.   

    如果是sql2005及以上的版本,可以用row_number()sql2000用子查询或者临时表加子增列
      

  3.   

    对不起,下面的才是对的,试过了:
    SELECT ID=ROW_NUMBER() OVER (order BY GETDATE()),* FROM  表名
      

  4.   

    2000版的:
    select   id=(select   count(*)   from   表   where   主键 <=a.主键),*   from   表   a
      

  5.   

    子查询吧SELECT *,(SELECT COUNT(1) FROM TB WHERE ID<=T.ID) FROM TB T
      

  6.   


    没ID字段呀 
    我要  as 个虚拟的往后排序
      

  7.   

    2000用临时表算了
    select identity(int,1,1) as id,* into #tb from tb
      

  8.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([col] varchar(1))
    insert [tb]
    select 'a' union all
    select 'b' union all
    select 'c' union all
    select 'd' union all
    select 'e' union all
    select 'f'--子查询
    select id=(select count(1)+1 from tb where col<t.col),*
    from tb t
    /**
    id          col
    ----------- ----
    1           a
    2           b
    3           c
    4           d
    5           e
    6           f(6 行受影响)
    **/--临时表
    select id=identity(int,1,1),* into # from tb order by col
    go
    select * from #
    /**
    id          col
    ----------- ----
    1           a
    2           b
    3           c
    4           d
    5           e
    6           f(6 行受影响)
    **/
    drop table #
      

  9.   

    UP,2K的常用这两种方法处理。
      

  10.   

    http://topic.csdn.net/u/20100705/18/e0be7ec0-16b0-4c71-81bc-f2126bb82ae7.html?seed=1648295254&r=66740338#r_66740338