Could anyone give a simple example of an Oracle stored procedure for updating two tab开发者_开发百科les in a single go.
CREATE OR REPLACE PROCEDURE update_2_tables
IS
begin
update t1 set c1 = 1;
update t2 set c1 = 2;
end;
/
I supposed that you update tables a and b with values from c. This is 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;
You have the advantage of single scan of source tables.
UPDATE: You can do it even in oracle SQL, but is more restrictive(you'll see when you try). But this can be faster.
Is a UPDATE SELECT statement:
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;
精彩评论