Oracle 游标有三种:显示游标、隐式游标、参照游标。
fetch...bulk collect into
select 语句会把结果集全部返回给用户,而无法对结果集中的每行数据进行单独的操作。因此游标可以解决此问题。
隐式游标是在执行DML SQL语句时,Oracle自动创建的,名字固定叫sql
显示游标由用户自行创建
REF游标:REF游标用于处理运行时才能确定的动态SQL查询的结果
一、隐式游标
隐式游标包含的属性有:
%FOUND :SQL语句影响了一行或者多行时为ture
%NOTFOUND :SQL语句没有影响任何行时为ture
%ROWCOUNT :SQL语句影响的行数
%ISOPEN : 游标是否打开
新建表:
declare
sql_command varchar2(500);
isExsit number ;
begin
select count(1) into isExsit from user_tables where table_name ='SALARY_TBL';
if isExsit <> 0 then
sql_command :='truncate table salary_tbl';
execute immediate sql_command;
sql_command :='drop table salary_tbl';
execute immediate sql_command;
end if;
sql_command := '
create table salary_tbl(
employer_nm varchar(20) ,
department varchar(20) not null,
salary number not null,
leader_nm varchar(20)
)';
execute immediate sql_command;
for i in 1..13000
loop
insert into salary_tbl values('雇佣者'||i,'部门'||Mod(i,50),100+sqrt(i),'雇佣者'||Mod(i,20));
if Mod(i,1000)=0 then
commit;
end if;
end loop;
commit;
end;
/
--隐式游标示例
begin
update salary_tbl set DEPARTMENT ='部门3' where DEPARTMENT ='部门3';
if sql%found then
dbms_output.put_line('修改了'||sql%ROWCOUNT||'行');
end if;
rollback;
end;
/
二、显示游标
首先需要定义游标,然后打开游标,此时游标就指向了某个结果集。此时一行一行地读取,读完之后再关闭游标。
cursor curname (参数集) is SQLstatement;
----把领导者的薪资变成99999----
declare
sal salary_tbl.employer_nm%type;
cursor mycur is select distinct LEADER_NM from salary_tbl ;
begin
open mycur;
fetch mycur into sal;
while mycur%found loop
update salary_tbl set salary=99999 where employer_nm = sal;
fetch mycur into sal;
end loop;
close mycur;
end;
/
传参的显示游标示例:
----把除了部门9的领导者的薪资变成8888----
declare
sal salary_tbl.employer_nm%type;
cursor mycur(depnum number) is select distinct LEADER_NM from salary_tbl where
employer_nm in (select distinct LEADER_NM from salary_tbl)and department <>'部门'||depnum;
begin
open mycur(9);
fetch mycur into sal;
while mycur%found loop
update salary_tbl set salary=8888 where employer_nm = sal;
fetch mycur into sal;
end loop;
close mycur;
end;
/
上面都是利用游标来查询,游标还可以用来更新删除操作。
cursor curname (参数集) is SQLstatement for update;
current of curname
----把除了部门8的领导者薪资变成7777----
declare
sal salary_tbl%rowtype;
cursor mycur(depnum number) is select * from salary_tbl where
employer_nm in (select distinct LEADER_NM from salary_tbl)and department <>'部门'||depnum for update;
begin
open mycur(8);
fetch mycur into sal;
while mycur%found loop
update salary_tbl set salary=7777 where current of mycur;
fetch mycur into sal;
end loop;
close mycur;
end;
/
循环游标只能用于查询但是大大简化代码:
----把除了部门7的领导者薪资变成6666----
declare
sal salary_tbl%rowtype;
cursor mycur(depnum number) is select * from salary_tbl where
employer_nm in (select distinct LEADER_NM from salary_tbl)and department <>'部门'||depnum for update;
begin
for c1 in mycur(7) loop
update salary_tbl set salary=6666 where current of mycur;
end loop;
end;
/
练习:利用游标来初始化表,新建部门表(部门号,员工总薪资,领导人员)
declare
sql_command varchar2(500);
isExsit number ;
cursor dep_cur is select department,sum(salary) as allsalary from salary_tbl group by department;
depdata dep_cur%rowtype;
begin
select count(1) into isExsit from user_tables where table_name ='DEPARTMENT_TBL';
if isExsit <> 0 then
sql_command :='truncate table DEPARTMENT_TBL';
execute immediate sql_command;
sql_command :='drop table DEPARTMENT_TBL';
execute immediate sql_command;
end if;
sql_command := '
create table DEPARTMENT_TBL(
department varchar(20) not null,
all_salary number not null,
leaders varchar(100)
)';
execute immediate sql_command;
open dep_cur;
fetch dep_cur into depdata;
while dep_cur%found loop
insert into DEPARTMENT_TBL(department,all_salary) values(depdata.department,depdata.allsalary);
fetch dep_cur into depdata;
commit;
end loop;
close dep_cur;
end;
/
三、参照游标
当无法直接确定游标的定义时可以使用参考游标;
定义:TYPE typename IS REF CURSOR;
typename refcursorname;
赋值: OPEN refcursorname FOR sqlstatement;
例如:初始化 DEPARTMENT_TBL.leaders 获取部门下的所有领导者.;
---初始化部门中的leader----
declare
cursor cur is select distinct department from salary_tbl ; --所有的部门
leadernm DEPARTMENT_TBL.LEADERS%type; --某个部门下的所有领导者,待赋值
type refcursor is ref cursor;
refcur refcursor; --参照游标
refdata salary_tbl.leader_nm%type; --某个部门下的某个领导者,待赋值
begin
for curdata in cur loop
--遍历部门集cur,确定部门curdata下的所有领导者集合
open refcur for select distinct leader_nm from salary_tbl where department=curdata.department;
leadernm :='';
fetch refcur into refdata;
while refcur%found loop
--遍历领导者集合,字符串拼接
if leadernm is null then
leadernm := leadernm ||refdata;
else
leadernm := leadernm ||','||refdata;
end if;
fetch refcur into refdata;
end loop; ---/refcur
dbms_output.put_line(curdata.department || ' '||leadernm);
update DEPARTMENT_TBL set LEADERS=leadernm where department=curdata.department;
end loop; --/cur
--commit;
close refcur;
end;
/
新表的结果如图: