如果 plsql 中的条件,则在内部引发异常
raise Exception inside if condition in plsql
declare
cnt number;
begin
select count(*)
into cnt
FROM employee;
IF cnt = 0 THEN
DELETE employee;
COMMIT;
EXCEPTION
WHEN others THEN
log_error_local(k_sub_module, l_step||' '||sqlerrm);
raise;
END IF;
end
出现如下语法错误..
错误(188,13):PLS-00103:在预期以下情况之一时遇到符号“EXCEPTION”:( begin case declare else elsif end exit for goto if loop mod null pragma raise return select update while with << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge
要解决语法问题,您可以将 IF .. THEN
语句的内容包装在 BEGIN
/END
块中:
DECLARE
cnt number;
BEGIN
select count(*)
into cnt
FROM employee;
IF cnt = 0 THEN
BEGIN
DELETE FROM employee;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
log_error_local(k_sub_module, l_step||' '||sqlerrm);
raise;
END;
END IF;
END;
/
注意:DELETE
语句缺少 FROM
关键字,您似乎没有声明 k_sub_module
或 l_step
变量。
或者,因为你是 re-raising 异常和执行无论如何都会停止,在主块中的 IF
语句之外捕获异常:
DECLARE
cnt number;
BEGIN
select count(*)
into cnt
FROM employee;
IF cnt = 0 THEN
DELETE FROM employee;
COMMIT;
END IF;
EXCEPTION
WHEN OTHERS THEN
log_error_local(k_sub_module, l_step||' '||sqlerrm);
raise;
END;
/
db<>fiddle here
declare
cnt number;
begin
select count(*)
into cnt
FROM employee;
IF cnt = 0 THEN
DELETE employee;
COMMIT;
EXCEPTION
WHEN others THEN
log_error_local(k_sub_module, l_step||' '||sqlerrm);
raise;
END IF;
end
出现如下语法错误.. 错误(188,13):PLS-00103:在预期以下情况之一时遇到符号“EXCEPTION”:( begin case declare else elsif end exit for goto if loop mod null pragma raise return select update while with << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge
要解决语法问题,您可以将 IF .. THEN
语句的内容包装在 BEGIN
/END
块中:
DECLARE
cnt number;
BEGIN
select count(*)
into cnt
FROM employee;
IF cnt = 0 THEN
BEGIN
DELETE FROM employee;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
log_error_local(k_sub_module, l_step||' '||sqlerrm);
raise;
END;
END IF;
END;
/
注意:DELETE
语句缺少 FROM
关键字,您似乎没有声明 k_sub_module
或 l_step
变量。
或者,因为你是 re-raising 异常和执行无论如何都会停止,在主块中的 IF
语句之外捕获异常:
DECLARE
cnt number;
BEGIN
select count(*)
into cnt
FROM employee;
IF cnt = 0 THEN
DELETE FROM employee;
COMMIT;
END IF;
EXCEPTION
WHEN OTHERS THEN
log_error_local(k_sub_module, l_step||' '||sqlerrm);
raise;
END;
/
db<>fiddle here