如表
id   name
----------
2    ee
2    rr
7    xx
9    ss
17   ww
现在要变成
id   name
----------
1    ee
2    rr
3    xx
4    ss
5    ww请问这个update该怎么写啊?

解决方案 »

  1.   

    create table t(
    id int,
    name varchar(10)
    )
    insert into t
    select 2,'ee'
    union all select 2,'rr'
    union all select 7,'xx'
    union all select 9,'ss'
    union all select 17,'ww'select  identity(int,1,1) as [id],[name] into #t from t select * from #tupdate t set id=a.id from #t a where a.name=t.name
    select * from t
    drop table #t
    drop table t
      

  2.   

    alter table t1 with nocheck add sn int identity(1,1);
    update t1 set id=sn;
    alter table t1 drop column sn;
      

  3.   

    update tab set tab.id = tt.id2
    from
    (
    select id2 = (select count(1) from tab where id < a.id or (id = a.id and name = a.name)),id,name
    from tab a
    )tt
    where tab.id = tt.id and tab.name = tt.name
      

  4.   

    create table t(
    id int,
    name varchar(10)
    )
    insert into t
    select 2,'ee'
    union all select 2,'rr'
    union all select 7,'xx'
    union all select 9,'ss'
    union all select 17,'ww'
    --创建临时表
    select  identity(int,1,1) as [id],[name] into #t from t 
    --更新
    update t set id=a.id from #t a where a.name=t.name
    select * from t
    --删除表
    drop table #t
    drop table t
      

  5.   

    多谢baoshan(石头)
    问题解决