最近在学plsql,苦于找不到练习题,所以自己写了一个用于练习.要求如下:
查找scott用户的emp表中的所有数据,如果job为MANAGER 工资大于2500,则下调百分之10;如果小于2500 则增加百分之10,如果job为SALESMAN 工资小于1500 则上调百分之10;
最后符合条件的人.插入到test表中.并且打印符合条件的人数的个数.
Test表结构如下:
Ename varchar
Sal number
大家可以做来练习一下.以下是我的答案:
create or replace procedure test_update_all as type emplist is table of emp%rowtype; empl emplist; coun number(3):=0; state number(1); procedure one_record(name_emp in emp.ename%type,job_name in emp.job%type,sal_emp in out emp.sal%type,coun in out number,sal_state in out number) as up_reails number(2,2):=0; low_reails number(2,2):=0; begin case when job_name='MANAGER' then if sal_emp>=2500 then low_reails:=.1; dbms_output.put_line(name_emp||' 工资大于2500 过高 ,下调百分之'||low_reails*100); sal_state:=1; coun:=coun+1; elsif sal_emp<2500 then low_reails:=.1; dbms_output.put_line(name_emp||' 工资大于2500 过低 ,上调百分之'||up_reails*100); sal_state:=1; coun:=coun+1; end if; when job_name='SALESMAN' then if sal_emp<1500 then begin low_reails:=.1; dbms_output.put_line(name_emp||' 工资大于1500 过低 ,上调百分之'||low_reails*100); sal_state:=1; coun:=coun+1; end; end if; else dbms_output.put_line(name_emp||'不符合条件,不进行更改'); end case; sal_emp:=sal_emp*(1+up_reails-low_reails); end one_record; begin select * bulk collect into empl from emp; for i in empl.first..empl.last loop state:=0; one_record(empl(i).ename,empl(i).job,empl(i).sal,coun,state); if state =1 then dbms_output.put_line(empl(i).ename||'的薪金更改为'||empl(i).sal); insert into test values(empl(i).ename,empl(i).sal); end if; end loop; dbms_output.put_line('更改的人数为:'||coun); end;
接触plsql时间尚浅,只是用最简单的方法实现.大牛们有更好的思路请跟帖告知.