不需要单独的步骤。只需尝试更新,如果没有行被更新,请这样说。如果您希望它是一个例外,那么使用
raise_application_error
。
假设这是一个学习练习,这就是为什么你不想做一个普通的
update
,您可以这样做:
declare
k_job constant emp3.job%type := '&JOB';
cursor employees_cur is
select * from emp3
where job = k_job
for update of sal;
v_update_count integer := 0;
v_payroll_increase integer := 0;
begin
for r in employees_cur loop
update emp3 set sal = sal + 100 where current of employees_cur;
dbms_output.put_line('Salary for ' || r.ename || ' is incremented by $100 from $' || r.sal || ' to $' || (r.sal +100));
v_update_count := v_update_count + 1;
v_payroll_increase := v_payroll_increase + 100;
end loop;
if v_update_count = 0 then
dbms_output.put_line('No staff are currently employed as ' || k_job ||'. Payroll is unchanged.');
else
dbms_output.put_line('Updated salary of '|| v_update_count ||' employee' || case when v_update_count <> 1 then 's' end||'.');
dbms_output.put_line('Payroll increased by $'||v_payroll_increase||'.');
end if;
end;
/
Enter value for job: SALESMAN
Salary for ALLEN is incremented by $100 from $1600 to $1700
Salary for WARD is incremented by $100 from $1250 to $1350
Salary for MARTIN is incremented by $100 from $1250 to $1350
Salary for TURNER is incremented by $100 from $1500 to $1600
Updated salary of 4 employees.
Payroll increased by $400.
PL/SQL procedure successfully completed.
对于一个不存在的工作,您可以得到:
Enter value for job: ASTRONAUT
No staff are currently employed as ASTRONAUT. Payroll is unchanged.
(在本例中,
v_payroll_increase
总是100次
v_update_count
,但如果你想加薪10%或按部门等不同的幅度加薪,可能会更有用。)