oracle 存储过程 游标

现有表结构完全相同的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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值