一个更新语句

ttyp 2004-07-27 02:09:22
字段A,每条记录中如果是NULL的话,则其内容更新为上条记录的值(第一条除外)有自动ID
...全文
158 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttyp 2004-07-27
  • 打赏
  • 举报
回复
OK
回复人: zhangzs8896(小二) ( ) 信誉:100 2004-07-27 16:01:00 得分: 0
正解
1老玉米1 2004-07-27
  • 打赏
  • 举报
回复
update table set A=(select top 1 A from table where T.id<id order by id desc)
From Table T
where A is null
zhangzs8896 2004-07-27
  • 打赏
  • 举报
回复
那就使用
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
ttyp 2004-07-27
  • 打赏
  • 举报
回复
对,取以前第一个不为空的值
因为别人建立表是一个EXECL表,名称只填写了一个,以后的就省略了,我想这种应用应该有很多的
zhangzs8896 2004-07-27
  • 打赏
  • 举报
回复
update aa 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
wzh1215 2004-07-27
  • 打赏
  • 举报
回复
如果用:
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
---当第一条'a'的值为NULL时那么更新后所有的记录a都为NULL
zhangzs8896 2004-07-27
  • 打赏
  • 举报
回复
楼主的结果是
id a
----------- -----------
1 1
2 1
3 2
4 2
5 NULL

---你的第5条是null,则取第四条记录的值,因为第四条也是null,显然是上面的结果啊。
你想如果null时,取它上面的不为null的值?
ttyp 2004-07-27
  • 打赏
  • 举报
回复
--还有就是数据一多也不行,只能更新最近的一条

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 aa set a=(select top 1 a from #t where id<aa.id order by id desc)
from #t aa
where a is null


select * from #t

drop table #t


--看来只能用游标做了
ttyp 2004-07-27
  • 打赏
  • 举报
回复
结果应该是
id a
----------- -----------
1 1
2 1
3 2
4 2
5 2
ttyp 2004-07-27
  • 打赏
  • 举报
回复
--改为后和楼上一样,可是最后一行仍然为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,2)
insert into #t values(4,null)
insert into #t values(5,null)

--update #t set a=(select top 1 a from #t where id<aa.id order by id) from #t aa where a is 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


select * from #t

drop table #t
ghosthjt 2004-07-27
  • 打赏
  • 举报
回复
update yourtable
set A=(select top 1 a.A from yourtable a where a.id>id order by id)
where A is null
zhangzs8896 2004-07-27
  • 打赏
  • 举报
回复
create table #T (id int,a int)

insert into #t values(1,1)
insert into #t values(2,null)
insert into #t values(3,2)
insert into #t values(4,null)
insert into #t values(5,null)

update #t
set a=(select top 1 a from #t where id=aa.id-1 order by id)
from #t aa
where a is null

select * from #t

drop table #t

--结果

(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 3 行)

id a
----------- -----------
1 1
2 1
3 2
4 2
5 NULL

(所影响的行数为 5 行)
zhangzs8896 2004-07-27
  • 打赏
  • 举报
回复
update #t
set a=(select top 1 a from #t where id=aa.id-1 order by id)
from #t aa
where a is null
ttyp 2004-07-27
  • 打赏
  • 举报
回复
谁来解释下啊~
ttyp 2004-07-27
  • 打赏
  • 举报
回复
很奇怪啊,我也是这么写的啊

create table #T (id int,a int)

insert into #t values(1,1)
insert into #t values(2,null)
insert into #t values(3,2)
insert into #t values(4,null)
insert into #t values(5,null)

update #t set a=(select top 1 b.a from #t b where b.id>id order by id) where a is null
select * from #t
drop table #t
loverpyh 2004-07-27
  • 打赏
  • 举报
回复
update table set A=(select top 1 a.A from yourtable a where a.id<id order by id desc) where A is null
zjcxc 元老 2004-07-27
  • 打赏
  • 举报
回复
update a set 字段A=(select top 1 字段A from 你的表 where id<a.id order by id desc)
from 你的表 a
where 字段A is null
wzh1215 2004-07-27
  • 打赏
  • 举报
回复
update yourtable
set A=(select top 1 a.A from yourtable a where a.id>id order by id)
where A is null

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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