更新表中记录为空的记录

sql20070907 2008-06-04 11:00:28
有一个表中有多条记录,但部分字段的数据为空,现想通过SQL语句将记录为空的记录更新为前一条或后一条非空记录的数据,怎么实现?

如:

create table TestEnvir
(ID smallint not null, Data decimal(4, 0))
insert into TestEnvir
select 1, 0 union all select 2, 1203 union all select 3, 0
union all select 4, 1344 union all select 5, 1988
union all select 6, 0 union all select 7, 1098

查询得到的记录如下:
ID Data
1 0
2 1203
3 0
4 1344
5 1988
6 0
7 1098

想要得到下面的结果:
ID Data
1 1203
2 1203
3 1344
4 1344
5 1988
6 1098
7 1098
...全文
79 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xierfly 2008-06-04
  • 打赏
  • 举报
回复
都是强人啊。学习了
elvis_gao 2008-06-04
  • 打赏
  • 举报
回复
楼上说的很对,改下我的代码
create table TestEnvir
(ID smallint not null, Data decimal(4, 0))
insert into TestEnvir
select 1, 0 union all select 2, 1203 union all select 3, 0
union all select 4, 1344 union all select 5, 1988
union all select 6, 0 union all select 7, 0
union all select 11, 0
union all select 25, 1098
union all select 28, 0

update a
set data=(SELECT MAX(B.data) FROM ((select TOP 1 data from testenvir where id<a.id+1 and data<>0 order by ID DESC)
union (select TOP 1 data from testenvir where id>a.id-1 and data<>0))B)
from testenvir a
where data=0

select * from testenvir

drop table testenvir
竹林听雨2005 2008-06-04
  • 打赏
  • 举报
回复
大家都写了,我就不写了。
指出:
1/2楼的问题:
当最后一条记录为0时,则会更新为NULL。

3/4楼的问题:
如果记录的ID号不连续,那也会产生NULL记录的。
ojuju10 2008-06-04
  • 打赏
  • 举报
回复



create table TestEnvir
(ID smallint not null, Data decimal(4, 0))
insert into TestEnvir
select 1, 0 union all select 2, 1203 union all select 3, 0
union all select 4, 1344 union all select 5, 1988
union all select 6, 0 union all select 7, 1098

select a.id,b.data from testenvir a,testenvir b
where a.data=0 and a.id=b.id-1
union all
select id,data from testenvir
where data<>0
order by id

id data
------ ---------------------------------------
1 1203
2 1203
3 1344
4 1344
5 1988
6 1098
7 1098

(7 行受影响)

elvis_gao 2008-06-04
  • 打赏
  • 举报
回复
借一楼的数据:

create table TestEnvir
(ID smallint not null, Data decimal(4, 0))
insert into TestEnvir
select 1, 0 union all select 2, 1203 union all select 3, 0
union all select 4, 1344 union all select 5, 1988
union all select 6, 0 union all select 7, 1098

select * from TestEnvir
update a
set data=(select max(data) from testenvir where (id=a.id+1 or id=a.id-1) and data<>0)
from testenvir a
where data=0

select * from testenvir

drop table testenvir
yrwx001 2008-06-04
  • 打赏
  • 举报
回复
update TestEnvir
set Data = (select top 1 B.Data from TestEnvir B where B.ID > TestEnvir.ID AND B.Data <> 0 ORDER BY B.ID)
where TestEnvir.Data = 0
liangCK 2008-06-04
  • 打赏
  • 举报
回复
create table TestEnvir
(ID smallint not null, Data decimal(4, 0))
insert into TestEnvir
select 1, 0 union all select 2, 1203 union all select 3, 0
union all select 4, 1344 union all select 5, 1988
union all select 6, 0 union all select 7, 1098

update a
set data=(select top 1 data from testenvir where id>=a.id and data<>0)
from testenvir a
where data=0

select * from testenvir

drop table testenvir

/*
ID Data
------ ---------------------------------------
1 1203
2 1203
3 1344
4 1344
5 1988
6 1098
7 1098

(7 row(s) affected)
*./
zanyi1986 2008-06-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yrwx001 的回复:]
SQL codeupdate TestEnvir
set Data = (select top 1 B.Data from TestEnvir B where B.ID > TestEnvir.ID AND B.Data <> 0 ORDER BY B.ID)
where TestEnvir.Data = 0
[/Quote]

这个是对的,我也是这样想的,被他抢先一步,郁闷

22,199

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