执行匿名PL / SQL块

时间:2017-04-06 20:40:49

标签: sql plsql

问题是要求匿名的PL / SQL块: 声明一个变量,用于计算Products表中的所有产品 有一个if-else语句取决于count是> =还是< 7个

到目前为止我的尝试:

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”:    (如果循环mod,则开始为goto声明结束异常退出    null pragma raise返回选择更新时使用     ORA-06550:第12行第1列:     
PLS-00103:遇到符号“EXCEPTION”     
06550。 00000 - “行%s,列%s:\ n%s”

我错过了什么?

1 个答案:

答案 0 :(得分: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;