游标的基本语法

declare
--定义显示游标
cursor emp_cursor is
select ename,sal from emp where deptno=30;
--定义变量用于保存ename和sal
v_ename emp.ename%type;
v_sal emp.sal%type;
begin

open emp_cursor;--打开游标
loop
fetch emp_cursor into v_ename,v_sal;--从游标中提取数据
exit when emp_cursor%notfound;
--输出
dbms_output.put_line(v_ename || '    ' ||v_sal);
end loop;
close emp_cursor;
end;



--2
declare
--定义显示游标
cursor emp_cursor is select ename,sal from emp where deptno=30;
--定义记录集用于封装雇员名和工资
type emp_rocord is record
(
ena emp.ename%type,
esal emp.sal%type
);
--定义table类型用于一次性提取多条记录
type ENAME_SAL_TABLE is table of emp_rocord;
--定义ENAME_SAL_TABLE变量
v_ename_table ENAME_SAL_TABLE;
begin
--打开游标
open emp_cursor;

--将游标内所有的行一次性提取到table类型变量中
fetch emp_cursor bulk collect into v_ename_table;
--关闭游标
close emp_cursor;
--循环table输出元素值
for i in v_ename_table.FIRST..v_ename_table.LAST loop
--输出结果
dbms_output.put_line(v_ename_table(i).ena||  '     '||v_ename_table(i).esal);
end loop;
end;




--3基于游标定义记录变量
declare 
--定义游标
cursor emp_cursor is select ename,sal from emp where deptno=10;
--定义游标记录类型
emp_record emp_cursor%rowtype;
begin
open emp_cursor;--打开游标
loop
--提取游标中的一行数据到emp_record记录类型
fetch emp_cursor into emp_record;
--当提取不到数据时,退出循环
exit when emp_cursor%notfound;
--输出信息
dbms_output.put_line(emp_record.ename||'    '||emp_record.sal);

end loop;
close emp_cursor;--关闭游标
end;

--参数游标
declare 
--定义有参数的游标,参数为Param_dept
cursor emp_cursor(param_dept number) is select ename,sal from emp where deptno=param_dept;
--定义游标记录类型
emp_record emp_cursor%rowtype;
begin 
open emp_cursor(10);
loop
--提取游标中的一行到记录集
fetch emp_cursor into emp_record;
--当提取不到数据退出循环
exit when emp_cursor%notfound;
--输出信息
dbms_output.put_line(emp_record.ename||'   '||emp_record.sal);
end loop;
--关闭游标
close emp_cursor;

end;





--使用游标更新数据
declare 
--定义游标
cursor emp_cursor is select ename,sal from emp for update;
--定义游标变量用于保存游标行数据
v_emp_row emp_cursor%rowtype;
--定义用于记录被更新雇员的个数
v_update_emp_count number(2):=0;
begin
--打开游标
open emp_cursor;
loop 
fetch emp_cursor into v_emp_row;--提取游标当前行数据
exit when emp_cursor%notfound;--提取完数据后,退出循环
if v_emp_row.sal<2000 then --如果当前雇员的工资小于2000
--更新当行雇员工资
update emp set sal=sal+100 where current of emp_cursor;
v_update_emp_count:=v_update_emp_count+1;--个数加1
end if;
end loop;
dbms_output.put_line('共有'||v_update_emp_count||'名雇员被更新了!');
--关闭游标
close emp_cursor;
end;




--使用游标删除数据
declare
--定义游标
cursor emp_curso is select * from emp for update;
--定义游标变量用于保存游标数据行
v_emp_row emp_curso%rowtype;
--定义被删除雇员的个数
v_del_emp_count number(2):=0;
begin
--打开游标
open emp_curso;
--提取一行到记录集
loop
fetch emp_curso into v_emp_row;
exit when emp_curso%notfound;
if v_emp_row.deptno=30 then 
delete emp where current of emp_curso;
v_del_emp_count:=v_del_emp_count+1;--个数加1
end if;
end loop;
dbms_output.put_line('共有'||v_del_emp_count||'名雇员被删除了!');
--关闭游标
close emp_curso;
end;



--游标的for循环
declare
--定义游标
cursor emp_cursor is select ename,sal from emp;
begin
for emp_row in emp_cursor loop
dbms_output.put_line('第'||emp_cursor%rowcount||'个雇员:'||emp_row.ename);
end loop;
end;



--在游标中使用for循环
begin
for emp_row in (select * from emp) loop

dbms_output.put_line(emp_row.ename);
end loop;

end; 


--使用游标变量

declare 
--定义ref cursor游标类型
type emp_cursor_type is ref cursor;
--通过类型定义游标变量
emp_cursor emp_cursor_type;
emp_row emp%rowtype;
begin
--打开游标变量
open emp_cursor for select * from emp where deptno=30;
loop
fetch emp_cursor into emp_row;--提取游标当前行数据
exit when emp_cursor%notfound;--提取完数据后,退出循环
--输出
dbms_output.put_line('第'||emp_cursor%rowcount||'个雇员:'||emp_row.ename);
end loop;
close emp_cursor;


end;




--使用游标变量开发返回结果集的子程序
create or replace procedure proc_getEmpsByDeptno
(
--定义部门编号in参数
param_deptno number,
--定义SYS_REFCURSOR类型out参数,返回结果集
param_resultset out sys_refcursor

)as

begin

--打开游标
open param_resultset for select ename,sal from emp where deptno=param_deptno;

end;



declare
--定义record记录类型
type EMP_RECORD_TYPE is record
(
ename varchar2(10),
sal number(7,2)
);
--定义sys_refcursor类型变量
v_emp_rows sys_refcursor;
--定义变量用于保存部门编号
v_deptno number(2):=10;
--定义记录变量用于保存雇员信息
v_emp_row EMP_RECORD_TYPE;
begin
--调用过程
proc_getEmpsByDeptno(v_deptno,v_emp_rows);
--使用for循环访问游标的每一行
loop

fetch v_emp_rows into v_emp_row;--提取游标行数据
exit when v_emp_rows%notfound;
--输出信息
dbms_output.put_line('第'||v_emp_rows%rowcount||'个雇员的名称:'||v_emp_row.ename||'工资:'||v_emp_row.sal);
end loop;
close v_emp_rows;
end;






 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值