请教一条update的语句

shangu 2012-05-23 09:10:33
比如有个表

字段a(自增长) 字段 b 字段 c
1 null ddd
2 1111 ffff
3 null ffff
4 222 ffff
5 null fff
6 null fff
7 null fff


如何得到如下结果
2 1111 ffff
3 1111 ffff
4 222 ffff
5 222 fff
6 222 fff
7 222 fff


即:字段b开始null的都删除,如果遇到有数值的,则下面的null用上面的数值替代。遇到新数值,同理更换
...全文
160 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
唐诗三百首 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
为什么我一个几万的数据,需要9分钟才算完啊??????????
[/Quote]
升级服务器.
shangu 2012-05-23
  • 打赏
  • 举报
回复
为什么我一个几万的数据,需要9分钟才算完啊??????????
黄_瓜 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

SQL code
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] int,[b] int,[c] varchar(4))
insert [tb]
select 1,null,'ddd' union all
select 2,1111,'ffff' union all
select 3,null,……
[/Quote]

什么情况,打开帖子没人回复的啊



数据量打的时候 我的好像比树果果的快点
黄_瓜 2012-05-23
  • 打赏
  • 举报
回复
--> 测试数据:[T]
if object_id('[T]') is not null drop table [T]
go
create table [T]([a] int,[b] int,[c] varchar(4))
insert [T]
select 1,null,'ddd' union all
select 2,1111,'ffff' union all
select 3,null,'ffff' union all
select 4,222,'ffff' union all
select 5,null,'fff' union all
select 6,null,'fff' union all
select 7,null,'fff'
--------------开始查询--------------------------
update a set b=(select top 1 [b] from [T] where a.a>=a and b is not null order by b) from [T] as a
delete from [T] where b is null
select * from [T]
----------------结果----------------------------
/*
a b c
----------- ----------- ----
2 1111 ffff
3 1111 ffff
4 222 ffff
5 222 fff
6 222 fff
7 222 fff

(6 行受影响)


*/
唐诗三百首 2012-05-23
  • 打赏
  • 举报
回复
应该从算法上减少每次处理的数据量.
shangu 2012-05-23
  • 打赏
  • 举报
回复
数据量很大的时候,效率很慢啊
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

SQL code
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] int,[b] int,[c] varchar(4))
insert [tb]
select 1,null,'ddd' union all
select 2,1111,'ffff' union all
select 3,null,……
[/Quote]

数个V587
百年树人 2012-05-23
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] int,[b] int,[c] varchar(4))
insert [tb]
select 1,null,'ddd' union all
select 2,1111,'ffff' union all
select 3,null,'ffff' union all
select 4,222,'ffff' union all
select 5,null,'fff' union all
select 6,null,'fff' union all
select 7,null,'fff'
go

update t
set b=(select top 1 b from tb where a<t.a and b is not null order by a desc)
from tb t
where b is null
go
delete from tb where b is null
go

select * from tb
/**
a b c
----------- ----------- ----
2 1111 ffff
3 1111 ffff
4 222 ffff
5 222 fff
6 222 fff
7 222 fff

(6 行受影响)
**/
shangu 2012-05-23
  • 打赏
  • 举报
回复
数据量很大的时候,如何提高效率?
shangu 2012-05-23
  • 打赏
  • 举报
回复
原来是没有加索引。。。加了索引,速度快很多了
唐诗三百首 2012-05-23
  • 打赏
  • 举报
回复
数据量多的话,用游标比较好.

create table sh
(a int, b int, c varchar(6))

insert into sh
select 1, null, 'ddd' union all
select 2, 1111, 'ffff' union all
select 3, null, 'ffff' union all
select 4, 222, 'ffff' union all
select 5, null, 'fff' union all
select 6, null, 'fff' union all
select 7, null, 'fff'


-- 建索引
create index ix_sh_a on sh(a)

-- 游标方式更新
declare s cursor for select a,b,c from sh

declare @a int,@b int,@c varchar(6),@x int
open s
fetch next from s into @a,@b,@c
while(@@fetch_status<>-1)
begin
if @b is null
update sh set b=@x where a=@a
if @b is not null
select @x=@b
fetch next from s into @a,@b,@c
end

close s
deallocate s

-- 结果
select * from sh

/*
a b c
----------- ----------- ------
1 NULL ddd
2 1111 ffff
3 1111 ffff
4 222 ffff
5 222 fff
6 222 fff
7 222 fff

(7 row(s) affected)
*/
shangu 2012-05-23
  • 打赏
  • 举报
回复
配置还不错了啊。。。要9分钟啊

各位高人试验了w条数据的情况了么?

22,210

社区成员

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

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