zoukankan      html  css  js  c++  java
  • 游标

    begin
    insert into userroles values (55,'aa');
    end;

    begin
    update userroles set name='bb' where id=55;
    end;

    begin
    delete from userroles where id=55;
    end;

    declare
    r userroles%rowtype;
    begin
    select * into r from userroles where id=2;
    dbms_output.put_line(r.id||','||r.name);
    end;



    declare
    a int;
    begin
    delete from books where lower(title) like 'java%';
    a:=sql%rowcount;--sql隐式游标
    rollback;
    dbms_output.put_line('删除了'||a||'条记录');
    end;


    --显式游标

    declare
    cursor cur is select * from books where rownum<=50;--1.定义游标
    bk books%rowtype;
    begin
    open cur;--2.打开游标
    loop
    exit when cur%notfound;
    fetch cur into bk;--3.从游标中提取数据
    dbms_output.put_line(bk.id||' '||bk.title);
    --dbms_output.put_line(cur%rowcount);
    end loop;
    close cur;--4.关闭游标
    end;

    --显式游标
    --带参
    declare
    cursor cur(cnt int) is select * from books where rownum<=cnt;--1.定义游标
    bk books%rowtype;
    begin
    open cur(20);--2.打开游标传入参数
    loop
    exit when cur%notfound;
    fetch cur into bk;--3.从游标中提取数据
    dbms_output.put_line(bk.id||' '||bk.title);
    --dbms_output.put_line(cur%rowcount);
    end loop;
    close cur;--4.关闭游标
    end;




    declare
    cursor cur(cnt int,bn varchar) is
    select * from books where
    lower(title) like bn and rownum<=cnt;--1.定义游标
    bk books%rowtype;
    begin
    open cur(20,'java%');--2.打开游标
    loop
    exit when cur%notfound;
    fetch cur into bk;--3.从游标中提取数据
    dbms_output.put_line(bk.id||' '||bk.title);
    --dbms_output.put_line(cur%rowcount);
    end loop;
    close cur;--4.关闭游标
    end;



    --基于游标的更新
    declare
    cursor cur(cnt int,bn varchar) is
    select * from books where
    lower(title) like bn and rownum<=cnt
    for update;--1.定义游标
    bk books%rowtype;
    begin
    open cur(20,'java%');--2.打开游标
    loop
    exit when cur%notfound;
    fetch cur into bk;--3.从游标中提取数据
    dbms_output.put_line(bk.id||' '||bk.title||' '||bk.unitprice);
    if bk.unitprice<50 then
    update books set unitprice=unitprice+5 where current of cur;
    end if;
    end loop;
    close cur;--4.关闭游标
    end;

    --for游标
    declare
    cursor cur is select id,title from books where rownum<=50;
    begin
    for bk in cur
    loop
    dbms_output.put_line(bk.id||' '||bk.title);
    end loop;
    end;



    --ref游标(动态游标)
    declare
    type MyCur is ref cursor;--定义一种类型
    cur MyCur;
    bk books%rowtype;
    rl userroles%rowtype;
    begin
    open cur for 'select * from books where rownum<=50';
    loop
    fetch cur into bk;
    exit when cur%notfound;
    dbms_output.put_line(bk.id||' '||bk.title);
    end loop;
    close cur;
    open cur for 'select * from userroles';
    loop
    fetch cur into rl;
    exit when cur%notfound;
    dbms_output.put_line(rl.id||' '||rl.name);
    end loop;
    close cur;
    end;

  • 相关阅读:
    未能加载文件或程序集"xxx"或它的一个依赖项。试图加载格式不正确的程序。
    js实现拖动div,兼容IE、FireFox,暂不兼容Chrome
    WPF 像CSS一样使用 Font Awesome 图标字体
    面向接口、面向对象、面向方面的区别
    ionic 编译 gradle下载。
    ionic 不是外部命令 不是内部命令
    Ionic 编译找不到index.html
    分区表
    安装.net framwork3.5
    MSDTC不可用解决办法
  • 原文地址:https://www.cnblogs.com/tian114527375/p/4915840.html
Copyright © 2011-2022 走看看