实验八 游标
一、实验目的与要求:
1.学会使用隐式游标和显示游标
2.掌握游标的常用属性
3.掌握对显示游标的处理步骤
4.学会使用游标操纵数据库
二、实验内容:
1.用游标显示部门 10 中员工的员工姓名、工作种类和薪金。
declare
cursor m_cursor is select ename,job,sal from emp where deptno=10;
begin
for r in m_cursor loop
dbms_output.put_line(r.ename||' ');
dbms_output.put_line(r.job||' ');
dbms_output.put_line(r.sal||' ');
end loop;
end;
/2.用游标实现以下功能:
对所有员工,如果该员工职位是 MANAGER,并且在 DALLAS 城市工作那么就给他薪金
加 15%;如果该员工职位是 CLERK,并且在 NEW YORK 城市工作那么就给他薪金扣除
5%;其他情况不作处理。
declare
cursor a_cursor is select job,loc from emp,dept where emp.deptno
=dept.deptno for update of sal;
begin
for r in a_cursor loop
if r.job='MANAGER' and r.loc='DALLS' then
update emp set sal=sal*1.15 where current of a_cursor;
elsif r.job='CLERK' and r.loc='NEW YORE' then
update emp set sal=sal*0.95 where current of a_cursor;
end if;
end loop;
end;
/
3.用游标实现以下功能:
为所有的经理涨工资,每人提升 10%,如果工作超过 5 年的话,在加 3000 元。
declare
v_job emp.job%type;
riqi emp.hiredate%type;
cursor th_cursor is select job,hiredate from emp for update of sal;