update t
set 名称1=(case when t.名称1 is null
then (select top 1 名称1
from 表
where 序号<t.序号
order by 序号 desc)
else t.名称1
end
)
,名称2=(case when t.名称2 is null
then (select top 1 名称2
from 表
where 序号<t.序号
and 名称1=t.名称1
order by 序号 desc)
else t.名称2
end
)
from 表 t
set 名称1=(case when t.名称1 is null
then (select top 1 名称1
from 表
where 序号<t.序号
order by 序号 desc)
else t.名称1
end
)
,名称2=(case when t.名称2 is null
then (select top 1 名称2
from 表
where 序号<t.序号
and 名称1=t.名称1
order by 序号 desc)
else t.名称2
end
)
from 表 t
(
序号 int,
名称1 varchar(10),
名称2 varchar(10),
名称3 varchar(10)
)
insert @tb
select 1 ,'A001' ,NULL ,NULL union
select 2 ,NULL ,'B001' ,NULL union
select 3 ,NULL ,NULL ,'C001' union
select 4 ,NULL ,NULL ,'C002' union
select 5 ,'A002' ,NULL ,NULL union
select 6 ,NULL ,'B002' ,NULL union
select 7 ,NULL ,'B003' ,NULL union
select 8 ,NULL ,NULL ,'C003'--更新
update t
set 名称1=(case when t.名称1 is null
then (select top 1 名称1
from @tb
where 序号<t.序号
and 名称1 is not null
order by 序号 desc)
else t.名称1
end
)
,名称2=(case when t.名称2 is null and t.名称1 is null
then (select top 1 名称2
from @tb
where 序号<t.序号
and 名称2 is not null
order by 序号 desc)
else t.名称2
end
)
,名称3=(case when t.名称3 is null
then (select top 1 名称3
from @tb
where 序号<t.序号
and 名称1=t.名称1
and 名称2=t.名称2
order by 序号 desc)
else t.名称3
end
)
from @tb t--查看
select * from @tb--结果
/*序号 名称1 名称2 名称3
----------- ---------- ---------- ----------
1 A001 NULL NULL
2 A001 B001 NULL
3 A001 B001 C001
4 A001 B001 C002
5 A002 NULL NULL
6 A002 B002 NULL
7 A002 B003 NULL
8 A002 B003 C003(所影响的行数为 8 行)
*/
while @@rowcount>0
begin
update t set 名称1=a1.名称1
from t a1 ,t
where a1.id=t.id-1 and (t.name=null or t.name='')
end
(
序号 int,
名称1 varchar(10),
名称2 varchar(10),
名称3 varchar(10)
)
insert @tb
select 1 ,'A001' ,NULL ,NULL union
select 2 ,NULL ,'B001' ,NULL union
select 3 ,NULL ,NULL ,'C001' union
select 4 ,NULL ,NULL ,'C002' union
select 5 ,'A002' ,NULL ,NULL union
select 6 ,NULL ,'B002' ,NULL union
select 7 ,NULL ,'B003' ,NULL union
select 8 ,NULL ,NULL ,'C003'--更新
update t
set 名称1=(case when t.名称1 is null
then (select top 1 名称1
from @tb
where 序号<t.序号
and 名称1 is not null
order by 序号 desc)
else t.名称1
end
)
,名称2=(case when t.名称2 is null and t.名称1 is null
then (select top 1 名称2
from @tb
where 序号<t.序号
and 名称2 is not null
order by 序号 desc)
else t.名称2
end
)
,名称3=(case when t.名称3 is null and t.名称1 is null
then (select top 1 名称3
from @tb
where 序号<t.序号
and 序号>=(select max(序号) from @tb where 名称1 is null)
and 名称3 is not null
order by 序号 desc)
else t.名称3
end
)
from @tb t--查看
select * from @tb--结果
/*序号 名称1 名称2 名称3
----------- ---------- ---------- ----------
1 A001 NULL NULL
2 A001 B001 NULL
3 A001 B001 C001
4 A001 B001 C002
5 A002 NULL NULL
6 A002 B002 NULL
7 A002 B003 NULL
8 A002 B003 C003(所影响的行数为 8 行)
*/
(
序号 int,
名称1 varchar(10),
名称2 varchar(10),
名称3 varchar(10)
)
insert @tb
select 1 ,'A001' ,NULL ,NULL union
select 2 ,NULL ,'B001' ,NULL union
select 3 ,NULL ,NULL ,'C001' union
select 4 ,NULL ,NULL ,'C002' union
select 5 ,'A002' ,NULL ,NULL union
select 6 ,NULL ,'B002' ,NULL union
select 7 ,NULL ,'B003' ,NULL union
select 8 ,NULL ,NULL ,'C003'declare @name1 varchar(10),@name2 varchar(10),@name3 varchar(10)update @tb
set
@name1 = isnull(名称1,@name1),
@name2 = isnull(名称2,(case when 名称1 is not null then 名称2 else @name2 end)),
@name3 = isnull(名称3,(case when 名称1 is not null then 名称3 else @name3 end)),
名称1 = @name1,
名称2 = @name2,
名称3 = @name3select * from @tb