Orace SQL - 嵌套的CASE语句

时间:2018-04-27 15:43:51

标签: sql oracle

我需要在函数中添加COD参数。 COD是一个数字,可以是2,4或6.我需要根据数量将参数unit1转换为unit2。

我只关心代码的最后一部分,当unit1是'平板电脑'而cod = 4并且它从'平板电脑'转换为公斤(对于unit2)。我希望代码按WHEN 'KG' THEN IVALUE := QUANTITY * 0.0000011计算,否则如果除了c od = 4之外还有其他任何内容,那么我希望它按WHEN 'KG' THEN IVALUE := QUANTITY * 0.00025进行计算。

如何编写下面的代码才能正确编译?

我在Oracle中遇到语法错误。我没有正确地嵌套我的CASE语句。

create or replace FUNCTION  CONVERTUNITOFMEASURE_COD
 (COD NUMBER,
QUANTITY NUMBER,
UNIT1 VARCHAR2,
UNIT2 VARCHAR2)
RETURN NUMBER IS
  IVALUE NUMBER;
  OUT_UOM NUMBER;
BEGIN

CASE UPPER(UNIT1)

WHEN 'KG' THEN
    CASE UPPER(UNIT2)
      WHEN 'LB' THEN IVALUE := QUANTITY / 0.45359237;
      WHEN 'G' THEN IVALUE := QUANTITY / 0.001;
      WHEN 'L' THEN IVALUE := QUANTITY / 0.8;
      WHEN 'TABL' THEN IVALUE := QUANTITY / 0.00025;
      WHEN 'GAL' THEN IVALUE := QUANTITY / 2.64;
      WHEN 'KG' THEN IVALUE := QUANTITY * 1;
      ELSE NULL;
    END CASE;

WHEN 'G' THEN
    CASE UPPER(UNIT2)
      WHEN 'LB' THEN IVALUE := QUANTITY  * 0.00220462262 ;
      WHEN 'KG' THEN IVALUE := QUANTITY * 0.001;
      WHEN 'G' THEN IVALUE := QUANTITY * 1;
      ELSE NULL;
    END CASE;

WHEN 'LB' THEN
    CASE UPPER(UNIT2)
        WHEN 'KG' THEN IVALUE := QUANTITY * 0.45359237;
        WHEN 'G' THEN IVALUE := QUANTITY * 453.59237;
        WHEN 'LB' THEN IVALUE := QUANTITY * 1;
        ELSE NULL;
    END CASE;

WHEN 'L' THEN
     CASE UPPER(UNIT2)
         WHEN 'ML' THEN IVALUE := QUANTITY * 1000;
         WHEN 'GAL' THEN IVALUE := QUANTITY * 0.264172052;
         WHEN 'KG' THEN IVALUE := QUANTITY * 0.8;
         WHEN 'L' THEN IVALUE := QUANTITY * 1;
         ELSE NULL;
      END CASE;

WHEN 'ML' THEN
     CASE UPPER(UNIT2)
         WHEN 'L' THEN IVALUE := QUANTITY * 0.001;
         WHEN 'GAL' THEN IVALUE := QUANTITY / 3785.41178;
         WHEN 'ML' THEN IVALUE := QUANTITY * 1;
         ELSE NULL;
      END CASE;

WHEN 'GAL' THEN
     CASE UPPER(UNIT2)
         WHEN 'L' THEN IVALUE := QUANTITY * 3.785411784;
         WHEN 'ML' THEN IVALUE := QUANTITY * 3785.41178;
         WHEN 'KG' THEN IVALUE := QUANTITY * 2.64;
         WHEN 'GAL' THEN IVALUE := QUANTITY * 1;
         ELSE NULL;
      END CASE;

WHEN 'TABL' THEN
             CASE WHEN (COD) = 4 THEN 
  CASE UPPER(UNIT2)
             WHEN 'KG' THEN IVALUE := QUANTITY * 0.0000011;
         WHEN 'TABL' THEN IVALUE := QUANTITY * 1;
     ELSE NULL;
     END CASE;

     CASE UPPER(UNIT2)
         WHEN 'KG' THEN IVALUE := QUANTITY * 0.00025;
         WHEN 'TABL' THEN IVALUE := QUANTITY * 1;
        ELSE NULL;
      END CASE;
ELSE NULL;
END CASE;
OUT_UOM := IVALUE ;
RETURN OUT_UOM;
END;

感谢。

1 个答案:

答案 0 :(得分:0)

如果你是粗略地缩进结尾部分,你会发现你错过了END CASE;

WHEN 'TABL' THEN
     CASE WHEN (COD) = 4 THEN 
         CASE UPPER(UNIT2)
             WHEN 'KG' THEN IVALUE := QUANTITY * 0.0000011;
             WHEN 'TABL' THEN IVALUE := QUANTITY * 1;
             ELSE NULL;
         END CASE;
         CASE UPPER(UNIT2)
             WHEN 'KG' THEN IVALUE := QUANTITY * 0.00025;
             WHEN 'TABL' THEN IVALUE := QUANTITY * 1;
             ELSE NULL;
         END CASE;
         ELSE NULL;
     END CASE;

-- shoudld have another END CASE; here

OUT_UOM := IVALUE ;
RETURN OUT_UOM;
END;

虽然两个嵌套的案例似乎也有错;第二个只是覆盖了第一个设置的值 - 也许你也错过了ELSE之间的值:

WHEN 'TABL' THEN
     CASE WHEN (COD) = 4 THEN 
         CASE UPPER(UNIT2)
             WHEN 'KG' THEN IVALUE := QUANTITY * 0.0000011;
             WHEN 'TABL' THEN IVALUE := QUANTITY * 1;
             ELSE NULL;
         END CASE;
     ELSE
         CASE UPPER(UNIT2)
             WHEN 'KG' THEN IVALUE := QUANTITY * 0.00025;
             WHEN 'TABL' THEN IVALUE := QUANTITY * 1;
             ELSE NULL;
         END CASE;
     END CASE;

END CASE;

OUT_UOM := IVALUE ;
RETURN OUT_UOM;
END;