新人求指教 oracle 选项缺失或无效

2025-05-11 08:33:04
推荐回答(1个)
回答1:

为啥要写EXCEPTION?你都没定义EXCEPTION的异常事件。如果要用异常,需要在触发异常的时候使用raise 异常事件名称,,异常触发后,循环将跳出。同时前后需要用begin 和end来表示异常监控的语句块。
例如
DECLARE
xxxx exception;--异常事件名称
begin

begin --异常开始
if a>0 then
raise XXXX;--调用异常
END IF;
exception
when xxxx then --声明的异常事件处理
insert into .........
when others then---其他异常处理
。。。。。。。。。。。
end --异常结束

其实你这个可以更简单
BEGIN
n:=0;
FOR emp_cu IN emp_curs LOOP
IF emp_cu.SALARY=&p_salary THEN
n:=n+1;
END IF;
END LOOP;

if n>1 THEN
INSERT INTO messages VALUES('More than one employee with a salary of'||to_char($p_salary));
COMMIT;
elsif n=0 THEN
INSERT INTO messages VALUES('No employee with a salary of'||to_char($p_salary));
COMMIT;
elsif n=1 THEN INSERT INTO messages VALUES(employees.FIRST_NAME||employees.LAST_NAME||'salary is'||to_char($p_salary));
COMMIT;
else
INSERT INTO messages VALUES('Some other error occurred.');
COMMIT;
end if;
END;