那就使用
update #t
set A=(select top 1 A from #t where id<aa.id and a is not null order by id desc)
from #t aa
where A is null
--测试你的数据如下:
create table #T (id int,a int)
insert into #t values(1,1)
insert into #t values(2,null)
insert into #t values(3,null)
insert into #t values(4,null)
insert into #t values(5,2)
insert into #t values(6,null)
insert into #t values(7,null)
insert into #t values(8,null)
update #t
set A=(select top 1 A from #t where id<aa.id and a is not null order by id desc)
from #t aa
where A is null
select * from #t
drop table #t
--结果:
id a
----------- -----------
1 1
2 1
3 1
4 1
5 2
6 2
7 2
8 2
insert into #t values(1,1)
insert into #t values(2,null)
insert into #t values(3,null)
insert into #t values(4,null)
insert into #t values(5,2)
insert into #t values(6,null)
insert into #t values(7,null)
insert into #t values(8,null)
update aa set a=(select top 1 a from #t where id<aa.id order by id desc)
from #t aa
where a is null