declare
sal_null_exc exception;
pragma exception_init(sal_null_exc, -20001);
v_sal emp.sal%type;
v_comm emp.comm%type;
counts number := 0;
begin
for v_empno in -5 .. 9999 loop
select sal into v_sal from emp where empno = v_empno;
if v_sal < 1000 then
v_comm := v_sal * 0.1;
counts := counts + 1;
elsif v_sal between 1000 and 1500 then
v_comm := v_sal * 0.15;
counts := counts + 1;
elsif v_sal > 1500 then
v_comm := v_sal * 0.2;
counts := counts + 1;
elsif v_sal is null then
raise sal_null_exc;
else
null;
end if;
update emp set comm = v_comm where empno = v_empno;
exception
when no_data_found then
dbms_output.put_line('没有找到员工号: '||v_empno);
when sal_null_exc then
dbms_output.put_line('工资为空'||sqlcode||sqlerrm);
when others then
rollback;
end loop;
dbms_output.put_line('一共修改了' || counts || '条记录');
end;
sal_null_exc exception;
pragma exception_init(sal_null_exc, -20001);
v_sal emp.sal%type;
v_comm emp.comm%type;
counts number := 0;
begin
for v_empno in -5 .. 9999 loop
select sal into v_sal from emp where empno = v_empno;
if v_sal < 1000 then
v_comm := v_sal * 0.1;
counts := counts + 1;
elsif v_sal between 1000 and 1500 then
v_comm := v_sal * 0.15;
counts := counts + 1;
elsif v_sal > 1500 then
v_comm := v_sal * 0.2;
counts := counts + 1;
elsif v_sal is null then
raise sal_null_exc;
else
null;
end if;
update emp set comm = v_comm where empno = v_empno;
exception
when no_data_found then
dbms_output.put_line('没有找到员工号: '||v_empno);
when sal_null_exc then
dbms_output.put_line('工资为空'||sqlcode||sqlerrm);
when others then
rollback;
end loop;
dbms_output.put_line('一共修改了' || counts || '条记录');
end;