PL SQL(Oracle)错误中的函数

时间:2017-03-19 03:48:38

标签: sql oracle plsql

CREATE OR REPLACE FUNCTION ComputeFreight(subtotal NUMBER)
RETURN NUMBER
IS
freightCharge NUMBER;
BEGIN
    IF subtotal <= 15000 THEN
    freightCharge := (subtotal * .10);
    ELSIF subtotal > 15000 THEN 
    freightCharge := (subtotal * .15);
    RETURN(freightCharge);
    END IF;
END;

DECLARE
  subtotal NUMBER := 15000;
BEGIN
  DBMS_OUTPUT.PUT_LINE( ComputeFreight(subtotal) );
END;

我不知道我的代码有什么问题,但它始终显示此错误:

  

声明无效

     

声明无效

     

ORA-06503:PL / SQL:返回的函数没有值ORA-06512:at   &#34; SQL_JQQXUMCMKFMRHRPZCYFNBEVTN.COMPUTEFREIGHT&#34;,第12行ORA-06512:at   第4行ORA-06512:在&#34; SYS.DBMS_SQL&#34;,第1721行

2 个答案:

答案 0 :(得分:3)

return语句应该在if-else之外:

CREATE OR REPLACE FUNCTION ComputeFreight(subtotal NUMBER)
RETURN NUMBER
IS
freightCharge NUMBER;
BEGIN
    IF subtotal <= 15000 THEN
        freightCharge := (subtotal * .10);
    ELSIF subtotal > 15000 THEN 
        freightCharge := (subtotal * .15);
    END IF;
    RETURN(freightCharge);    -- here
END;

答案 1 :(得分:2)

如果这是IF语句的惯例,那很好(使用GurV建议放置RETURN语句)。但是,如果您在某种生产中实际需要它,您可能希望简化功能并使其更有效。像这样:

CREATE OR REPLACE FUNCTION ComputeFreight(subtotal NUMBER)
RETURN NUMBER
IS
BEGIN
    RETURN subtotal * (CASE WHEN subtotal <= 15000 THEN 0.1 ELSE 0.15 END);
END;