在期望以下之一时遇到符号:如果在 Pl/SQL 函数中

Posted

技术标签:

【中文标题】在期望以下之一时遇到符号:如果在 Pl/SQL 函数中【英文标题】:Encountered the symbol when expecting one of the following: if in Pl/SQL function 【发布时间】:2015-09-11 11:20:46 【问题描述】:

我正在编写下面的函数,其中我收到一个错误,我认为 if/else 条件为Error(1360,5): PLS-00103: Encountered the symbol "BUILD_ALERT_EMAIL_BODY" when expecting one of the following: if。我想我使用了正确的语法,但不知道为什么会出现错误

FUNCTION BUILD_ALERT_EMAIL_BODY
(
  IN_ALERT_LOGS_TIMESTAMP IN TIMESTAMP
, IN_ALERT_LOGS_LOG_DESC IN VARCHAR2
, IN_KPI_LOG_ID IN NUMBER
) RETURN VARCHAR2 AS
BODY VARCHAR2(4000) := '';
V_KPI_TYPE_ID NUMBER;
V_KPI_THRESHOLD_MIN_VALE NUMBER;
V_KPI_THRESHOLD_MAX_VALE NUMBER;
V_EXPECTED_VALE NUMBER;
V_ACTUAL_VALE NUMBER;  

BEGIN
-- ,'yyyy-MM-dd H24 mm ss'
Select KPI_DEF_ID INTO V_KPI_DEF_ID FROM KPI_LOGS WHERE KPI_LOG_ID = IN_KPI_LOG_ID;
Select EXPECTED_VALE INTO V_EXPECTED_VALE FROM KPI_LOGS WHERE KPI_LOG_ID = IN_KPI_LOG_ID;
Select ACTUAL_VALE INTO V_ACTUAL_VALE FROM KPI_LOGS WHERE KPI_LOG_ID = IN_KPI_LOG_ID;


  Select KT.KPI_TYPE_ID INTO V_KPI_TYPE_ID FROM RATOR_MONITORING_CONFIGURATION.KPI_DEFINITION KD JOIN RATOR_MONITORING_CONFIGURATION.KPI_TYPE KT ON KD.KPI_TYPE = KT.KPI_TYPE_ID WHERE KD.KPI_DEF_ID = V_KPI_DEF_ID;
Select KPI_THRESHOLD_MIN_VAL INTO V_KPI_THRESHOLD_MIN_VALE FROM RATOR_MONITORING_CONFIGURATION.KPI_DEFINITION WHERE KPI_DEF_ID = V_KPI_DEF_ID;
Select KPI_THRESHOLD_MAX_VAL INTO V_KPI_THRESHOLD_MAX_VALE FROM RATOR_MONITORING_CONFIGURATION.KPI_DEFINITION WHERE KPI_DEF_ID = V_KPI_DEF_ID;

    IF ((V_KPI_TYPE_ID = 18) || (V_KPI_TYPE_ID = 19))  THEN
    BODY := BODY || 'KPI_THRESHOLD_MIN_VAL:' || V_KPI_THRESHOLD_MIN_VALE || Chr(13) || Chr(10);
    BODY := BODY || 'KPI_THRESHOLD_MAX_VAL:' || V_KPI_THRESHOLD_MAX_VALE || Chr(13) || Chr(10);
    ELSE IF ((V_KPI_TYPE_ID = 11) || (V_KPI_TYPE_ID = 12) || (V_KPI_TYPE_ID = 13) || (V_KPI_TYPE_ID = 14) || (V_KPI_TYPE_ID = 20)) THEN
    BODY := BODY || 'EXPECTED_VALE:' || V_EXPECTED_VALE || Chr(13) || Chr(10);
    BODY := BODY || 'ACTUAL_VALE:' || V_ACTUAL_VALE || Chr(13) || Chr(10);    
    END IF;

    RETURN BODY;
END BUILD_ALERT_EMAIL_BODY;

【问题讨论】:

只要检查你的 if-else 结构 if-elsif-else 就可以了.. 语法正确 【参考方案1】:

RETURNstatement 之前还需要一个END IF;

