Oracle游标
1.隐式游标
1.隐式游标:系统自动维护的,在我们做DML操作的时候系统会自动的维护这样一个游标
名称叫:sql
隐式游标提供的常用的属性:
sql%found: boolean 找到数据 true
sql%notfound: boolean 没有找到数据 true
sql%rowcount: 数值型 影响的行数
sql%isopen: 是否打开 dml中都是 false
插入:select * from student;
begin
-- 插入一条记录
--insert into student(id,name,sex)values(seq_t_student.nextval,'黄开彬','男') ;
-- update student set sex='女' ;
delete from student where id between 120 and 130;
-- 判断sql游标是否有数据
if sql%found then
dbms_output.put_line('影响了:'||sql%rowcount||'条记录');
end if;
if sql%isopen then
dbms_output.put_line('--1-------');
end if;
-- 提交数据 提交的同时会清空隐式游标中的数据
commit;
if sql%found then
dbms_output.put_line('----');
end if;
if sql%isopen then
dbms_output.put_line('---2------');
end if;
end;
2.显式游标
2.显示游标:处理多行数据,隐式游标配合显示游标使用
2.1无参游标
查询出学生表中的所有的记录:
使用的步骤:
1.声明游标
2.打开游标
3.循环提取数据
4.关闭游标
declare
v_student student%rowtype;
-- 1.声明游标
cursor mycursor is select * from student;
begin
--2.打开游标 指向第一行数据之前
open mycursor;
--3.循环提取数据
loop
--提取数据
-- 每循环一次从游标中取一条记录保存到v_student变量中
fetch mycursor into v_student;
--指定退出条件
exit when mycursor%notfound;
dbms_output.put_line(v_student.id||v_student.name||v_student.sex);
end loop;
--4.关闭游标
close mycursor;
end;
List<Student> list = new ArrayList();
for(Student stu:list){
System.out.println(stu);
}
2.2 有参游标
根据姓名查询学生表中的所有的学生信息
declare
v_student student%rowtype;
v_name student.name%type := '&请输入要查询的姓名';
cursor mycursor(c_name varchar2) -- 带有参数
is select * from student where name like '%'||c_name||'%';
begin
open mycursor(v_name); --打开的时候需要指定参数
loop
fetch mycursor into v_student;
if mycursor%found then
-- 有数据
dbms_output.put_line(v_student.id||v_student.name||v_student.sex);
else
-- 退出
exit;
end if;
end loop;
close mycursor;
end;
declare
v_student student%rowtype;
v_name student.name%type := '&请输入要查询的姓名';
cursor mycursor -- 带有参数
is select * from student where name like '%'||v_name||'%';
begin
open mycursor; --打开的时候需要指定参数
loop
fetch mycursor into v_student;
if mycursor%found then
-- 有数据
dbms_output.put_line(v_student.id||v_student.name||v_student.sex);
else
-- 退出
exit;
end if;
end loop;
close mycursor;
end;
2.3 游标循环时使用for循环提取
declare
cursor mycursor is select * from student;
begin
-- 注意:for循环会帮助我们维护游标【打开和关闭操作】
-- 循环变量可以不用事先显示的声明
for v_student in mycursor loop
dbms_output.put_line(v_student.id||v_student.name||v_student.sex);
end loop;
end;
2.4 更新行数据
查询所有的学生信息并将birth为空的记录更新为2000;
declare
v_student student%rowtype;
cursor mycursor is select * from student for update; -- 1. for update
begin
open mycursor;
loop
fetch mycursor into v_student;
exit when mycursor%notfound;
dbms_output.put_line(v_student.id||v_student.name||v_student.sex||v_student.birth);
if v_student.birth is null then
-- 2.在更新语句后加 current of 游标名称
update student set birth=2000 where current of mycursor;
end if;
end loop;
commit;
close mycursor;
end;
3.ref游标(动态游标)
3.REF游标【动态游标】:是解决游标动态执行sql
显示游标在声明的时候就必须制定sql语句
动态游标:在打开的时候确定要执行的sql语句比显示游标更加的灵活
缺点:不能使用for循环和更新行
3.1 自定义ref游标
通过REF游标查询出学生表中的所有的学生记录
declare
-- Oracle系统中给我们提供类型 number varchar2 exception
type myreftype is ref cursor; -- 1.定义一个ref 类型
myrefcursor myreftype; -- 2.声明一个myreftype类型的变量
v_student student%rowtype;
v_sql varchar2(100);
begin
v_sql := 'select * from student where 1=1 ';
-- for 后及可以带'' 也可以直接是sql语句
--open myrefcursor for select * from student; -- 打开游标的同时指定要执行的sql语句
open myrefcursor for v_sql;
loop
fetch myrefcursor into v_student;
exit when myrefcursor%notfound;
dbms_output.put_line(v_student.id||v_student.name||v_student.sex||v_student.birth);
end loop;
close myrefcursor;
end;
3.2 sys_refcursor:系统提供的一个 ref cursor 类型
declare
myrefcursor sys_refcursor; -- 声明一个变量类型是 ref cursor 类型
v_student student%rowtype;
v_sql varchar2(100);
begin
open myrefcursor for select * from student;
loop
fetch myrefcursor into v_student ;
exit when myrefcursor%notfound;
dbms_output.put_line(v_student.id||v_student.name||v_student.sex||v_student.birth);
end loop ;
close myrefcursor;
end;