2011-09-29 32 views
1

任何人都可以給出一個簡單的Oracle存儲過程示例,用於一次更新兩個表。簡單的oracle存儲過程一次更新兩個表的示例

+1

見:http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_6009.htm –

+0

如果單去指原子(所做的所有更改,否則不會更改)您可以像schurik說的那樣進行更新並拍攝提交;如果單走就意味着一個命令,或單一的單位更新處理,請參閱我的迴應。 –

回答

6
CREATE OR REPLACE PROCEDURE update_2_tables 
IS 
begin 
    update t1 set c1 = 1; 
    update t2 set c1 = 2; 
end; 
/
2

我假設你用c中的值更新表a和b。 這是PL/SQL

create or replace procedure update_one_scan as 
    cursor c is 
    select a.rowid r1, b.rowid r1, c.value_to_get 
    from a join b on (join conditions) 
    join c on (join conditions) 
    where conditions; 
begin 
    for r in c 
    loop 
    update a set col_to_update=r.value_to_get where rowid=r1; 
    update b set col_to_update=r.value_to_get where rowid=r2; 
    end loop; 
end; 

你有源表的單次掃描的優勢。您可以在Oracle SQL中執行此操作,但限制較多(您會在嘗試時看到)。但這可以更快。

是一個UPDATE SELECT語句:

Create or replace Procedure update_select AS 
BEGIN 
    update 
    (select a.col_to_update as c1, b.col_to_update as c2, c.value_to_get v1 
     from a join b on (join conditions) 
      join c on (join conditions) 
     where conditions) 
    set 
    c1 = v1, c2 = v2; 
END; 
+1

ORA-01776:無法通過連接視圖修改多個基本表 –

+0

mmm,可能你是對的,我沒有測試過。我使用了更新選擇語句,但我不知道這些限制。謝謝。 –