zoukankan      html  css  js  c++  java
  • PL/SQL学习笔记游标

    一:普通游标

    declare
    --定义record类型变量
    type v_record is record
    (title labor.xland.title%type,state labor.xland.title%type);
    --定义字符串类型变量
    v_title labor.xland.title%type;
    --定义一个游标
    cursor c1 is
    select title from labor.xland;
    begin 
    open c1;--打开游标
    loop
    fetch c1 into v_title;--把游标中的数据存入变量,可以有多个变量
    if c1%found then--如果找到数据,找不到数据为notfound
      dbms_output.put_line(v_title);
    else
      dbms_output.put_line('已经处理完结果集');
      exit;--退出循环
    end if;
    end loop;
    close c1;--关闭游标
    end;


    输出结果

    new xland
    123
    xland
    123
    xland
    已经处理完结果集



    二:有参游标

    declare
    --定义record类型变量
    type v_record is record
    (title labor.xland.title%type,state labor.xland.title%type);
    --定义字符串类型变量
    v_title labor.xland.title%type;
    --定义有参数的游标
    cursor c1(v_no number) is
    select title from labor.xland where state > v_no;
    begin 
    open c1(0);--打开游标,传递参数
    loop
    fetch c1 into v_title;--把游标中的数据存入变量,可以有多个变量
    if c1%found then--如果找到数据,找不到数据为notfound
      dbms_output.put_line(v_title);
    else
      dbms_output.put_line('已经处理完结果集');
      exit;--退出循环
    end if;
    end loop;
    close c1;--关闭游标
    end;


    输出结果

    123
    xland
    123
    xland
    已经处理完结果集



    三:有返回值的游标

    declare
    --定义一个类型
    type t_record is record
    (title labor.xland.title%type,state labor.xland.state%type);
    --定义v_record类型的变量
    v_record t_record;
    --定义字符串类型变量
    v_title labor.xland.title%type;
    --定义有返回值的游标
    cursor c1(v_no number) return t_record is
    select title,state from labor.xland where state > v_no;
    begin 
    open c1(v_no => 0);--打开游标,传递参数(参数的另一种传递方式)
    loop
    fetch c1 into v_record;--把游标中的数据存入变量,可以有多个变量
    exit when c1%notfound;
      dbms_output.put_line(v_record.title||'  '||to_char(v_record.state));
    end loop;
    close c1;--关闭游标
    end;


    输出结果

    123  3
    xland  3
    123  3
    xland  3



    四:运行过程中获取游标中的内容

    declare
    --定义游标
    cursor c1(v_no number) is
    select * from labor.xland where state > v_no;
    --定义变量为游标的行类型
    v_record c1%rowtype;
    begin 
    open c1(v_no => 0);--打开游标,传递参数(参数的另一种传递方式)
    loop
    fetch c1 into v_record;--把游标中的数据存入变量,可以有多个变量
    exit when c1%notfound;
      dbms_output.put_line(v_record.title||'  '||to_char(v_record.state));
    end loop;
    close c1;--关闭游标
    end;


    输出结果

    123  3
    xland  3
    123  3
    xland  3


    五:隐式游标

    declare
    v_rows number;
    begin 
    update labor.xland set xland.title = 'new xland' where xland.state>0;
    v_rows := sql%rowcount;
    dbms_output.put_line(to_char(v_rows));
    end;


    更多隐式游标属性请看此系列文章的附录:
    常用函数
    输出结果为受影响的行数
    不能对隐式游标执行显示游标类似的操作,如:
    open  fetch close等

    六:用for循环简化游标的操作

    declare
    --定义游标
    cursor c1(v_no number default 0) is--默认值为0
    select * from labor.xland where state > v_no;
    --定义变量为游标的行类型
    v_record c1%rowtype;
    begin 
    for v_record in c1() loop
      dbms_output.put_line(v_record.title||'  '||to_char(v_record.state));
    end loop;
    end;


    输出结果

    new xland  3
    new xland  3
    new xland  3
    new xland  3


    注意此游标的参数有个默认值
    for循环使得我们的程序不必再写 open  fetch  close等操作了
    它已经给我们实现了

    七:在游标中更新或删除数据

    declare
    --定义游标
    cursor c1(v_no number) is
    select * from labor.xland where state > v_no for update;--注意最后的for update
    --定义变量为游标的行类型
    v_record c1%rowtype;
    begin 
    open c1(v_no => 0);--打开游标,传递参数(参数的另一种传递方式)
    loop
    fetch c1 into v_record;--把游标中的数据存入变量,可以有多个变量
    exit when c1%notfound;
    if v_record.state=3 then
       update xland set state = 6 where current of c1;--注意where子句
      dbms_output.put_line('更新了一条数据');
    end if;
    end loop;
    commit;--提交更新
    close c1;--关闭游标
    end;



    当使用for update打开游标后就可以用current of cursor_name来更新数据了

  • 相关阅读:
    Js实现继承的几种方式
    ES6-promise实现异步请求
    CSS重排和重绘
    jq在元素的后面插入内容
    yii2 Query Builder 查询打印sql语句
    js replace(a,b)替换指定字符
    linux 打印当前工作目录
    linux 查看磁盘空间
    linux查看大文件
    js-Cannot read property 'innerHTML' of null
  • 原文地址:https://www.cnblogs.com/liulun/p/1542145.html
Copyright © 2011-2022 走看看