27,579
社区成员
发帖
与我相关
我的任务
分享
/*******作者:正在初学的菜鸟*******/
declare @t table (name char(1),code varchar(5))
insert into @t
select 'q','sad' union all
select 'q','sad' union all
select 'q','sad' union all
select 'f','ddd' union all
select 'f','ddd' union all
select 'f','ddd' union all
select 'g','gt' union all
select 'g','gt' union all
select 'g','gt' union all
select 'b','' union all
select 'x',null
declare @q int,@name char(1)
set @q=0
update @t set
@q=(case when (@name is null or @name<>name) and code is not null and code <>'' then @q+1 else @q end),
@name=name,
code=(case when code is not null and code <>'' then 'T'+right('000'+cast(@q as varchar(4)),4) else code end)
select * from @t
/*输出结果
q T0001
q T0001
q T0001
f T0002
f T0002
f T0002
g T0003
g T0003
g T0003
b
x NULL
*/