如何通过update语句得到如下结果
例如: 表 test
ID:      name:
1 阿勒
2 悬空
3 酥软
5 andy
6 bean
想要结果: 1 阿勒
2 悬空
3 酥软
4 andy
5 bean

解决方案 »

  1.   

    with cte as
    (
    select rn=row_number()over(order by id),*
    from tb
    )
    update cte 
    set id=rn;
      

  2.   


    --> 测试数据: [test]
    if object_id('[test]') is not null drop table [test]
    create table [test] (ID int,name varchar(4))
    insert into [test]
    select 1,'阿勒' union all
    select 2,'悬空' union all
    select 3,'酥软' union all
    select 5,'andy' union all
    select 6,'bean'declare @i int
    set @i=0
    update test set id=@i,@i=@i+1--测试
    select * from [test]--结果:
    ID          name
    ----------- ----
    1           阿勒
    2           悬空
    3           酥软
    4           andy
    5           bean
      

  3.   


    with cte as 这句话的意思是做什么的?
      

  4.   

    sql server2005新增语法
    你可以理解为一个临时表
      

  5.   

    http://blog.csdn.net/canduecho/archive/2007/11/18/1891602.aspx五楼注意好好看看这个
      

  6.   

    if object_id('test') is not null
    drop table testcreate table test([id] int,[name] varchar(4))
    insert test
    select 1,'阿勒' union all
    select 2,'悬空' union all
    select 3,'酥软' union all
    select 5,'andy' union all
    select 6,'bean'with result_test as
    (
    select rn=row_number()over(order by id),*
    from test
    )
    update result_test  
    set id=rn;select * from test
      

  7.   

    对于cte来说 就是这样的LZ自己试试