目录
1、声明游标
2、打开游标
3、读取数据
4、关闭游标
隐士游标 不要打开也不需要关闭。实际上就是 执行select into的时候讲结果集自动读取到制定的变量中
声明一个游标
declare sursor 游标名
[参数列表]
is
select 语句;
ORACLE的游标和SQLSERVER有些不一样
声明游标 一般和变量在一起声明
declare cursor mycursor(varType number)
is
select name,value from users where type=vartype;
参数vartype在打开游标时指定
打开游标
open mycursor(1);
取值 游标必须先打开才能取值
fetch mycursor into 变量列表;
关闭游标
close 游标名
下面是一个完整的小例子
declare
operation AHVIEW_RPT1_EQPSTATUSSMY_SEV.OPERATION%type :=N'H-DFSR曝光';
RESOURCENAME AHVIEW_RPT1_EQPSTATUSSMY_SEV.RESOURCENAME%type;
DURSTARTTIME AHVIEW_RPT1_EQPSTATUSSMY_SEV.DURSTARTTIME%type;
cursor mycursor(namee varchar2)----这里要注意 游标参数类型 不需要带大小,不然会报错
is
select RESOURCENAME,DURSTARTTIME from AHVIEW_RPT1_EQPSTATUSSMY_SEV where operation=namee;
begin
open mycursor(operation);
fetch mycursor into RESOURCENAME,DURSTARTTIME;
close mycursor;
dbms_output.put_line(RESOURCENAME||' '||to_char(DURSTARTTIME,'yyyyMMdd HH24 mi ss'));
end;
游标属性
%isopen %found %notfound %rowcount 游标读取的记录行数,从1开始
修改后第二版
declare
operation AHVIEW_RPT1_EQPSTATUSSMY_SEV.OPERATION%type :=N'H-DFSR曝光';
RESOURCENAME AHVIEW_RPT1_EQPSTATUSSMY_SEV.RESOURCENAME%type;
DURSTARTTIME AHVIEW_RPT1_EQPSTATUSSMY_SEV.DURSTARTTIME%type;
cursor mycursor(namee varchar2)
is
select RESOURCENAME,DURSTARTTIME from AHVIEW_RPT1_EQPSTATUSSMY_SEV where operation=namee;
begin
if mycursor%isopen=false then
open mycursor(operation);
end if;
fetch mycursor into RESOURCENAME,DURSTARTTIME;
while mycursor%found
loop
dbms_output.put_line(RESOURCENAME||' '||to_char(DURSTARTTIME,'yyyyMMdd HH24 mi ss'));
fetch mycursor into RESOURCENAME,DURSTARTTIME;
end loop;
close mycursor;
end;
首先介绍 记录(record)
TYPE typename is record
(
userid User.UserID%TYPE,
username User.Username%TYPE
);
userRecord typename ;--声明变量
如果声明的变量和表结构一样, 变量名 表明%rowtype;
修改后第三版
declare
operation AHVIEW_RPT1_EQPSTATUSSMY_SEV.OPERATION%type :=N'H-DFSR曝光';
RESOURCENAME AHVIEW_RPT1_EQPSTATUSSMY_SEV.RESOURCENAME%type;
DURSTARTTIME AHVIEW_RPT1_EQPSTATUSSMY_SEV.DURSTARTTIME%type;
cursor mycursor(namee varchar2)
is
select RESOURCENAME,DURSTARTTIME from AHVIEW_RPT1_EQPSTATUSSMY_SEV where operation=namee;
Type tempinfo is record
(
RESOURCENAME AHVIEW_RPT1_EQPSTATUSSMY_SEV.RESOURCENAME%type,
DURSTARTTIME AHVIEW_RPT1_EQPSTATUSSMY_SEV.DURSTARTTIME%type
);
info tempinfo;--定义 record变量
begin
if mycursor%isopen=false then
open mycursor(operation);
end if;
loop
fetch mycursor into info;
exit when mycursor%notfound;
dbms_output.put_line(mycursor%rowcount);
dbms_output.put_line(info.RESOURCENAME||' '||to_char(info.DURSTARTTIME,'yyyyMMdd HH24 mi ss'));
end loop;
close mycursor;
end;
游标for循环
不能使用 open fetch close ,不然会报错
第三版修改
declare
operation AHVIEW_RPT1_EQPSTATUSSMY_SEV.OPERATION%type :=N'H-DFSR曝光';
cursor mycursor(namee varchar2)
is
select RESOURCENAME,DURSTARTTIME from AHVIEW_RPT1_EQPSTATUSSMY_SEV where operation=namee;
begin
for mytemp in mycursor(operation)
loop
dbms_output.put_line(mycursor%rowcount);
dbms_output.put_line(mytemp.RESOURCENAME||' '||to_char(mytemp.DURSTARTTIME,'yyyyMMdd HH24 mi ss'));
end loop;
end;
或者
不用 游标的 for循环
for tempdata in (select * from AHVIEW_RPT1_EQPSTATUSSMY_SEV)
loop
dbms_output.put_line(11);
end loop;
引用游标
特点是不依赖指定的查询语句,可以在使用时关联不一样的查询语句
分为强游标和弱游标两类
弱游标
declare
Type refcur is ref cursor;
inforow AHVIEW_RPT1_EQPSTATUSSMY_SEV%rowtype;
myrefcur refcur;
begin
open myrefcur for select * from AHVIEW_RPT1_EQPSTATUSSMY_SEV where WORKCENTER=N'电镀课';
fetch myrefcur into inforow;
while myrefcur%found
loop
dbms_output.put_line(inforow.WORKCENTER);
fetch myrefcur into inforow;
end loop;
close myrefcur;
end;
注意 游标的%found 必须先 fetch 一下,才有用
强游标则
Type refcur is ref cursor return AHVIEW_RPT1_EQPSTATUSSMY_SEV%rowtype;
指定返回类型
在定义游标时,可以指定允许更新或者删除游标结果集中的行
1、修改游标集中的行
declare cursor mycursor (mytype number)
is
select * from 表明 where 条件
for update of 栏位名称;
在使用update 语句修改游标中数据时,需要使用 where current of 指定要更新的游标
比如
declare cursor mycursor(vardeptid number) is
select name,salary from employee where deptid=vardeptid
for update of salary;
begin
for rec1 in mycursor(1)
loop
update employee set salary=salary+100
where current of myempcursor; --条件是当前游标中记录
end loop;
end;
删除游标结果集中的行
delete from 表 where current of 游标名;
将 上面的替换成
delete from employee where current of mycursor;