• oracle 游标


    1.用for遍历游标,不必打开、关闭游标。for自动控制。

    创建使用游标的存储过程,

    create or replace procedure pro_dff_cursor(sex1 varchar)

    as

    --定义游标

    cursor test_cursor is

    select id,name,age,sex from table where sex = sex1;

    cur test_cursor %rowtype; --cur为结果集的一条数据,跟java中list的对象相似,这里定义了游标的类型。List<Object>

    begin

     for cur in test_cursor loop

     exit when test_cursor %notfound;

     dbms_output.print_line('id:'||cur.id||',name:'||cur.name||',age:'||cur.age||',sex:'||cur.sex);

     end loop;

    end;

    ===========================================================================================

      上边,首先看Cursor test_cursor is 这一行,它的意思是定义一个游标,test_cursor 为你要定义的名字,而is 后边是一个sql,也就是说当前这个sql的查询结果,赋值给游标test_cursor 。
      然后,往下,接着cur test_cursor%rowtype ,这个是定义了一个类型,而这个类型,即是游标test_cursor 的返回结果类型,类型的名字为cur 。有点类似于java语言 中List 集合中的一个泛型 。
      另外,关于for 是一个循环的写法,for cur in test_cursor ,即,从游标test_cursor 中取出一个结果cur 。
      还有,注意,loop 和end loop 这是一个循环的开始标志和结束标志,但它俩兄弟是一个很执着的循环,如果没有定义退出条件,永远不会退出的,所以在上边的循环里边,有了退出条件exit when test_cursor%notfound; ,即当游标test_cursor 中没有数据了,就退出循环。
      当然loop 循环的退出,发生下边的情况,才能退出:

      有exit,并满足条件后退出。
      loop中抛出了异常。
      存在goto 标识。

    2.上述红字部分替换循环方式

    a.Fetch 循环

    open test_cursor; --需要明确打开游标

    loop

     fetch test_cursor into cur;

     exit when test_cursor%notfound;

     dbms_output.print_line('id:'||cur.id||',name:'||cur.name||',age:'||cur.age||',sex:'||cur.sex); --循环体

    end loop;

    close test_cursor; --关闭游标

    b.While循环

    open test_cursor;

    while test_cursor%notfound loop 

    fetch test_cursor into cur ;

    dbms_output.print_line('id:'||cur.id||',name:'||cur.name||',age:'||cur.age||',sex:'||cur.sex); --循环体

    close test_cursor;

  • 相关阅读:
    asp.net连接数据库,在web.config中配置数据库连接
    在处理向该请求提供服务所需的配置文件时出错。请检查下面的特定错误详细信息并适当地修改配置文件如何解决
    centos7查看CPU的利用率
    阿里云云盘在线扩容
    微信和支付宝付款码条码规则(官方)
    微信和支付宝付款码条码规则
    IPFS与般若文海
    Moira果老星宗七政四余排盘软件下载
    Odoo 电子公章/印章模块
    playtoearn
  • 原文地址:https://www.cnblogs.com/oath-keeper/p/10374948.html
Copyright © 2020-2023  润新知