作者load111 (稀客)
看板Database
标题Re: [SQL ] Oracle update 问题...
时间Thu Nov 22 00:05:53 2007
※ 引述《cde123 (cde123)》之铭言:
: 现在有这样的资料
: ID col_A
: -----------------
: 1 A
: 2
: 3
: 4
: 5 B
: 6
: 7
: 8
: 9
: 10
: 我希望可以 2,3,4 都填成 A ,6,7,8,9,10 都填成 B 就是往前找值
: 我是用
: update table tb_A set tb_A.col_A = (select tb_B from table tb_B where
: tb_B.ID = tb_A.ID-1) where tb_A.col_A is null
: 可是只做成了...
: ID col_A
: -----------------
: 1 A
: 2 A
: 3
: 4
: 5 B
: 6 B
: 7
: 8
: 9
: 10
: 我用的是 Oracle ,这需要怎样修改呢
id 2~4同时是null,所以以你的sql只有id 2会得到id 1的A
id 3~4只会得到id 2~3的null
id 6~10同理
update table tb_A
set tb_A.col_A = (select max(tb_B.col_A)
from table tb_B
where tb_B.ID <= tb_A.ID
and tb_B.col_A is not null
)
where tb_A.col_A is null
不然就老老实实多keyin一些字,用cursor,一笔一笔update comit ...
declare
cursor cur_a is
select rowid row_id
from table
where col_A is null
order by ID
;
begin
for rec in cur_a loop
update table tb_A
set col_A = (select tb_B.col_A
from table tb_B
where tb_B.ID = tb_A.ID-1
)
where rowid=rec.row_id
;
commit;
end loop;
end;
/
--
http://img222.imageshack.us/img222/2653/1217ur2.jpg
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.216.197.36