嵌套 IF 语句的 PL/SQL 错误

Posted

技术标签:

【中文标题】嵌套 IF 语句的 PL/SQL 错误【英文标题】:PL/SQL Error With Nested IF statements 【发布时间】:2013-08-23 18:10:49 【问题描述】:

Apex 4.2

我在下面有以下 pl/sql 代码块,但是每当我在 APex 中运行它时,我都会不断收到错误消息:

ORA-06550:第 35 行,第 4 列:PLS-00103:遇到符号“;”预期以下情况之一时:如果

DECLARE
   show_changes BOOLEAN;
   l_exists INTEGER;

BEGIN

Select count(*) into l_exists
From dba_role_privs
where grantee = upper(:APP_USER) and
(granted_role = 'SURVEY_JOB_SUID' or granted_role = 'SURVEY_SUID');

IF l_exists = 0 THEN            /* unauthorized */
   IF :P32_SURVEY_JOB_REQUEST_ID IS NOT NULL  AND (:P32_SUBMISSION_DATE IS NULL     AND :P32_LOCK_DATE IS NULL) THEN   
           show_changes := TRUE;
   IF :P32_SURVEY_JOB_REQUEST_ID IS NOT NULL AND (:P32_SUBMISSION_DATE IS NOT NULL AND :P32_LOCK_DATE IS NULL) THEN
      show_changes := TRUE;
   IF :P32_SURVEY_JOB_REQUEST_ID IS NOT NULL AND (:P32_SUBMISSION_DATE IS NOT NULL     AND :P32_LOCK_DATE IS NOT NULL) THEN
          show_changes := FALSE;    
END IF;
ELSE                          /* authorized */  
   IF :P32_SURVEY_JOB_REQUEST_ID IS NOT NULL  AND (:P32_SUBMISSION_DATE IS NULL AND :P32_LOCK_DATE IS NULL) THEN   
           show_changes := TRUE;
   IF :P32_SURVEY_JOB_REQUEST_ID IS NOT NULL AND (:P32_SUBMISSION_DATE IS NOT NULL AND :P32_LOCK_DATE IS NULL) THEN
      show_changes := TRUE;
   IF :P32_SURVEY_JOB_REQUEST_ID IS NOT NULL AND (:P32_SUBMISSION_DATE IS NOT NULL AND :P32_LOCK_DATE IS NOT NULL) THEN
      show_changes := TRUE;

END IF;
Return show_changes;
END;

我不确定是什么问题。任何帮助将不胜感激。提前致谢。

【问题讨论】:

IF-条件选择语句的语法错误。请查看PL/SQL Control Statements。 【参考方案1】:

IF 始终必须与 END IF 配对,ELSE 位于 END IF 之前。如果您想在外部 IF 的两个分支中的每个分支中都有三个单独的 IF 语句,那么您需要类似

IF l_exists = 0
THEN
  IF :P32_SURVEY_JOB_REQUEST_ID IS NOT NULL  AND 
    (:P32_SUBMISSION_DATE IS NULL     AND :P32_LOCK_DATE IS NULL) 
  THEN   
           show_changes := TRUE;
  END IF;
  IF :P32_SURVEY_JOB_REQUEST_ID IS NOT NULL AND 
     (:P32_SUBMISSION_DATE IS NOT NULL AND :P32_LOCK_DATE IS NULL) 
  THEN
      show_changes := TRUE;
  END IF;
  IF :P32_SURVEY_JOB_REQUEST_ID IS NOT NULL AND 
     (:P32_SUBMISSION_DATE IS NOT NULL     AND :P32_LOCK_DATE IS NOT NULL) 
  THEN
          show_changes := FALSE;    
  END IF;
ELSE
  <<repeat the pattern>>
END IF;

不过,我的猜测是,您真的不希望在每个分支中有三个单独的 IF 语句。我的猜测是你想要一个带有ELSIFIF 语句

IF l_exists = 0
THEN
  IF :P32_SURVEY_JOB_REQUEST_ID IS NOT NULL AND 
    (:P32_SUBMISSION_DATE IS NULL     AND :P32_LOCK_DATE IS NULL) 
  THEN   
           show_changes := TRUE;
  ELSIF :P32_SURVEY_JOB_REQUEST_ID IS NOT NULL AND 
       (:P32_SUBMISSION_DATE IS NOT NULL AND :P32_LOCK_DATE IS NULL) 
  THEN
      show_changes := TRUE;
  ELSIF :P32_SURVEY_JOB_REQUEST_ID IS NOT NULL AND 
       (:P32_SUBMISSION_DATE IS NOT NULL     AND :P32_LOCK_DATE IS NOT NULL) 
  THEN
          show_changes := FALSE;    
  END IF;
ELSE
  <<repeat the pattern>>
END IF;

【讨论】:

以上是关于嵌套 IF 语句的 PL/SQL 错误的主要内容,如果未能解决你的问题,请参考以下文章

PL\SQL 语句忽略错误

执行立即错误:PL/SQL:数字或值错误:字符到数字转换错误

在更新中使用 IF 语句的 PL/SQL

oracle数据库之PL/SQL 流程控制语句

PL/SQL基础的执行部分

pl/sql流程控制语句if else条件选择语句