PL/SQL语法 - 游标

Oracle中的游标分为显式游标和隐式游标。
隐式游标是系统自动为你创建的。
显式游标是用户通过创建CURSOR的方式来创建。

在Oracle中有三种类型的游标:

1)不带参数的游标
      eg:
       cursor customer_cur is
            select * from customer s;      

2)带参数的游标
      eg:
        cursor customer_cur(customer_id in number) is
            select * from customers
            where customerid = customer_id;

3)有返回结果的游标
      
eg:
        cursor customer_cur return customers%rowtype is
            select * from customers;

游标的五大属性:
1)%FOUND              - 记录找到
2)  %NOTFOUND        - 记录没找到
3)  %ROWCOUNT       - 游标当前记录数
                                    (每取回一条记录,ROWCOUNT将增加1)
4)  %ISOPEN              - 游标是否被打开
5)  %ROWTYPE           - 代表游标取回的某一行记录

游标使用四大步骤:
1) 声明游标
      cursor is select语句

2) 打开游标  
      open 游标名

3) 从游标中取回数据
      fetch 游标名 into 行记录变量

4) 关闭游标
      close 游标名

下面我们就来举个例子:
declare  
  
--  声明游标
   cursor  customer_cur(city_in  char is
    
select   *   from  customers
    
where  city  =  city_in;
  customer_rec customer_cur
% ROWTYPE;
begin
  
--  打开游标
   if   not  customer_cur % ISOPEN  then
    
open  customer_cur( ' Madrid ' );
  
end   if ;
  
  
--  从游标中取回数据
   fetch  customer_cur  into  customer_rec;
  
  
/*
    使用游标
  
*/
  
while  customer_cur % FOUND
  loop
    dbms_output.put_line(TO_CHAR(customer_cur
% ROWCOUNT ||   ' : '   ||  customer_rec.customerid  
      
||   ' '   ||  customer_rec.city);
    
fetch  customer_cur  into  customer_rec;
  
end  loop;
  
  
--  关闭游标
   close  customer_cur;
  
end ;



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值