我想创建一个简单的过程,当传递主键时,将删除表中的记录。
我已阅读有关SQL%NOTFOUND的this thread。我认为该问题的解决方案是使用一个函数来检查传递的参数是否是该表的主键并返回一个布尔值。我确信这里有更多值得关注的事情。
我应该寻找哪些其他问题或特定例外?
以下是我的程序的基本模板:
create or replace procedure delete_employee
( employee_id_i in employees.employee_id%type) is
begin
if valid_employee(employee_id_i) then
delete from employees where employee_id = employee_id_i;
end if;
exception
when others then
log_error_proc(dbms_utility.format_error_stack(),
dbms_utility.format_error_backtrace());
end delete_employees;
答案 0 :(得分:5)
您应该使用隐式光标的ROWCOUNT
属性。这将从DELETE
语句返回受影响的行数,从而使您无需查询valid_employee
函数中的表格。
create or replace procedure delete_employee
( employee_id_i in employees.employee_id%type) is
begin
delete from employees where employee_id = employee_id_i;
if SQL%ROWCOUNT == 0 then
null; -- throw exception, write log message, return something...
end if;
exception
when others then
log_error_proc(dbms_utility.format_error_stack(),
dbms_utility.format_error_backtrace());
end delete_employees;