现有表结构完全相同的cusA和cusB,现在要将cusA表中的id不存在于cusB中的记录添加到cusB表中,同时,将id相同的记录更新到cusB表:
create or replace procedure test0720(O_RET OUT NUMBER,O_RESULTMSG OUT VARCHAR2) is
cursor a_list is select a.id,a.name,a.address from cusA a where a.id not in(select c.id from cusA c,cusB b where c.id = b.id);
cursor b_list is select a.id,a.name,a.address from cusA a ,cusB b where a.id = b.id;
begin
O_RET := 0;
O_RESULTMSG := '操作完毕,无错误';
for blist in b_list loop
update cusB b set b.name = alist.name,b.address = alist.address where b.id = blist.id;
commit;
end loop;
for alist in a_list loop
insert into cusB values(alist.id,alist.name,alist.address);
commit;
end loop;
exception
when others then
rollback;
DBMS_OUTPUT.PUT_LINE('执行错误,原因:' || SQLCODE || ', ' ||
SUBSTR(SQLERRM, 1, 90) || '.');
O_RET := 1;
O_RESULTMSG := '操作失败';
end test0720;