加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oracle 游标

发布时间:2020-12-12 13:19:44 所属栏目:百科 来源:网络整理
导读: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 %r

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 标识。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读