如何使用' if'条件内的条件如果'条件?

时间:2015-12-03 14:36:19

标签: oracle function if-statement plsql oracle11g

我正在尝试使用' if'条件内的条件如果'条件来创建一个函数。 我知道语法看起来有点像下面,但我无法在我的代码中执行此操作。

IF sales > (quota + 200) THEN
    bonus := (sales - quota)/4;
ELSE
    IF sales > quota THEN
        bonus := 50;
    ELSE
        bonus := 0;
    END IF;
END IF;

以下是我要完成的计划。请帮帮我。 它的功能是根据呼叫次数查找电话费金额 并计划类型

CREATE OR REPLACE  FUNCTION BILL(NUM_OF_CALLS NUMBER, PLAN_TYPE NUMBER) RETURN NUMBER IS :

    BILL_AMT NUMBER;
    MIN_1 NUMBER :=150;
    MIN_2 NUMBER :=1000;    

    BEGIN
    IF PLAN_TYPE:=150 THEN 
        IF NUM_OF_CALLS<150 THEN 
            BILL_AMT:=MIN_1;
            ELSIF NUM_OF_CALLS BETWEEN 151 AND 250 THEN
            BILL_AMT:=MIN_1+(NUM_OF_CALLS-150);
            ELSIF NUM_OF_CALLS BETWEEN 251 AND 400 THEN
            BILL_AMT:=MIN_1+(100*1)+(NUM_OF_CALLS-250)*0.5;
            ELSIF NUM_OF_CALLS>400 THEN
            BILL_AMT:=MIN_1+(100*1)+(150*0.5)+(NUM_OF_CALLS-400)*0.3;
        END IF;
        ELSE PLAN_TYPE:=500 THEN
            IF NUM_OF_CALLS<1000 THEN 
            BILL_AMT:=MIN_2;
            ELSIF NUM_OF_CALLS BETWEEN 1001 AND 1500 THEN
            BILL_AMT:=MIN_2+(NUM_OF_CALLS-1000)*0.50;
            ELSIF NUM_OF_CALLS>1500 THEN
            BILL_AMT:=MIN_2+(500*0.50)+(NUM_OF_CALLS-1500)*0.25;
        END IF;
    END IF;

    RETURN BILL_AMT;

END;

以下是错误

SQL&GT;显示错误 功能条例错误:

LINE/COL ERROR

11/14    PLS-00103: Encountered the symbol "=" when expecting one of the
         following:
         . ( * @ % & = - + < / > at in is mod remainder not rem then
         <an exponent (**)> <> or != or ~= >= <= <> and or like like2
         like4 likec between || multiset member submultiset
         The symbol "* was inserted before "=" to continue.

22/22    PLS-00103: Encountered the symbol "THEN" when expecting one of
         the following:
         * & = - + ; < / > at in is mod remainder not rem
         <an exponent (**)> <> or != or ~= >= <= <> and or like like2
         like4 likec between || multiset member submultiset

31/6     PLS-00103: Encountered the symbol "IF" when expecting one of the
         following:
         ; <an identifier> <a double-quoted delimited-identifier>
         current delete exists prior <a single-quoted SQL string>

3 个答案:

答案 0 :(得分:0)

您的答案直接在错误输出中。

PLSQL使用:=进行分配,使用=进行比较。您正在混合这些运算符并导致这些错误。

仔细阅读错误,他们准确地描述了问题。

提示:一次添加少量逻辑,以便您知道问题的来源。我们每个人在某个时间点都会学到这一点:)

答案 1 :(得分:0)

你不能

   ELSE PLAN_TYPE:=500 THEN

但你可以

   ELSIF PLAN_TYPE =500 THEN

如果我已经弄清楚你的逻辑流程

CREATE OR REPLACE  FUNCTION BILL(NUM_OF_CALLS NUMBER, PLAN_TYPE NUMBER) RETURN NUMBER IS 

    BILL_AMT NUMBER;
    MIN_1    NUMBER :=150;
    MIN_2    NUMBER :=1000;    

 BEGIN
    IF PLAN_TYPE = 150 
    THEN 
        IF NUM_OF_CALLS<150 THEN 
            BILL_AMT  :=MIN_1;
        ELSIF NUM_OF_CALLS BETWEEN 151 AND 250 THEN
            BILL_AMT:=MIN_1+(NUM_OF_CALLS-150);
        ELSIF NUM_OF_CALLS BETWEEN 251 AND 400 THEN
            BILL_AMT:=MIN_1+(100*1)+(NUM_OF_CALLS-250)*0.5;
        ELSIF NUM_OF_CALLS>400 THEN
            BILL_AMT:=MIN_1+(100*1)+(150*0.5)+(NUM_OF_CALLS-400)*0.3;
        END IF;
    ELSIF PLAN_TYPE = 500 THEN
        IF NUM_OF_CALLS<1000 THEN 
            BILL_AMT:=MIN_2;
        ELSIF NUM_OF_CALLS BETWEEN 1001 AND 1500 THEN
            BILL_AMT:=MIN_2+(NUM_OF_CALLS-1000)*0.50;
        ELSIF NUM_OF_CALLS>1500 THEN
            BILL_AMT:=MIN_2+(500*0.50)+(NUM_OF_CALLS-1500)*0.25;
        END IF;
    END IF;

    RETURN BILL_AMT;

END;

答案 2 :(得分:0)

这样的事情对你有用:

CREATE OR REPLACE  
FUNCTION BILL(NUM_OF_CALLS NUMBER, PLAN_TYPE NUMBER) 
   RETURN NUMBER 
IS

    BILL_AMT NUMBER;
    MIN_1    NUMBER := 150;
    MIN_2    NUMBER := 1000;    

BEGIN
    IF PLAN_TYPE = 150 
    THEN 
       IF NUM_OF_CALLS<150 
       THEN 
          BILL_AMT:= MIN_1;
       ELSIF NUM_OF_CALLS BETWEEN 151 AND 250 
       THEN
          BILL_AMT:=MIN_1+(NUM_OF_CALLS-150);
       ELSIF NUM_OF_CALLS BETWEEN 251 AND 400 
       THEN
          BILL_AMT:=MIN_1+(100*1)+(NUM_OF_CALLS-250)*0.5;
       ELSIF NUM_OF_CALLS>400 
       THEN
          BILL_AMT:=MIN_1+(100*1)+(150*0.5)+(NUM_OF_CALLS-400)*0.3;
       END IF;
    ELSIF PLAN_TYPE = 500 
    THEN
       IF NUM_OF_CALLS<1000 
       THEN 
          BILL_AMT:=MIN_2;
       ELSIF NUM_OF_CALLS BETWEEN 1001 AND 1500 THEN
          BILL_AMT:=MIN_2+(NUM_OF_CALLS-1000)*0.50;
       ELSIF NUM_OF_CALLS>1500 THEN
          BILL_AMT:=MIN_2+(500*0.50)+(NUM_OF_CALLS-1500)*0.25;
       END IF;
    END IF;

    RETURN BILL_AMT;

END BILL;

希望它有所帮助...