Re: [SQL ] Oracle update 問題...
※ 引述《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
討論串 (同標題文章)