2017-04-06 82 views
1

的問題是要求匿名的PL/SQL塊:
         宣佈,在計算Products表
         所有產品的變量有一個如果 -執行匿名PL/SQL塊

:這取決於計數是否> =或

我嘗試到目前爲止else語句個

SET SERVEROUTPUT ON; DECLARE number_of_products NUMBER; BEGIN SELECT SUM(product_id) INTO number_of_products FROM products IF number_of_products >= 7 THEN --Syntax error on this line, not sure why DBMS_OUTPUT.PUT_LINE('The number of products is greater than or equal to 7'); ELSE DBMS_OUTPUT.PUT_LINE('The number of products is less than 7'); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred'); END; 

錯誤消息:
PL/SQL:ORA-00933:SQL命令未正確結束
ORA-06550:行4,列3:
PL/SQL:SQL語句忽略
ORA -06550:第9行第3列:
PLS-00103:在期待以下某項時遇到符號「ELSE」: (開始case如果循環mod聲明結束異常退出 null pragma raise return select update with ORA-06550:第12行,colu MN 1:
PLS-00103:出現符號 「EXCEPTION」
06550. 00000 - 「行%S,列%S:\ n%S」

我缺少什麼?

+0

你會得到什麼錯誤? – Zeus

+0

我剛剛將錯誤消息添加到原始帖子。 – SummerBoy

+1

分號在sql查詢結束? – Zeus

回答

1
SET SERVEROUTPUT ON; 
DECLARE 
    number_of_products NUMBER; 
BEGIN 
    SELECT SUM(product_id) 
    INTO number_of_products 
    FROM products; 
    IF number_of_products >= 7 THEN --Syntax error on this line, not sure why 
    DBMS_OUTPUT.PUT_LINE('The number of products is greater than or equal to 7'); 
    ELSE 
    DBMS_OUTPUT.PUT_LINE('The number of products is less than 7'); 
    END IF; 
EXCEPTION 
    WHEN OTHERS THEN 
    DBMS_OUTPUT.PUT_LINE('An error occurred'); 
END; 
+0

嗯,它的功能就像一個魅力,但我看不出你改變了哪一部分? – SummerBoy

+0

@SummerBoy'FROM products;'在select查詢中。 – Zeus