在一个程序中,我想做逻辑单元1,如果失败并不重要,执行逻辑单元2
这似乎是典型的尝试捕获方案。但我怎么能在pl / sql中做到这一点?
create or replace
PACKAGE BUILD_PKG
AS
PROCEDURE reset_seq_and_truncate_tbl(
p_seq_name IN VARCHAR2,
p_table_name IN VARCHAR2 );
END BUILD_PKG;
/
create or replace
PACKAGE BODY BUILD_PKG
AS
BEGIN
PROCEDURE reset_seq_and_truncate_tbl(
p_seq_name IN VARCHAR2,
p_table_name IN VARCHAR2 )
IS
l_val NUMBER;
BEGIN
BEGIN
EXECUTE immediate 'truncate table ' || p_table_name;
EXCEPTION
WHEN OTHERS
THEN dbms_output.put_line(SQLCODE);
END;
BEGIN
EXECUTE immediate 'alter sequence ' || p_seq_name || ' increment by 1 minvalue 0';
EXCEPTION
WHEN OTHERS
THEN dbms_output.put_line(SQLCODE);
END;
END reset_seq_and_truncate_tbl;
END BUILD_PKG;
你可以看到第二个异常块导致问题。
答案 0 :(得分:18)
将EXCEPTION
块中的第二个BEGIN..END
括起来。试试这种方式
PROCEDURE reset_seq_and_truncate_tbl(
p_seq_name IN VARCHAR2,
p_table_name IN VARCHAR2 )
IS
l_val NUMBER;
BEGIN
EXECUTE immediate 'truncate table ' || p_table_name;
EXCEPTION
WHEN OTHERS
THEN
BEGIN
dbms_output.put_line(SQLCODE);
EXECUTE immediate 'alter sequence ' || p_seq_name || ' increment by 1 minvalue 0';
--this would cause compilation error
EXCEPTION
WHEN OTHERS
THEN dbms_output.put_line(SQLCODE);
END;
END reset_seq_and_truncate_tbl;
更新以回应评论
PROCEDURE reset_seq_and_truncate_tbl(
p_seq_name IN VARCHAR2,
p_table_name IN VARCHAR2 )
IS
l_val NUMBER;
BEGIN
BEGIN
EXECUTE immediate 'truncate table ' || p_table_name;
EXCEPTION
WHEN OTHERS
THEN dbms_output.put_line(SQLCODE);
END;
BEGIN
EXECUTE immediate 'alter sequence ' || p_seq_name || ' increment by 1 minvalue 0';
EXCEPTION
WHEN OTHERS
THEN dbms_output.put_line(SQLCODE);
END;
END reset_seq_and_truncate_tbl;
答案 1 :(得分:1)
程序( ) IS
BEGIN
- 逻辑单位1 开始 ... 例外 等等 然后 ... END;
- 逻辑单元2 开始 ... 例外 等等 然后 ... 结束;
- 最终异常阻止 例外 等等 然后......
结束;