表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中的第三位来标识
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中的第三位来标识
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 水泵
*/
set Name = Name + substring(id,3,1)
from 表A t1
where exists (select ID from 表A where Name = t1.Name)
set Name = Name + substring(id,3,1)
from 表A t1
where exists (select ID from 表A where Name = t1.Name and id <> t1.id)
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
where name not in
(select name from 表A group by name having count(name)=1)