Oracle 过程抛出 IF ELSE 语句的语法错误

问题描述 投票:0回答:2

此处显示的过程抛出错误:

错误(5718,1):PLS-00103:在预期以下情况之一时遇到符号“ELSE”:

代码:

PROCEDURE GET_FTTX_DATA_BY_MZ_FL
(
  PUSERTYPE IN NVARCHAR2,
  POPERATIONTYPE IN NVARCHAR2,
  PSPANTYPE IN NVARCHAR2,
  PMAINTZONECODE IN VARCHAR2,  
  PSPANDATA OUT SYS_REFCURSOR
)
AS
SQLSTM   VARCHAR2 (2000);
BEGIN 
    IF PSPANTYPE = 'FTTX'  AND POPERATIONTYPE = 'New' THEN
               BEGIN
                    OPEN PSPANDATA FOR
                     
                    SELECT JB.FSA_ID AS FSA_ID,JB.MAINTENANCEZONECODE,JBP.JOB_PROGRESS_ID AS REQ_ID,JB.REOFFERFLAG,
                        MODIFIED_BY AS LAST_UPDATE_BY,SM.STATUS_NAME,
                        TO_DATE(SYSDATE) - TO_DATE(JBP.UMS_GROUP_ASS_TO_DATE) AS PENDING_DAYS
                        FROM TBL_FIBER_INV_JOBS JB
                        INNER JOIN TBL_FIBER_INV_JOB_PROGRESS JBP ON JB.JOB_ID = JBP.JOB_ID
                        INNER JOIN TBL_FIBER_INV_STATUS_MASTER SM ON JBP.STATUS_ID = SM.STATUS_ID
                        WHERE JB.MAINTENANCEZONECODE = PMAINTZONECODE                      
                        AND UMS_GROUP_ASS_TO_NAME NOT IN ('Construction_Engineer','NHQ-PMO')
                   --     AND JBP.APPROVED_BY IS NULL
                        AND JBP.JOB_PROGRESS_FLAG = 0
                       -- AND JBP.APPROV_REJECT_REMARK IS NULL
                        AND JB.SPAN_TYPE= 'FTTX'              
                        AND JBP.APPROVED_BY IS NULL
                        ORDER BY JBP.JOB_PROGRESS_ID DESC;            
                        
ELSE PSPANTYPE = 'FTTX'  AND POPERATIONTYPE = 'Partial' THEN    

          BEGIN                
                
                OPEN PSPANDATA FOR
                     
                    SELECT JB.FSA_ID AS FSA_ID,JB.MAINTENANCEZONECODE,JBP.JOB_PROGRESS_ID AS REQ_ID,JB.REOFFERFLAG,
                        MODIFIED_BY AS LAST_UPDATE_BY,SM.STATUS_NAME,
                        TO_DATE(SYSDATE) - TO_DATE(JBP.UMS_GROUP_ASS_TO_DATE) AS PENDING_DAYS
                        FROM TBL_FIBER_INV_JOBS JB
                        INNER JOIN TBL_FIBER_INV_JOB_PROGRESS JBP ON JB.JOB_ID = JBP.JOB_ID
                        INNER JOIN TBL_FIBER_INV_STATUS_MASTER SM ON JBP.STATUS_ID = SM.STATUS_ID
                        WHERE JB.MAINTENANCEZONECODE = PMAINTZONECODE                      
                        AND UMS_GROUP_ASS_TO_NAME NOT IN ('Construction_Engineer','NHQ-PMO')
                   --     AND JBP.APPROVED_BY IS NULL
                        AND JBP.JOB_PROGRESS_FLAG = 0
                       -- AND JBP.APPROV_REJECT_REMARK IS NULL
                        AND JB.SPAN_TYPE= 'FTTX'     
                        AND JBP.APPROVED_BY IS NULL OR AND JBP.REJECTED_BY IS NULL
                        ORDER BY JBP.JOB_PROGRESS_ID DESC;   
                
          END;   
END;          
   END IF;   
       --  END IF;     
--END;
END GET_FTTX_DATA_BY_MZ_FL;
oracle stored-procedures
2个回答
0
投票

最佳实践是在发布之前通过删除不相关的部分来简化代码。 在您的情况下,只有主要

