oracle 笔记4 游标

--游标

游标的属性
%isopen         boolean    如果游标打开,则为TRUE
%notfound       boolean    如果最近的提取没有返回一条记录,则为TRUE
%found          boolean    一直为TRUE,直到最近提取没有取回行记录
%rowcount       boolean    到目前为止,提取的总行数
何云祥制作
--显示游标的使用方法
declare   --声明
    --定义变量
    v_id test.id%type;
    v_name test.name%type;
    v_age test.age%type;
    v_rowcount number;  --存储提取总行数

    cursor test_cur is
        select id, name, age from test;    --注意,游标声明的select语句中,不要包含INTO子句。
begin
    open test_cur;  --打开游标
    fetch test_cur into v_id, v_name, v_age; --取值
    while test_cur%found loop    --或者使用while not test_cur%notfound loop 
        dbms_output.put_line(v_id||'  '||v_name||'  '||v_age); --输出
        fetch test_cur into v_id, v_name, v_age;
    end loop;
    v_rowcount:=test_cur%rowcount;    --使用%rowcount属性
    dbms_output.put_line(v_rowcount);
    close test_cur;   --关闭游标
end;

--游标更新使用
declare
v_id employee.e_id%type;
v_name employee.e_name%type;
v_slary number(10);
cursor first_cur is select * from employee for update;
rec1 employee%rowtype;
begin
  if(first_cur%isopen) then
     null;
  else
    open first_cur;
  end if;
  fetch    first_cur  into rec1;
  while  first_cur%found loop
     dbms_output.put_line(rec1.e_id||' '||rec1.e_name||'  '||rec1.slary);

     UPDATE employee set e_name='F-'||rec1.e_id   WHERE CURRENT OF first_cur;
     fetch    first_cur into rec1;
  end loop;
  close first_cur;
end;
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值