所以这是我在oracle类中给出的代码:
CREATE OR REPLACE PROCEDURE add_job_hist
(p_emp_id IN employees.employee_id%TYPE,
p_new_jobid IN jobs.job_id%TYPE) IS
BEGIN
INSERT INTO job_history
SELECT employee_id, hire_date, SYSDATE, job_id, department_id
FROM employees
WHERE employee_id = p_emp_id;
UPDATE employees
SET hire_date = SYSDATE,
job_id = p_new_jobid,
salary = (SELECT min_salary + 500
FROM jobs
WHERE job_id = p_new_jobid)
WHERE employee_id = p_emp_id;
DBMS_OUTPUT.PUT_LINE('Added employee ' || p_emp_id || '
details to the JOB_HISTORY table');
DBMS_OUTPUT.PUT_LINE('Updated current job of employee ' ||
p_emp_id|| 'to ' || p_new_jobid);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR (-20001, 'Employee does not exist!');
END add_job_hist;
/
问题是这不是“SELECT INTO”语句,因此不处理WHEN NO_DATA_FOUND异常。我可以提出自己的用户定义的异常,但似乎不允许我在一个过程中执行此操作。我如何解决这个问题,以便它处理异常?因为现在,如果我给出一个不存在的p_emp_id,它无论如何都要处理它,并且指令不需要任何额外的过程变量。
谢谢!
答案 0 :(得分:4)
在insert语句后使用SQL%ROWCOUNT进行检查,如果返回0,则显式提出no_data_found。
CREATE OR REPLACE PROCEDURE add_job_hist(p_emp_id IN employees.employee_id%TYPE,
p_new_jobid IN jobs.job_id%TYPE) IS
BEGIN
INSERT INTO job_history
SELECT employee_id,
hire_date,
SYSDATE,
job_id,
department_id
FROM employees
WHERE employee_id = p_emp_id;
IF SQL%ROWCOUNT = 0 THEN
RAISE no_data_found;
END IF;
UPDATE employees
SET hire_date = SYSDATE,
job_id = p_new_jobid,
salary =
(SELECT min_salary + 500 FROM jobs WHERE job_id = p_new_jobid)
WHERE employee_id = p_emp_id;
dbms_output.put_line('Added employee ' || p_emp_id || '
details to the JOB_HISTORY table');
dbms_output.put_line('Updated current job of employee ' || p_emp_id || 'to ' || p_new_jobid);
EXCEPTION
WHEN no_data_found THEN
raise_application_error(-20001, 'Employee does not exist!');
END add_job_hist;
/