游标的作用:处理多行数据,类似与java中的集合
一般是配合显示游标去使用的,不需要显示声明,打开,关闭,系统自定维护,名称为:sql
常用属性:
案例:
begin
update t_student set age=20 ;
if sql%found then
dbms_output.put_line('修改成功,共修改了 ' || sql%rowcount || ' 条记录');
else
dbms_output.put_line('没有这个学生');
end if;
-- commit ;-- 提交应该要放在隐式游标后面
end ;
显式游标在PL/SQL块的声明部分定义查询,该查询可以返回多行,处理多行数据
实现步骤:
案例:
a) 无参数 :查询所有学生信息,并显示出学生姓名,性别,年龄
-- 步骤:1.声明一个游标 2.打开游标 3.循环提取数据 4.关闭游标
-- 查询所有的学生信息。并且显示学生的姓名,年龄和性别
declare
v_row t_student%rowtype;
-- 1.游标的声明
cursor mycursor is select * from t_student ;
begin
-- 2.打开游标
open mycursor;
-- 3.循环提取数据
loop
fetch mycursor into v_row;
-- 找到出口
exit when mycursor%notfound;
dbms_output.put_line(v_row.name||'-'||v_row.gender||'-'||v_row.age);
end loop;
-- 4.关闭游标
close mycursor;
end;
b) 有参数:
declare
v_sex varchar2(4) :='&请输入性别' ;
v_row t_student%rowtype ;
cursor mycursor(p_sex varchar2) is select * from t_student where sex=p_sex ; -- 注:参数的类型不要指定长度大小
begin
open mycursor(v_sex) ;-- 2、打开游标
loop
fetch mycursor into v_row;
exit when mycursor%notfound;
dbms_output.put_line(v_row.stuname || ',' || v_row.sex || ',' || v_row.age);
end loop;
close mycursor;-- 4、 关闭游标
end ;
c ) 循环游标. 简化 游标 for:不需要打开游标 也不需要关闭游标
declare
v_sex varchar2(4) :='&请输入性别' ;
cursor mycursor(p_sex varchar2) is select * from t_student where sex=p_sex ; -- 注:参数的类型不要指定长度大小
begin
for v_row in mycursor(v_sex) loop
dbms_output.put_line(v_row.stuname || ',' || v_row.sex || ',' || v_row.age);
end loop;
end ;
d) 使用显式游标更新行:
允许使用游标删除或更新活动集中的行,声明游标时必须使用 select … for update 语句。
declare
v_sex varchar2(4) :='&请输入性别' ;
v_row t_student%rowtype ;
cursor mycursor(p_sex varchar2) is select * from t_student where sex=p_sex for update; -- 注:参数的类型不要指定长度大小
begin
open mycursor(v_sex) ;-- 2、打开游标
loop
fetch mycursor into v_row;
exit when mycursor%notfound;
-- dbms_output.put_line(v_row.stuname || ',' || v_row.sex || ',' || v_row.age);
update t_student set age = age +10 where current of mycursor;
end loop;
--commit ;
close mycursor;-- 4、 关闭游标
end ;
处理运行时动态执行的 SQL 查询,特点:
优点:
缺点:
使用步骤:
案例讲解
declare
v_sex varchar2(4) ;
--type mytype is ref cursor return t_student%rowtype; -- 强类型的 ref 游标类型
type mytype is ref cursor ; -- 1)弱类型的 ref 游标类型
mycursor mytype; -- 2) 声明游标
v_sql varchar2(100) ;
v_row t_student%rowtype ;
begin
v_sql :=' select * from t_student ' ;
-- open mycursor for select * from t_student;
open mycursor for v_sql ;
loop
fetch mycursor into v_row ;
exit when mycursor%notfound ;
dbms_output.put_line(v_row.stuname || ',' || v_row.sex || ',' || v_row.age);
end loop;
close mycursor ;
end ;
可以使用sys_refcursor类型
declare
v_stuname t_student.stuname%type :='&请输入名字' ;
v_sex varchar2(3) :='&请输入性别' ;
mycursor sys_refcursor ; -- 2) 声明游标
v_sql varchar2(100) ;
v_row t_student%rowtype ;
begin
v_sql :='select * from t_student where 1=1 ';
if v_stuname is not null then
v_sql :=v_sql || ' and stuname like ''%' || v_stuname || '%'' ' ;
end if;
if v_sex is not null then
v_sql :=v_sql || ' and sex = ''' || v_sex || ''' ' ;
end if;
dbms_output.put_line('v_sql= ' || v_sql );
-- open mycursor for select * from t_student;
open mycursor for v_sql ;
loop
fetch mycursor into v_row ;
exit when mycursor%notfound ;
dbms_output.put_line(v_row.stuname || ',' || v_row.sex || ',' || v_row.age);
end loop;
close mycursor ;
end ;
游标的小结:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有