• 游标


    --显式游标

    declare
      cursor c_c is select * from scott.emp;--定义游标
      r_r scott.emp%rowtype;                --定义变量[rowtype为行类型]
    begin
      open c_c;                        --打开游标
      loop                             --简单循环
        fetch c_c into r_r;            --将数据放入变量[执行FETCH语句时,每次返回一个数据行,然后自动将游标移动指向下一个数据行。]
        dbms_output.put_line(r_r.job); --输出行变量中字段数据
        exit when c_c%notfound;        --如果没有下一条数据[退出条件 found有下条数据 notfound没有下条数据]
      end loop;
      close c_c;                       --关闭游标
    end;

    --带有参数的游标

    declare
      cursor c_c(mid number default 7521) is select * from scott.emp where empno=mid;--定义游标
      r_r scott.emp%rowtype;                --定义变量[rowtype为行类型]
    begin
      open c_c(7788);                       --打开游标
      loop                                  --简单循环
        fetch c_c into r_r; 
        dbms_output.put_line(r_r.job||' '||r_r.empno); --输出行变量中字段数据
        exit when c_c%notfound;             --如果没有下一条数据[退出条件 found有下条数据 notfound没有下条数据]
      end loop;
      close c_c;                            --关闭游标
    end;

     --隐式游标

    declare
      cursor c_c is
        select * from scott.emp;
      r_r scott.emp %rowtype;
    begin
      for v_r in c_c loop                --自动打开游标,自动检测NOTFOUND
        dbms_output.put_line(v_r.job);
      end loop;
      --隐含关闭游标
    end;

    --游标变量

    declare
      type m_c_type is ref cursor; --定义游标类型 m_c_type
      c_c m_c_type;                --声明游标变量
      r_r scott.emp%rowtype;
    begin
      open c_c for                 --打开游标变量
        select * from scott.emp;
      loop
        fetch c_c
          into r_r;
        dbms_output.put_line(r_r.job);
        exit when c_c%notfound;
      end loop;
    end;

    --动态定义游标 

    declare
      msql    varchar2(111) := 'select * from emp';
      tbl_emp emp%rowtype;
      type cur_type is ref CURSOR;
      cur cur_type;
    begin
      OPEN cur for msql; --打开动态定义的游标
      LOOP
        FETCH cur
          into tbl_emp; --循环赋值
        EXIT WHEN cur%NOTFOUND; --跳出条件
        dbms_output.put_line(tbl_emp.empno || '  ' || tbl_emp.ename); --打印
      END LOOP; 
    end;
  • 相关阅读:
    git clone --early EOF
    weex 小结 -- <list>
    weex 小结 --官方扩展组件
    weex 小结--内建模块
    viewPager 的可滑动 Title
    viewPager + fragment
    onInterceptTouchEvent / onTouchEvent响应事件的详析
    startActivity跳转失败而且没有异常信息
    ios2048
    插入排序
  • 原文地址:https://www.cnblogs.com/zhaopei/p/4171456.html
Copyright © 2020-2023  润新知