日期:2014-05-17 浏览次数:20560 次
UPDATE test1 SET NAME = NAME + CASE WHEN CONVERT(VARCHAR, N.no) ='0' THEN '' ELSE CONVERT(VARCHAR, N.no) END FROM test1 INNER JOIN ( SELECT id , ROW_NUMBER() OVER ( PARTITION BY NAME ORDER BY GETDATE() ) - 1 AS no FROM test1 ) N ON test1.id = N.ID SELECT * FROM test1 /* id Name 1 A 2 A1 3 B1 4 C 5 C1 6 C2 7 D 8 A2 9 D1 10 B*/
------解决方案--------------------
declare @num int
update a set Name=name+case when @num=0 then '' else rtrim(@num) end,
@num=(select count(*) from test1 where name=a.name and id<a.id)
from test1 a