Oracle--SQL程序优化案例一
Posted Leader.Z
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle--SQL程序优化案例一相关的知识,希望对你有一定的参考价值。
下面是存储过程的一部分程序:
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程序优化案例一的主要内容,如果未能解决你的问题,请参考以下文章
使用 NOT IN(Oracle Sql Developer)的查询性能优化