2011-09-29 57 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,可能你是对的,我没有测试过。我使用了更新选择语句,但我不知道这些限制。谢谢。 –