设为首页 收藏本站
查看: 836|回复: 0

[经验分享] oracle中创建存储过程,以及存储过程中调用游标

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-1-14 08:51:40 | 显示全部楼层 |阅读模式
CREATE OR REPLACE PROCEDURE aa
is
   CURSOR prodCombind_cur IS SELECT * FROM proddata.product_combind_info pci;
   prodCombindInfo prodCombind_cur%ROWTYPE;
   id_marketproduct_info varchar2(32);

   CURSOR combindPackage_cur IS SELECT * FROM proddata.combind_package_info;
   combindPackageInfo combindPackage_cur%ROWTYPE;
   id_package_info varchar2(32);

   tmpid_marketproduct_info varchar2(32);

   --查询包含有要删除的子产品的产品组合
    CURSOR delete_combind_cur(id_sub_product VARCHAR2) IS
       select * from proddata.product_combind_rel pcr where pcr.id_product=id_sub_product;
    delCombindInfo delete_combind_cur%ROWTYPE;

begin
    begin
--marketproduct_info表中已经存在的a57,和a72先删除
     select m.id_marketproduct_info into tmpid_marketproduct_info from  proddata.marketproduct_info m where m.marketproduct_code='MP02000057' and m.is_combined='1';
     --删除市场产品对应的产品组合
     OPEN delete_combind_cur(tmpid_marketproduct_info); --打开游标;
     loop   
         FETCH delete_combind_cur INTO delCombindInfo; --从游标中取值要删除的产品组合的id
                 --首先删除套餐下所有的子产品
                 delete from proddata.combind_package_rel cpr where cpr.id_combind_package_info in (
                     select cpi.id_combind_package_info from proddata.combind_package_info cpi
                     where cpi.id_product_combind_info=delCombindInfo.id_product_combind_info);
                 --在删除套餐
                 delete from proddata.combind_package_info cpi
                     where cpi.id_product_combind_info=delCombindInfo.id_product_combind_info;

                --删除产品组合下的子产品
                delete from proddata.product_combind_rel pcr where pcr.id_product_combind_info=delCombindInfo.id_product_combind_info;
                delete from proddata.marketproduct_rule_param mrp where mrp.id_marketproduct_info= delCombindInfo.id_product_combind_info;
                delete from proddata.marketproduct_department md where md.id_marketproduct_info=delCombindInfo.id_product_combind_info;
                delete from proddata.product_combind_info pci where pci.id_product_combind_info=delCombindInfo.id_product_combind_info;
         exit when delete_combind_cur%notfound;  
     end loop;
     close delete_combind_cur; --关闭游标
     delete from proddata.marketproduct_rule_param mr where mr.id_marketproduct_info= tmpid_marketproduct_info;
     delete from proddata.marketproduct_department md where md.id_marketproduct_info=tmpid_marketproduct_info;
     delete from proddata.marketproduct_plan_duty_rel mpdr where mpdr.id_marketproduct_info=tmpid_marketproduct_info;
     delete from proddata.marketproduct_info m where m.id_marketproduct_info=tmpid_marketproduct_info;  
     --
     select m.id_marketproduct_info into tmpid_marketproduct_info from  proddata.marketproduct_info m  where m.marketproduct_code='MP02000072' and m.is_combined='1';
     --删除市场产品对应的产品组合
     OPEN delete_combind_cur(tmpid_marketproduct_info); --打开游标;
     loop   
         FETCH delete_combind_cur INTO delCombindInfo; --从游标中取值
                  --首先删除套餐下所有的子产品
                 delete from proddata.combind_package_rel cpr where cpr.id_combind_package_info in (
                     select cpi.id_combind_package_info from proddata.combind_package_info cpi
                     where cpi.id_product_combind_info=delCombindInfo.id_product_combind_info);
                 --在删除套餐
                 delete from proddata.combind_package_info cpi
                     where cpi.id_product_combind_info=delCombindInfo.id_product_combind_info;
                --删除产品组合下的子产品
                delete from proddata.product_combind_rel pcr where pcr.id_product_combind_info=delCombindInfo.id_product_combind_info;
                delete from proddata.marketproduct_rule_param mrp where mrp.id_marketproduct_info= delCombindInfo.id_product_combind_info;
                delete from proddata.marketproduct_department md where md.id_marketproduct_info=delCombindInfo.id_product_combind_info;
                delete from proddata.product_combind_info pci where pci.id_product_combind_info=delCombindInfo.id_product_combind_info;
         exit when delete_combind_cur%notfound;  
     end loop;
     close delete_combind_cur; --关闭游标
     delete from proddata.marketproduct_rule_param mr where mr.id_marketproduct_info= tmpid_marketproduct_info;
     delete from proddata.marketproduct_department md where md.id_marketproduct_info=tmpid_marketproduct_info;
     delete from proddata.marketproduct_plan_duty_rel mpdr where mpdr.id_marketproduct_info=tmpid_marketproduct_info;
     delete from proddata.marketproduct_info m where m.id_marketproduct_info=tmpid_marketproduct_info;
     --
     commit;
     exception
        when others then
           dbms_output.put_line('删除a57和a72产品时,出现异常');
     end;

