码迷,mamicode.com
首页 > 数据库 > 详细

Oracle--SQL程序优化案例一

时间:2017-02-16 11:13:15      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:ora-01403   end   har   pen   number   type   arch   char   行数据   

下面是存储过程的一部分程序:  

PROCEDURE SAP_MAN_ROUTING_SO (CITEM_ID    VARCHAR2,
                                 CSITE_ID    VARCHAR2,
                                 CTYPE       VARCHAR2)
   IS
      V_ROUTING   VARCHAR2 (40);
        BEGIN                                 
      IF (CITEM_ID NOT LIKE ‘%C%‘)
      THEN
         SELECT NVL (TRIM (RR.ROUTING_ID), ‘‘)
           INTO V_ROUTING
           FROM (SELECT MAPL.PLNNR || ‘_‘ || MAPL.WERKS ROUTING_ID
                   FROM SAP_MAPL_QMCUSTOMER MAPL
                  WHERE     MAPL.MATNR = CITEM_ID
                        AND MAPL.WERKS = CSITE_ID
                        AND EXISTS
                               (SELECT NULL
                                  FROM SAP_MAPL_QMCUSTOMER
                                 WHERE     SAP_MAPL_QMCUSTOMER.MATNR =
                                              CITEM_ID
                                       AND SAP_MAPL_QMCUSTOMER.WERKS =
                                              CSITE_ID)) RR;
      ELSIF (CITEM_ID LIKE ‘%C%‘)
      THEN
         SELECT NVL (TRIM (RR.ROUTING_ID), ‘‘)
           INTO V_ROUTING
           FROM (SELECT MAX (MAPL.PLNNR) ROUTING_ID
                   FROM SAP_MAPL_QMCUSTOMER MAPL
                  WHERE MAPL.MATNR = CITEM_ID AND MAPL.WERKS = CSITE_ID) RR;
      END IF;
 从代码上来看,改程序似乎没有什么缺陷,执行也相当顺畅,但有一天突然一直报

ORA-01403: 未找到任何数据ORA-06512: 在 "STG.SAP_SO_BOM_PROC", line 428
ORA-06512: 在 "STG.SAP_SO_BOM_PROC", line 1097

通过分析发现缺少对结果集行数做一个判断,如果返回的是0行数据,那就是没有值,没有值就会报错,

所以以下是对该程序进行判断:

  PROCEDURE SAP_MAN_ROUTING_SO (CITEM_ID    VARCHAR2,
                                 CSITE_ID    VARCHAR2,
                                 CTYPE       VARCHAR2)
   IS
      V_ROUTING   VARCHAR2 (40);
      qty         number;
     BEGIN
       select count(*) into qty          
           FROM (SELECT MAPL.PLNNR || ‘_‘ || MAPL.WERKS ROUTING_ID
                   FROM SAP_MAPL_QMCUSTOMER MAPL
                  WHERE     MAPL.MATNR = CITEM_ID
                        AND MAPL.WERKS = CSITE_ID
                        AND EXISTS
                               (SELECT NULL
                                  FROM SAP_MAPL_QMCUSTOMER
                                 WHERE     SAP_MAPL_QMCUSTOMER.MATNR =
                                              CITEM_ID
                                       AND SAP_MAPL_QMCUSTOMER.WERKS =
                                              CSITE_ID)) RR;
      IF qty>0  then                                       
      IF (CITEM_ID NOT LIKE ‘%C%‘)
      THEN
         SELECT NVL (TRIM (RR.ROUTING_ID), ‘‘)
           INTO V_ROUTING
           FROM (SELECT MAPL.PLNNR || ‘_‘ || MAPL.WERKS ROUTING_ID
                   FROM SAP_MAPL_QMCUSTOMER MAPL
                  WHERE     MAPL.MATNR = CITEM_ID
                        AND MAPL.WERKS = CSITE_ID
                        AND EXISTS
                               (SELECT NULL
                                  FROM SAP_MAPL_QMCUSTOMER
                                 WHERE     SAP_MAPL_QMCUSTOMER.MATNR =
                                              CITEM_ID
                                       AND SAP_MAPL_QMCUSTOMER.WERKS =
                                              CSITE_ID)) RR;
      ELSIF (CITEM_ID LIKE ‘%C%‘)
      THEN
         SELECT NVL (TRIM (RR.ROUTING_ID), ‘‘)
           INTO V_ROUTING
           FROM (SELECT MAX (MAPL.PLNNR) ROUTING_ID
                   FROM SAP_MAPL_QMCUSTOMER MAPL
                  WHERE MAPL.MATNR = CITEM_ID AND MAPL.WERKS = CSITE_ID) RR;
      END IF;
      ELSE
        --程序漏洞,没有考虑返回值为0行的结果,现加判断参数qty作为返回行数的总数,大于1将执行原先的程序,否则直接赋空值 add by zhangguipeng20170216
        V_ROUTING:=‘‘;
        END IF;

 

Oracle--SQL程序优化案例一

标签:ora-01403   end   har   pen   number   type   arch   char   行数据   

原文地址:http://www.cnblogs.com/guipeng/p/6404218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!