游标的使用

–无参显游标,%type接收
declare
cursor dept_cursor is select deptno,dname from dept;
v_deptno dept.deptno%type;
v_dname dept.dname%type;
begin
open dept_cursor;
loop
fetch dept_cursor into v_deptno,v_dname;
exit when dept_cursor%notfound;
dbms_output.put_line(‘部门号为:’||v_deptno||’,部门名为:’||v_dname);
end loop;
close dept_cursor;
end;

–无参显游标,%rowtype接收
declare
cursor dept_cursor is select deptno,dname,loc from dept;
v_record dept%rowtype;
begin
open dept_cursor;
loop
fetch dept_cursor into v_record;
exit when dept_cursor%notfound;
dbms_output.put_line(‘部门号为:’||v_record.deptno||’,部门名为:’||v_record.dname||’,部门地址为:’||v_record.loc);
end loop;
close dept_cursor;
end;

–无参显游标,集合接收
declare
cursor dept_cursor is select deptno,dname,loc from dept;
type dept_table_type is table of dept_cursor%rowtype index by binary_integer;
dept_table dept_table_type;
idx number;
begin
open dept_cursor;
loop
idx:=dept_cursor%rowcount+1;
fetch dept_cursor into dept_table(idx);
exit when dept_cursor%notfound or dept_cursor%rowcount>3;
dbms_output.put_line(‘部门号为:’||dept_table(idx).deptno||’,部门名为:’||dept_table(idx).dname||’,部门地址为:’||dept_table(idx).loc);
end loop;
close dept_cursor;
end;

–游标for循环
declare
cursor dept_cursor is select deptno,dname,loc from dept;

begin

for v_record in dept_cursor loop

exit when dept_cursor%notfound or dept_cursor%rowcount=#
dbms_output.put_line(‘部门号为:’||v_record.deptno||’,部门名为:’||v_record.dname||’,部门地址为:’||v_record.loc);
end loop;
end;

–修改游标所在位置
declare
v_deptno emp.deptno%type:=&deptno;
cursor emp_cursor is select empno,sal from emp where deptno=v_deptno for update nowait;

begin
for emp_record in emp_cursor loop
if emp_record.sal<1500 then
dbms_output.put_line(‘雇员号为:’||emp_record.empno||’,工资为:’||emp_record.sal);
update emp set sal=1500 where current of emp_cursor;
end if;
end loop;
commit;
end;

–删除游标所在位置
declare
v_deptno emp.deptno%type:=&deptno;
cursor emp_cursor is select empno,sal,deptno from emp for update nowait;

begin
for emp_record in emp_cursor loop
if emp_record.sal<1500 and emp_record.deptno=v_deptno then
dbms_output.put_line(‘雇员号为:’||emp_record.empno||’,工资为:’||emp_record.sal);
delete from emp where current of emp_cursor;
end if;
end loop;
commit;
end;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值