Oracle 数据库的游标

一. Oracle 游标

  1. 什么是游标

  2. 什么是游标
    就是一个集合,数组,游标中存储很多的数据,处理多行数据的问题
    游标就是指定一条DQL语句,内容就是DQL语句查询出的内容

  3. 游标的声明: cursor游标的关键字
    cursor 游标名 is DQL;

  4. 游标的遍历
    a,打开游标
    b,提取游标中的内容, 提取一行记录:fetch 游标名 into 变量;
    c,循环, 退出,exit when 游标名%notfound; 当游标中没有提取到记录时退出
    d,关闭游标

  5. 栗子:使用游标在控制台打印20号部门所有的员工信息
    declare
    – 声明游标,把要在控制台显示的数据赋值给游标
    cursor cur is select * from emp where deptno = 20;
    – 声明一个变量,用来存储游标的一行
    e_row emp%rowtype;
    begin
    – 遍历游标
    –a,打开游标
    open cur;
    loop
    –b,提取游标中的内容, 提取一行记录:fetch 游标名 into 变量;
    fetch cur into e_row;
    exit when cur%notfound;
    dbms_output.put_line(e_row.empno || e_row.ename);
    –c,循环, 退出,exit when 游标名%notfound; 当游标中没有提取到记录时退出
    end loop;
    –d,关闭游标
    close cur;
    end;

  6. 栗子:使用游标给20号部门所有的员工涨工资
    declare
    cursor cur is select empno,ename from emp where deptno = 20;
    begin
    – 注意:for循环特点,可以自动打开和关闭游标
    – i代表的是游标一行的内容
    for i in cur loop
    update emp set sal = sal + 1 where empno = i.empno;
    end loop;
    end;

    update emp set sal = sal + 1 where deptno = 20;

Oracle的plsql基本语句
Oracle的视图,索引

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值