这个问题已经在这里有一个答案:
起初,我试图创建一个例外,赶上didnt存在的表(-00942)。基于来自亚历克斯和Raj反馈,我修改了代码中使用EXECUTE IMMEDIATE。这个工作在确定表但是当我纠正从“雇工”到“员工”,这是在我的数据库中存在的表的表,我本来期望的代码运行没有错误的是,当我没有使用EXECUTE IMMEDIATE的情况下。
代替生成捕获所有异常,有可能是代码通过无一例外使用EXECUTE IMMEDIATE时运行?
EXECUTE IMMEDIATE 'SELECT INTO姓氏FROM v_name员工工资WHERE = v_sal';
WHEN OTHERS THEN:g_message:= '发生其他一些错误。';
我希望的代码无一例外通过运行。
VARIABLE g_message VARCHAR2(250)
DEFINE p_sal = 12000
DECLARE
v_name employees.last_name%TYPE;
v_sal employees.salary%TYPE := &p_sal;
table_does_not_exist exception;
PRAGMA EXCEPTION_INIT(table_does_not_exist, -942);
BEGIN
EXECUTE IMMEDIATE
'SELECT last_name INTO v_name FROM employees WHERE salary = v_sal';
DBMS_OUTPUT.put_line(v_name);
EXCEPTION
WHEN table_does_not_exist then
:g_message := 'table dose not exist';
WHEN NO_DATA_FOUND THEN
:g_message := 'No employee with a salary of '||TO_CHAR(v_sal);
WHEN OTHERS THEN
:g_message := 'Some other error occurred.';
END;
/
PRINT g_message
试试下面:
DECLARE
v_name employee.emp_name%TYPE;
v_sal employee.emp_sal%TYPE := 12000;
table_does_not_exist exception;
PRAGMA EXCEPTION_INIT(table_does_not_exist, -942);
BEGIN
execute immediate
'SELECT emp_name
FROM employee_l
WHERE emp_sal = v_sal' INTO v_name;
DBMS_OUTPUT.put_line(v_name);
EXCEPTION
WHEN table_does_not_exist then
dbms_output.put_line( 'table dose not exist');
WHEN NO_DATA_FOUND THEN
dbms_output.put_line( 'No employee with a salary of '||TO_CHAR(v_sal));
WHEN OTHERS THEN
dbms_output.put_line( 'Some other error occurred.');
END;
/