我現在使用merge into
聲明,但它不符合要求。目前這樣的代碼......Oracle 11g - 如何在合併語句中執行多行?
create or replace procedure
mv_data
is
begin
merge into WORK w
using (select * from IF_WORK where batch = 0) i
on (w.token in (select token from IF_WORK where batch = 0))
when matched then
update set
w.area = i.area
, w.cust_id = i.cust_id
, w.status = i.status
, w.changed = sysdate
where w.token = i.token
-- I want to put "update IF_WORK set batch = 1 where work_id = i.work_id" here. Exception handling also.
when not matched then
insert
(
token, area, cust_id, status)
values
(i.token, i.area, i.cust_id, i.status)
-- I want to put "update IF_WORK set batch = 1 where work_id = i.work_id" here. Exception handling also.
;
update IF_WORK set batch = 1;
commit;
exception
when others then rollback;
end;
,並遵循什麼我關心...
- 在當前的代碼,它運行
for
環比更新所有。需要個人控制。 (我必須更新IF_WORK表,只有成功數據) exception
聲明的位置。我想回滾或提交每一行,而不是全部。但是,現在它將全部回滾。
所以我試圖將代碼更改爲一個merge
程序和程序,它處理單行。但我知道不能使用when matched then
的程序。
我該如何執行此操作?有沒有好的陳述/方法? 謝謝。
P.S
因爲我已經和Java一起工作了很長時間,所以我提供的代碼看起來像Java中的後續代碼。
/* This is something I wanted
public static void main(String[] args) {
List<String> aList = new ArrayList<String>();
for(String s : aList){
try{
* INSERT data !!! *
}catch(Exception e) {
* UPDATE data !!! *
}
}
}
*/
// Something I implemented for now
public static void main(String[] args) {
List<String> aList = new ArrayList<String>();
try{
for(String s : aList){
* INSERT data !!! *
}
}catch(Exception e) {
* UPDATE all !!! *
}
}
感謝。這非常有幫助。我把過程放在'loop'裏面的'begin〜end;'塊中,並把異常聲明放到那裏。所以我可以通過行單元處理異常。再次感謝:D –