1、游标的说明:游标是一种向包含多条数据记录的结果集中每次读取一行的机制,逐行处理查询结果,以编程的方式访问数据库。可以把游标当成指针,可以指定结果集中的任何位置,然后允许用户对指定位置的数据进行操作。SQL的游标是一种临时数据库对象,可以临时存放数据表中的数据行副本,也可以指向存储在数据表中数据行指针。
2、游标的种类及使用
(1)隐式游标:在PLSQL程序中执行DML SQL语句(insert、update、delete、select...into...)时自动创建隐式游标,自动打开和关闭,名字固定叫sql。隐式游标是静态游标
可以通过隐式游标的属性来了解操作的状态和结果,进而控制程序的流程;sql游标名只能访问前一个DML操作的游标属性,所以在DML操作完成后立即使用sql游标名来访问游标属性
隐式游标属性:%notfound:SQL语句影响一行或多行数据时为ture
%found:SQL语句没有影响任何行时为ture
%rowcount:返回SQL语句影响的行数
%isopen:判断游标是否打开,隐式游标的该属性一直是flase,因为隐式游标在执行DML SQL语句时打开,执行结束后关闭
例:begin
--insert into test(code,name) values ('aa','张三');
update test set code='bb';
dbms_output.put_line('游标影响的行数'||sql%rowcount);
if sql%notfound then
dbms_output.put_line('notfound为真');
else
dbms_output.put_line('notfound为假');
end if;
if sql%found then
dbms_output.put_line('found为真');
else
dbms_output.put_line('found为假');
end if;
if sql%isopen then
dbms_output.put_line('isopen为真');
else
dbms_output.put_line('isopen为假');
end if;
commit;
end;
(2)显式游标:用于处理返回多行的查询
无参游标语法:cursor <游标名> is select语句; --声明游标
open <游标名>; --打开游标
fetch <游标名> into <用于保存数据的变量名>; --移动游标并获取数据
close <游标名>; --关闭游标
例1:无参游标,loop循环
declare
cursor cur is select code,name from test; --声明游标cur
temp cur%rowtype; --声明一个游标变量,通配游标一行所有列数据类型
begin
execute immediate 'truncate table test_01';
open cur;--打开游标
loop
fetch cur into temp;--将游标所在行的数据转存到temp中
exit when cur%notfound;--当游标到最后一行时退出
insert into test_01(code,name)
values(temp.code,temp.name);
end loop;
commit;
close cur;--关闭游标
end;
例2:无参游标,while循环
declare
cursor cur is select code,name from test; --声明游标
m1 varchar2(20); --声明变量
m2 varchar2(20);
begin
execute immediate 'truncate table test_01';
open cur;--打开游标
fetch cur into m1,m2;--将第一行数据转存到m1,m2中!!!
while cur%found --判断是否有数据
loop
insert into test_01(code,name) values(m1,m2);
fetch cur into m1,m2;--将下一行的数据转存到m1,m2中!!!
end loop;
commit;
close cur;--关闭游标
end;
例3:无参游标,for循环
declare
cursor cur is select code,name from test; --声明变量
begin
execute immediate 'truncate table test_01';
for temp in cur --使用for...in...可以省略打开和关闭游标的操作
loop
insert into test_01(code,name) values(temp.code,temp.name);
end loop;
commit;
end;
通配类型操作符:
%rowtype:通配一行所有列数据类型
%type:通配某行某列数据类型
游标属性:(游标属性必须在关闭游标之前)
%isopen:判断游标是否打开
%isfound:找不到数据时
%found:找到数据时
%rowcount:返回当前游标已扫描的数据行数量
(3)动态游标:ref游标也叫动态游标,ref游标和游标变量用于处理动态执行的SQL查询
语法:type <ref游标类型名> is ref cursor;--声明ref游标类型
<游标名> <ref游标类型名>;--声明ref游标变量
open 游标名 for select语句;--打开游标
fetch <游标名> into <用于保存数据的变量名>;--移动游标并获取数据
close <游标名>;--关闭游标
例:
declare
type cur is ref cursor;--声明ref游标类型
cur1 cur;--声明ref游标变量
m1 varchar2(30); --声明变量,用于存放游标中取出的数据(注:动态游标不能使用%type或%rowtype来作为存放游标取出数据的变量的类型)
m2 varchar2(30);
begin
execute immediate 'truncate table test_01';--动态sql
open cur1 for select code,name from test; --打开游标
loop
fetch cur1 into m1,m2;--移动游标并获取数据,将数据存储在m1、m2中
exit when cur1%notfound;
insert into test_01(code,name) values(m1,m2);
end loop;
commit;
close cur1;
end;
3、循环语句:游标通常搭配循环语句使用
a、loop循环语法:
loop
exit when 表达式;
执行语句;
end loop;
b、while循环语法:
while 表达式;
loop
执行语句;
end loop;
c、for循环语法:
for <变量> in <变量取值范围(小值...大值 如:1...100)>
loop
执行语句;
end loop;