1、使用显式游标,和简单循环,读取employees表中某部门的所有员工信息,并显示。
declare
item employees%rowtype;
cursor v_employeescursor
is select * from employees
where department_id=100;
begin
open v_employeescursor;
loop
fetch v_employeescursor into item;
exit when v_employeescursor%notfound;
dbms_output.put_line('[id-'||
item.employee_id||']'
||item.first_name||' '||
item.last_name||' '||
item.email||' '||item.phone_number
||item.hire_date||' '
||' '||item.job_id||' '||
item.salary||' '||
item.commission_pct||' '||item.manager_id||' '
||item.department_id);
end loop;
close v_employeescursor;
end;
2、使用显式游标,和游标for循环,读取employees表中某部门的所有员工信息,并显示。
declare
item employees%rowtype;
cursor v_e
is select * from employees
where department_id=100;
begin
for item in v_e
loop
dbms_output.put_line('[id-'||
item.employee_id||']'
||item.first_name||' '||
item.last_name||' '||
item.email||' '||item.phone_number
||item.hire_date||' '
||' '||item.job_id||' '||
item.salary||' '||
item.commission_pct||' '||item.manager_id||' '
||item.department_id);
end loop;
end;
3、使用带变量的游标,和简单循环,读取employees表中几个部门的所有员工信息,并显示。
declare
TYPE emp_job_refcur_type IS REF CURSOR;
zz emp_job_refcur_type;
item employees%rowtype;
zzz number;
begin
zzz:=0;
while zzz<40
loop
zzz:=zzz+10;
OPEN zz FOR SELECT * FROM EMPLOYEES WHERE DEPARTMENT_ID=zzz;
loop
FETCH zz INTO item;
EXIT WHEN zz%NOTFOUND;
dbms_output.put_line('[id-'||
item.employee_id||']'
||item.first_name||' '||
item.last_name||' '||
item.email||' '||item.phone_number
||item.hire_date||' '
||' '||item.job_id||' '||
item.salary||' '||
item.commission_pct||' '||item.manager_id||' '
||item.department_id);
end loop;
dbms_output.put_line('deparrment_id:'||' '||zzz);
end loop;
CLOSE zz;
end;
4、使用带变量的游标,和游标for循环,读取employees表中几个部门的所有员工信息,并显示。
declare
zzz number;
begin
zzz:=0;
while zzz<40
loop
zzz:=zzz+10;
declare
cursor zz is SELECT * FROM EMPLOYEES WHERE DEPARTMENT_ID=zzz;
item zz%rowtype;
begin
for item in zz loop
dbms_output.put_line('[id-'||
item.employee_id||']'
||item.first_name||' '||
item.last_name||' '||
item.email||' '||item.phone_number
||item.hire_date||' '
||' '||item.job_id||' '||
item.salary||' '||
item.commission_pct||' '||item.manager_id||' '
||item.department_id);
end loop;
end;
dbms_output.put_line('deparrment_id:'||' '||zzz);
end loop;
end;
5、使用带参数的游标,和简单循环,读取employees表中几个部门的所有员工信息,并显示。
declare
item employees%rowtype;
cursor v_employeescursor(zz number)
is select * from employees where department_id=zz;
zzz number;
begin
zzz:=0;
dbms_output.put_line('显示游标带参数简单循环:');
while zzz<40 loop
zzz:=zzz+10;
open v_employeescursor(zzz);
loop
fetch v_employeescursor into item;
exit when v_employeescursor%notfound;
dbms_output.put_line('[id-'||
item.employee_id||']'
||item.first_name||' '||
item.last_name||' '||
item.email||' '||item.phone_number
||item.hire_date||' '
||' '||item.job_id||' '||
item.salary||' '||
item.commission_pct||' '||item.manager_id||' '
||item.department_id);
end loop;
close v_employeescursor;
dbms_output.put_line('department_id :'||' '||zzz);
end loop;
end;
6、使用带参数的游标,和游标for循环,读取employees表中几个部门的所有员工信息,并显示。
declare
cursor mycur(p_deptno number) is select * from employees where DEPARTMENT_id= p_deptno;
dno EMPLOYEES.DEPARTMENT_ID%type;
item employees%rowtype;
begin
dbms_output.put_line('显示游标带参数for循环');
dno:=0;
while dno<40 loop
dno:=dno+10;
for item in mycur(dno) loop
dbms_output.put_line('[id-'||
item.employee_id||']'
||item.first_name||' '||
item.last_name||' '||
item.email||' '||item.phone_number
||item.hire_date||' '
||' '||item.job_id||' '||
item.salary||' '||
item.commission_pct||' '||item.manager_id||' '
||item.department_id);
end loop;
dbms_output.put_line('department_id :'||' '||dno);
end loop;
end;
7、使用游标,读取公司各部门的员工的工资信息,统计各部门的总工资,各部门员工的工资按逆序显示,并显示其所在部门工资的排名。(不要使用rollup和rank)
DECLARE
cursor zz(hh number) is SELECT employee_id,SALARY,department_id from employees where DEPARTMENT_id=hh order by salary desc;
sum1 employees.SALARY%type;
i number;
ll number;
begin
i:=0;
while i <270 loop
i:=i+10;
sum1:=0;
ll:=0;
for item in zz(i) loop
sum1:=sum1+item.SALARY;
ll:=ll+1;
dbms_output.put_line(item.EMPLOYEE_ID||' '||item.SALARY||' '||' '||ll);
end loop;
dbms_output.put_line(i||': '||sum1);
end loop;
end;
8、使用for update游标,读取公司员工的工资信息,按照一定的策略,更新员工的工资信息,下面为将每个员工工资加111。
DECLARE
cursor zz is SELECT * from employees;
item zz%rowtype;
begin
for item in zz loop
update employees set EMPLOYEES.salary=employees.salary+111 where employees.employee_id=item.employee_id;
end loop;
end;