BEGIN
END
块是相关的。通过用简单的例子替换条件和语句,你会得到

BEGIN 
  IF true THEN
     BEGIN
         NULL;                                  
  ELSE true THEN    
     BEGIN                
       NULL;   
     END;   
END;          
   END IF;   
END;

这显然是错误的,只有你知道如何修复它。


0
投票

在 IF PSPANTYPE = 'FTTX' AND POPERATIONTYPE = 'New' THEN 之后,您打开了新的 pl/sql 块,发出 BEGIN 命令,但该块有 END; ELSE 部分中的命令。在 ELSE 部分中,您还打开了一个新的 pl/sql 块,该块已关闭,但有两个 END;一个接一个的命令是行不通的。
当你开始一个区块时,你必须正确地结束;它 - 在 if 部分中打开的块必须在同一 if 部分中关闭,而不是在其他地方...您根本不需要打开新块(阅读代码中的注释)

PROCEDURE GET_FTTX_DATA_BY_MZ_FL( PUSERTYPE IN NVARCHAR2,
                                  POPERATIONTYPE IN NVARCHAR2,
                                  PSPANTYPE IN NVARCHAR2,
                                  PMAINTZONECODE IN VARCHAR2,  
                                  PSPANDATA OUT SYS_REFCURSOR
                                ) AS
    SQLSTM   VARCHAR2 (2000);
BEGIN 
    IF PSPANTYPE = 'FTTX'  AND POPERATIONTYPE = 'New' THEN
        --BEGIN         <--- delete this row (Begin without End)
            OPEN PSPANDATA FOR
            SELECT  JB.FSA_ID AS FSA_ID,
                    JB.MAINTENANCEZONECODE,
                    JBP.JOB_PROGRESS_ID AS REQ_ID,
                    JB.REOFFERFLAG,
                    MODIFIED_BY AS LAST_UPDATE_BY,
                    SM.STATUS_NAME,
                    TO_DATE(SYSDATE) - TO_DATE(JBP.UMS_GROUP_ASS_TO_DATE) AS PENDING_DAYS
            FROM    TBL_FIBER_INV_JOBS JB
            INNER JOIN TBL_FIBER_INV_JOB_PROGRESS JBP ON JB.JOB_ID = JBP.JOB_ID
            INNER JOIN TBL_FIBER_INV_STATUS_MASTER SM ON JBP.STATUS_ID = SM.STATUS_ID
            WHERE   JB.MAINTENANCEZONECODE = PMAINTZONECODE                      
                    AND UMS_GROUP_ASS_TO_NAME NOT IN ('Construction_Engineer','NHQ-PMO')
           --     AND JBP.APPROVED_BY IS NULL
                    AND JBP.JOB_PROGRESS_FLAG = 0
               -- AND JBP.APPROV_REJECT_REMARK IS NULL
                    AND JB.SPAN_TYPE= 'FTTX'              
                    AND JBP.APPROVED_BY IS NULL
            ORDER BY JBP.JOB_PROGRESS_ID DESC;            
    ELSE PSPANTYPE = 'FTTX'  AND POPERATIONTYPE = 'Partial' THEN 
        --
        --BEGIN         <--- delete this row   (Begin with TWO Ends)     
        --
            OPEN PSPANDATA FOR
                SELECT  JB.FSA_ID AS FSA_ID,
                        JB.MAINTENANCEZONECODE,
                        JBP.JOB_PROGRESS_ID AS REQ_ID,
                        JB.REOFFERFLAG,
                        MODIFIED_BY AS LAST_UPDATE_BY,
                        SM.STATUS_NAME,
                        TO_DATE(SYSDATE) - TO_DATE(JBP.UMS_GROUP_ASS_TO_DATE) AS PENDING_DAYS
                FROM    TBL_FIBER_INV_JOBS JB
                INNER JOIN TBL_FIBER_INV_JOB_PROGRESS JBP ON JB.JOB_ID = JBP.JOB_ID
                INNER JOIN TBL_FIBER_INV_STATUS_MASTER SM ON JBP.STATUS_ID = SM.STATUS_ID
                WHERE   JB.MAINTENANCEZONECODE = PMAINTZONECODE                      
                        AND UMS_GROUP_ASS_TO_NAME NOT IN ('Construction_Engineer','NHQ-PMO')
                --     AND JBP.APPROVED_BY IS NULL
                        AND JBP.JOB_PROGRESS_FLAG = 0
                -- AND JBP.APPROV_REJECT_REMARK IS NULL
                        AND JB.SPAN_TYPE= 'FTTX'     
                        AND JBP.APPROVED_BY IS NULL (OR) (AND) JBP.REJECTED_BY IS NULL  -- is this OR or AND ???
                ORDER BY JBP.JOB_PROGRESS_ID DESC;   
        --
        --END;      <--- delete this row (2 consecutive Ends)
        --END;      <--- delete this row (2 consecutive Ends)
        --
    END IF;   
       --  END IF;     
