数据库重复替换 有一个userinfo 表,把name字段有重复的值替换成原先name+[1-n]
比如 name中有重复n个"a" 替换成a1,a2 , a3, a4, a5...an ------最佳解决方案-------------------- ;with cte as
(select *, Case (ROW_NUMBER() over(partition by name order by GetDate())-1) when 0 then Name else Name+rtrim(ROW_NUMBER() over(partition by Name order by GetDate())-1) end as nID from userinfo)
update cte set name=nID ------其他解决方案-------------------- select *,name+rtrim(select row_number() over(partition by name order by getdate())) from userinfo ------其他解决方案-------------------- 不好意思,更正一下,当只要不重复的时候,直接显示原先的值的!要得到结果是
比如 name中有重复n个"a" 替换成a, a1,a2 , a3, a4, a5...an
并更新原来的表 ------其他解决方案-------------------- ;with cte as
(select *, name + CAST( ROW_NUMBER() over(partition by name order by GetDate()) as varchar(5) ) as nID from userinfo)
update cte set name=nID ------其他解决方案-------------------- 可以用一条语句实现吗? ------其他解决方案--------------------