oracle游标cursor:oracle18(跟着宝哥学java:oracle系列:全网最全):oracle游标cursor、显式游标、隐式游标、遍历游标

/*
游标:cursor
      类似于集合的迭代器 用于遍历集合的数据库对象
分类:隐式游标+显式游标
隐式游标:进行dml语句时  数据库自动创建的游标  :::默认名字SQL
显式游标:进行dql语句时  程序员自己创建的游标 
游标属性:%FOUND:::boolean::是否有元素受到影响
          %NOTFOUND:::boolean::是否没有元素受到影响
          %ROWCOUNT:::int::::::记录影响的行数
          %ISOPEN:::::boolean::是否开启:::隐式游标默认是关闭的
定义显式游标:
     cursor 游标名 as select语句;
     open 游标名;
          loop
              fetch 游标名 into 变量;
          end loop;
     close 游标名;
     
*/

-- 隐式游标
declare
begin
     update stuinfo set sage=sage+1 where sex='男'; 
     if SQL%FOUND then
          dbms_output.put_line('隐式游标的属性:SQL%FOUND::是否影响到行=true');
     end if;
     if not SQL%NOTFOUND then
          dbms_output.put_line('隐式游标的属性:SQL%NOTFOUND::是否没有影响到行=false');
     end if;
     if not SQL%ISOPEN then
          dbms_output.put_line('隐式游标的属性:SQL%ISOPEN::是否开启=false');
     end if;
     dbms_output.put_line('隐式游标的属性:SQL%ROWCOUNT='||SQL%ROWCOUNT);
     exception
          when others then
          dbms_output.put_line('出错了!!!');  
end;

-- 显式游标::一列多行
declare
     cursor c_1 is select sname from stuinfo;-- 定义游标
     snameX varchar(100);-- 定义一个变量记录取出的元素
begin
     open c_1;-- 开启游标
     loop
         fetch c_1 into snameX;-- 从游标中取出一个元素
         if c_1%NOTFOUND then  -- 判断是否还有元素可以遍历
             exit;
         end if;
         dbms_output.put_line('snameX::'||snameX);  
     end loop;
     close c_1;-- 开关闭游标
     exception
          when others then
          dbms_output.put_line('出错了!!!');  
end;

-- 显式游标::plsql特殊类型::列名%type::变量的类型和列类型一致
-- 显式游标::多行多列
declare
     cursor c_1 is select sname,sno,sex,sage from stuinfo;-- 定义游标
     -- snameX varchar(100);-- 定义一个变量记录取出的元素
     -- snoX int;-- 定义一个变量记录取出的元素
     -- sageX int;-- 定义一个变量记录取出的元素
     -- sexX varchar(100);-- 定义一个变量记录取出的元素
     snameX stuinfo.sname%type;
     snoX stuinfo.sno%type;
     sexX stuinfo.sex%type;
     sageX stuinfo.sage%type;
begin
     open c_1;-- 开启游标
     loop
         fetch c_1 into snameX,snoX,sexX,sageX;-- 从游标中取出一个元素
         if c_1%NOTFOUND then  -- 判断是否还有元素可以遍历
             exit;
         end if;
         dbms_output.put_line(snameX||'::'||sageX||'::'||sexX||'::'||snoX);  
     end loop;
     close c_1;-- 开关闭游标
     exception
          when others then
          dbms_output.put_line('出错了!!!');  
end;

-- 显式游标::plsql特殊类型::表名%rowtype::变量的类型和行类型一致
-- 显式游标::多行多列
declare
     cursor c_1 is select * from stuinfo;-- 定义游标
     stu stuinfo%rowtype; -- 变量类型和表的行类型一致
begin
     open c_1;-- 开启游标
     loop
         fetch c_1 into stu;-- 从游标中取出一个元素
         if c_1%NOTFOUND then  -- 判断是否还有元素可以遍历
             exit;
         end if;
         -- 结果集中有哪些列  stu中就要哪些属性
         dbms_output.put_line(stu.sno||'::'||stu.sname||'::'||stu.sex||'::'||stu.sbirth);  
     end loop;
     close c_1;-- 开关闭游标
     exception
          when others then
          dbms_output.put_line('出错了!!!');  
end;

-- 遍历游标的简洁方式
declare
     cursor c_1 is select * from stuinfo;-- 定义游标
     -- cursor c_1 is select sage,sno,sname,sbirth from stuinfo;-- 定义游标
begin
     for stu in c_1 loop   -- 从游标中取出一行数据给stu:::结果集有哪些列 stu就有哪些属性
         dbms_output.put_line(stu.sno||'::'||stu.sname||'::'||stu.sbirth);
     end loop;
     exception
          when others then
          dbms_output.put_line('出错了!!!');  
end;

-- 通过游标进行dml
declare
     cursor c_1 is select * from student for update;-- 定义游标 声明可以用于dml
begin
     for stu in c_1 loop   -- 从游标中取出一行数据给stu:::结果集有哪些列 stu就有哪些属性
         dbms_output.put_line(stu.sno||'::'||stu.sname||'::'||stu.sbirth);
         if stu.sex='男' then
             delete from student where current of c_1;  -- 删除当前行
         end if;
         if stu.sex='sex' then
             update student set sage=sage+10  where current of c_1;   -- 修改当前行
         end if;
     end loop;
     commit;
     exception
          when others then
          dbms_output.put_line('出错了!!!');  
          rollback;
end;

select * from student;

---
drop table Student;
create table Student(
   Sno  varchar(50)  primary key,
   Sname  varchar(50)  not null,
   Sage  int   check(sage >=18 and Sage <=40),
   Sbirth  date  not null,
   address  varchar(50)  default '地址不详',
   sex char(3)
);
create sequence seq_stu;
insert into student values(
   seq_stu.nextval,
   dbms_random.string('i',4),
   trunc(dbms_random.value(18,41),0),
   to_date(trunc(dbms_random.value(1990,2010),0)||'-'||trunc(dbms_random.value(1,13),0)||'-'||trunc(dbms_random.value(1,32),0),'yyyy-mm-dd'),
   dbms_random.string('p',4),
   '女'
);

-- 做练习: 1 添加分数列score:分数取值[0,100]
--         2 进行自我介绍:我叫xxx 我是美女/帅哥 今年多少岁 再有多少天过生日 考了xx分
--         3 给比平均分少的学生 分数+10  删除最高分和最低分的学生记录



在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值