set nocount on
create table tb (f1 varchar(20),f2 varchar(20))
go
insert into tb
select 'B10','E槽位' union all
select null,'F槽寬' union all
select null,'G槽徑' union all
select null,null union all
select null,null union all
select 'B20','去端面釘' union all
select null,'F槽寬' union all
select null,'G槽徑' union all
select null,null union all
select null,null
declare @col varchar(20)
set @col=''
update tb set f1=@col,
@col=case when f1 is null then @col else f1 end
insert t
select 'B10','E槽位' union all
select NULL,'F槽寬' union all
select NULL,'G槽徑' union all
select NULL,NULL union all
select NULL,NULL union all
select 'B20','去端面釘' union all
select NULL,'F槽寬' union all
select NULL,'G槽徑' union all
select NULL,NULL union all
select NULL,NULL
go
alter table t add id int identity(1,1)
go
update t
set F1=(case when a.F1 is null then (select top 1 F1 from t
where id<a.id and F1 is not null
order by id desc) else a.F1 end)
from t a
go
alter table t drop column id
go
select * from t