运行此程序时出错

时间:2015-02-19 17:24:32

标签: oracle plsql

Declare
BlakeSal number;
MaxSal number;
Begin
    BlakeSal:= SELECT sal FROM emp WHERE ename = 'Blake';
    MaxSal:= SELECT MAX(sal) FROM emp);
If BlakeSal > MaxSal THEN
 dbms_output.put_line('Blake''s Salary is the highest amoungst his employees');
Else
 dbms_output.put_line('Blake''s Salary isn't the highest salary amoungst his employees');
End if;
EXCEPTION
    WHEN OTHERS THEN
    dbms_output.put_line('There seems to be a null value, Please check the salary column');
END;
/

不知道为什么这不起作用 获取错误QRA-01756:引用字符串未正确终止

1 个答案:

答案 0 :(得分:3)

您未在此声明中转义isn't中的单引号:

dbms_output.put_line('Blake''s Salary isn't the highest salary amoungst his employees');

应该是:

dbms_output.put_line('Blake''s Salary isn''t the highest salary amongst his employees');

或使用引用的语法使这更容易:

dbms_output.put_line(q'[Blake's Salary isn't the highest salary amongst his employees]');

(我冒昧地纠正了amongst的拼写* 8 - )


您也不会从这样的查询中分配值;您使用select ... into设置值:

SELECT sal INTO BlakeSal FROM emp WHERE ename = 'Blake';

...虽然你可以在一个简单的SQL语句而不是PL / SQL中完成整个事情;我认为这是一个练习。

捕获OTHERS也被认为是不好的做法;你失去了关于实际出错的所有信息以及在哪里,并且工资栏中的空值无论如何都不会抛出异常,它只会给出错误的结果。