表A
Name 为主键
ID        Name
A0101     一次风机
A0201     一次风机
A0301     一次风机
A0201     二次风机
A0301     二次风机
A0401     三次风机
A0501     水泵结果
ID          Name
A0101     一次风机1
A0201     一次风机2
A0301     一次风机3
A0201     二次风机2
A0301     二次风机3
A0401     三次风机
A0501     水泵即Name字段重复的用ID中的第三位来标识

解决方案 »

  1.   

    declare @t table(ID varchar(10),Name varchar(10))
    insert into @t select 'A0101','一次风机'
    insert into @t select 'A0201','一次风机'
    insert into @t select 'A0301','一次风机'
    insert into @t select 'A0201','二次风机'
    insert into @t select 'A0301','二次风机'
    insert into @t select 'A0401','三次风机'
    insert into @t select 'A0501','水泵'update t 
    set
        Name=t.Name+substring(ID,3,1)
    from 
        @t t
    where
        exists(select 1 from @t where Name=t.Name and ID!=t.ID) select * from @t
    /*
    ID         Name       
    ---------- ---------- 
    A0101      一次风机1
    A0201      一次风机2
    A0301      一次风机3
    A0201      二次风机2
    A0301      二次风机3
    A0401      三次风机
    A0501      水泵
    */
      

  2.   

    ID为主键吧?update t1
    set Name = Name + substring(id,3,1)
    from 表A t1
    where exists (select ID from 表A where Name = t1.Name)
      

  3.   

    --修改一下update t1
    set Name = Name + substring(id,3,1)
    from 表A t1
    where exists (select ID from 表A where Name = t1.Name and id <> t1.id)
      

  4.   

    --这样?
    declare @t table(ID varchar(10),Name varchar(20))
    insert into @t select 'A0101'     ,'一次风机'
    union all select 'A0201'     ,'一次风机'
    union all select 'A0301'     ,'一次风机'
    union all select 'A0201'    ,'二次风机'
    union all select 'A0301'    , '二次风机'
    union all select 'A0401'    , '三次风机'
    union all select 'A0501'    , '水泵'
    union all select 'A1501'    , '水泵'
    union all select 'A1501'    , '水泵'update @t set Name=Name+cast(cast(substring(ID,2,2) as int) as varchar) from @t a where exists(select 1 from @t where id<>a.id and name=a.name)
    select * from @t
      

  5.   

    update 表A set name=name+substring(id,3,1) from 表A
    where name not in
    (select name from 表A group by name having count(name)=1)
      

  6.   

    update 表A set Name=Name+cast(substring(ID,3,1) as varchar(1)) where exists(select 1 from 表A where Name=表A.Name and ID!=表A.ID)