--END;
END GET_FTTX_DATA_BY_MZ_FL;
  • 未检查没有示例数据的 Select 语句,但存在未定义格式掩码的 TO_DATE() 函数,这可能会导致一些其他问题。
  • 不要使用 TO_DATE(SYSDATE),因为 SYSDATE 已经是 DATE 数据类型。
  • 两个 Select 之间的唯一区别是第二个查询中附加的“OR AND JBP.REJECTED_BY IS NULL” - 是 OR 还是 AND。如果是 OR - 最好重新考虑逻辑,也许将基本 select 定义一次作为变量(根本不使用 SQLSTM),并使用 if 添加或不添加该 OR 部分(分别向两者添加 Order By)。另外,再次检查您是否必须将任何/某些/所有条件放在括号中...如果是 AND,则不需要括号,您仍然可以将基本文本放入变量一次并使用 if 来是否添加其他条件。
    如下所示(未以任何方式测试):
PROCEDURE GET_FTTX_DATA_BY_MZ_FL( PUSERTYPE IN NVARCHAR2,
                                  POPERATIONTYPE IN NVARCHAR2,
                                  PSPANTYPE IN NVARCHAR2,
                                  PMAINTZONECODE IN VARCHAR2,  
                                  PSPANDATA OUT SYS_REFCURSOR
                                ) AS
    SQLSTM   VARCHAR2 (2000) := 'SELECT JB.FSA_ID AS FSA_ID,
                                        JB.MAINTENANCEZONECODE,
                                        JBP.JOB_PROGRESS_ID AS REQ_ID,
                                        JB.REOFFERFLAG,
                                        MODIFIED_BY AS LAST_UPDATE_BY,
                                        SM.STATUS_NAME,
                                        SYSDATE - TO_DATE(JBP.UMS_GROUP_ASS_TO_DATE, ''yyyy-mm-dd'') AS PENDING_DAYS  -- add your own format mask - this is just an example
                                 FROM   TBL_FIBER_INV_JOBS JB
                                 INNER JOIN TBL_FIBER_INV_JOB_PROGRESS JBP ON JB.JOB_ID = JBP.JOB_ID
                                 INNER JOIN TBL_FIBER_INV_STATUS_MASTER SM ON JBP.STATUS_ID = SM.STATUS_ID
                                 WHERE  JB.MAINTENANCEZONECODE = PMAINTZONECODE                      
                                        AND UMS_GROUP_ASS_TO_NAME NOT IN (''Construction_Engineer'',''NHQ-PMO'')
                                        AND JBP.JOB_PROGRESS_FLAG = 0
                                        AND JB.SPAN_TYPE= ''FTTX''              
                                ';
BEGIN 
    IF PSPANTYPE = 'FTTX'  AND POPERATIONTYPE = 'New' THEN
            OPEN PSPANDATA FOR 
                SQLSTM || ' AND JBP.APPROVED_BY IS NULL ORDER BY JBP.JOB_PROGRESS_ID DESC';            
    ELSE PSPANTYPE = 'FTTX'  AND POPERATIONTYPE = 'Partial' THEN 
            OPEN PSPANDATA FOR
                SQLSTM || ' AND ( JBP.APPROVED_BY IS NULL OR JBP.REJECTED_BY IS NULL ) ORDER BY JBP.JOB_PROGRESS_ID DESC';   
    END IF;   
END GET_FTTX_DATA_BY_MZ_FL;
© www.soinside.com 2019 - 2024. All rights reserved.