1,王
4,李
6,黄
想select出一个结果集如下
1,1,王
2,4,李
3,6,黄
也就是产生新的ID
然后再操作这个结果集,怎么实现?

解决方案 »

  1.   

    select
      id=row_number()over(order by getdate()),* 
    from
      tb
      

  2.   

    select id1=(select count(1) from @tb where  id<a.id)+1 , * from @tb a
      

  3.   

    declare @tb table(id int,name varchar(50))
    insert into @Tb select 1,'王'
    insert into @Tb select 4,'李'
    insert into @Tb select 6,'黄'
    select *,row_number() over(order by id) as id2 
    from @tb
    id name id2
    1 王 1
    4 李 2
    6 黄 3
      

  4.   

    --2000select
      id=(select count(1)+1 from TB where id<t.id),*
    from
     TB t
      

  5.   

    --> 测试数据:@tb
    declare @tb table([id] int,[name] varchar(2))
    insert @tb
    select 1,'王' union all
    select 4,'李' union all
    select 6,'黄'select id1=(select count(1) from @tb where  id<a.id)+1 , * from @tb a
    id1         id          name
    ----------- ----------- ----
    1           1           王
    2           4           李
    3           6           黄(3 行受影响)
      

  6.   

    报错row_number,2000和2005还不一样啊,不好意思,新手,下次得先指明了,还是很感谢