数据库 表Test

id 自动增长的主键

name nvarchar(10)

id  name

1    A

2    A

3    A

4    B

5    B

6    C

7    C

把重复的name后面加-1,-2....

如下所示

id  name

1    A

2    A-1

3    A-2

4    B

5    B-1

6    C

7    C-1


select id,name=name+case when no=1 then '' else '-'+rtrim(no-1) end into #AA

from (select *,no=row_number() over(partition by nameorder by id) from test) t

order by id

update test set name=(select name from #AA where id=test.id)