--需要处理的表:将表product_combind_info中的数据,copy到表marketproduct_info中;
for prodCombindInfo in prodCombind_cur loop
    begin
       select m.id_marketproduct_info into id_marketproduct_info from proddata.marketproduct_info m
         where m.id_marketproduct_info = prodCombindInfo.id_product_combind_info;
       dbms_output.put_line('product已经存在的:'|| prodCombindInfo.marketproduct_code ||'-'|| prodCombindInfo.MARKETPRODUCT_NAME);
    exception
         --这里抛出异常,是表示上面的select没有查询到记录引起的
         when others then
           dbms_output.put_line('product不存在的:'|| prodCombindInfo.marketproduct_code||'-'||prodCombindInfo.marketproduct_name);
           insert into proddata.marketproduct_info
             (CREATED_BY,
              CREATED_DATE,
              UPDATED_BY,
              UPDATED_DATE,
              marketproduct_code,
              marketproduct_name,
              product_class,
              status,
              is_combined,
              id_marketproduct_info,
              department_code,
              version,
              policy_type)
           values
             (prodCombindInfo.CREATED_BY,
              prodCombindInfo.CREATED_DATE,
              prodCombindInfo.UPDATED_BY,
              prodCombindInfo.UPDATED_DATE,
              prodCombindInfo.marketproduct_code,
              prodCombindInfo.marketproduct_name,
              prodCombindInfo.MAIN_PRODUCT_CLASS,
              prodCombindInfo.status,
              '1',
              prodCombindInfo.ID_PRODUCT_COMBIND_INFO,
              prodCombindInfo.DEPARTMENT_CODE,
              prodCombindInfo.version,
              prodCombindInfo.POLICY_TYPE);
    end;
end loop;
commit;
--需要处理的表:将表combind_package_info中的数据,copy到表package_info中
for combindPackageInfo in combindPackage_cur loop
    begin
        select p.id_package_info into id_package_info from  proddata.package_info p where p.id_package_info=combindPackageInfo.ID_COMBIND_PACKAGE_INFO;
         dbms_output.put_line('package已经存在的:'|| combindPackageInfo.PACKAGE_CODE||'-'||combindPackageInfo.PACKAGE_NAME);
    exception
         --这里抛出异常,是表示上面的select没有查询到记录引起的
         when others then
            insert into  proddata.package_info
               (ID_PACKAGE_INFO,
                ID_MARKETPRODUCT_INFO,
                PACKAGE_CODE,
                PACKAGE_NAME,
                CREATED_BY,
                CREATED_DATE,
                UPDATED_BY,
                UPDATED_DATE,
                status)
             values
               (combindPackageInfo.ID_COMBIND_PACKAGE_INFO,
                combindPackageInfo.ID_PRODUCT_COMBIND_INFO,
                combindPackageInfo.PACKAGE_CODE,
                combindPackageInfo.PACKAGE_NAME,
                combindPackageInfo.CREATED_BY,
                combindPackageInfo.CREATED_DATE,
                combindPackageInfo.UPDATED_BY,
                combindPackageInfo.UPDATED_DATE,
                '1');
            dbms_output.put_line('package不存在的:'|| combindPackageInfo.PACKAGE_CODE||'-'||combindPackageInfo.PACKAGE_NAME);
    end;
end loop;
  commit;
end;


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-40741-1-1.html 上篇帖子: Windows下启动Oracle时的OracleDBConsoleorcl服务无法启动解决方法 下篇帖子: Linux 免安装oracle客户端软件 连接 oracle数据库 oracle
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表