• 游标


    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;

  • 相关阅读:
    Linux 中 eclipse 的tomcat端口号被占用
    JDBC的常用API
    eclipse Alt+/ 无法提示代码
    javaweb项目开发错误代码
    PSP总结报告
    20181204-1 每周例行报告
    对团队成员公开感谢博客
    20181127-2 每周例行报告
    20181120-1 每周例行报告
    20181113-2 每周例行报告
  • 原文地址:https://www.cnblogs.com/tian114527375/p/4915840.html
Copyright © 2020-2023  润新知