FUNCTION BUILD_ALERT_EMAIL_BODY (IN_ALERT_LOGS_TIMESTAMP   IN TIMESTAMP,
                                 IN_ALERT_LOGS_LOG_DESC    IN VARCHAR2,
                                 IN_KPI_LOG_ID             IN NUMBER)
   RETURN VARCHAR2
AS
   BODY                       VARCHAR2 (4000) := '';
   V_KPI_TYPE_ID              NUMBER;
   V_KPI_THRESHOLD_MIN_VALE   NUMBER;
   V_KPI_THRESHOLD_MAX_VALE   NUMBER;
   V_EXPECTED_VALE            NUMBER;
   V_ACTUAL_VALE              NUMBER;
BEGIN
   -- ,'yyyy-MM-dd H24 mm ss'
   SELECT KPI_DEF_ID
     INTO V_KPI_DEF_ID
     FROM KPI_LOGS
    WHERE KPI_LOG_ID = IN_KPI_LOG_ID;

   SELECT EXPECTED_VALE
     INTO V_EXPECTED_VALE
     FROM KPI_LOGS
    WHERE KPI_LOG_ID = IN_KPI_LOG_ID;

   SELECT ACTUAL_VALE
     INTO V_ACTUAL_VALE
     FROM KPI_LOGS
    WHERE KPI_LOG_ID = IN_KPI_LOG_ID;


   SELECT KT.KPI_TYPE_ID
     INTO V_KPI_TYPE_ID
     FROM    RATOR_MONITORING_CONFIGURATION.KPI_DEFINITION KD
          JOIN
             RATOR_MONITORING_CONFIGURATION.KPI_TYPE KT
          ON KD.KPI_TYPE = KT.KPI_TYPE_ID
    WHERE KD.KPI_DEF_ID = V_KPI_DEF_ID;

   SELECT KPI_THRESHOLD_MIN_VAL
     INTO V_KPI_THRESHOLD_MIN_VALE
     FROM RATOR_MONITORING_CONFIGURATION.KPI_DEFINITION
    WHERE KPI_DEF_ID = V_KPI_DEF_ID;

   SELECT KPI_THRESHOLD_MAX_VAL
     INTO V_KPI_THRESHOLD_MAX_VALE
     FROM RATOR_MONITORING_CONFIGURATION.KPI_DEFINITION
    WHERE KPI_DEF_ID = V_KPI_DEF_ID;

   IF ( (V_KPI_TYPE_ID = 18) || (V_KPI_TYPE_ID = 19))
   THEN
      BODY :=
            BODY
         || 'KPI_THRESHOLD_MIN_VAL:'
         || V_KPI_THRESHOLD_MIN_VALE
         || CHR (13)
         || CHR (10);
      BODY :=
            BODY
         || 'KPI_THRESHOLD_MAX_VAL:'
         || V_KPI_THRESHOLD_MAX_VALE
         || CHR (13)
         || CHR (10);
   ELSE
      IF (   (V_KPI_TYPE_ID = 11)
          || (V_KPI_TYPE_ID = 12)
          || (V_KPI_TYPE_ID = 13)
          || (V_KPI_TYPE_ID = 14)
          || (V_KPI_TYPE_ID = 20))
      THEN
         BODY :=
               BODY
            || 'EXPECTED_VALE:'
            || V_EXPECTED_VALE
            || CHR (13)
            || CHR (10);
         BODY :=
            BODY || 'ACTUAL_VALE:' || V_ACTUAL_VALE || CHR (13) || CHR (10);
      END IF;
   END IF; --<-- HERE

   RETURN BODY;
END BUILD_ALERT_EMAIL_BODY;

【讨论】:

以上是关于在期望以下之一时遇到符号:如果在 Pl/SQL 函数中的主要内容,如果未能解决你的问题,请参考以下文章

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

PLS-00103:在预期以下之一时遇到符号“文件结尾”

如何修复“在预期以下情况之一时遇到符号“=”:plsql中的错误

ORA-06550 第 10 行,第 41 列:PLS-00103:在预期以下情况之一时遇到符号“,”:

PLS-00103:错误。在期望以下之一时遇到符号“BEGIN”:<an identifier> <a double-quoted delimited-identifier>

错误:PLS-00103:遇到符号“;”当期望以下之一时:* & = - + < / > at in is mod remaining not rem then