日期:2014-05-17 浏览次数:20464 次
create table jer
(id int, code varchar(10))
insert into jer
select 1, '978777' union all
select 2, '978777' union all
select 3, '978777' union all
select 4, '978775' union all
select 5, '978775' union all
select 6, '978776' union all
select 7, '978776' union all
select 8, '978776' union all
select 9, '978776' union all
select 10, '978776'
-- 更新
update a
set a.code=a.code+case when b.rn=0 then ''
else replicate('0',2-len(b.rn))+rtrim(b.rn) end
from jer a
inner join
(select id,code,row_number() over(partition by code order by id)-1 'rn'
from jer) b on a.id=b.id
-- 结果
select id,code from jer
/*
id code
----------- ----------