zoukankan      html  css  js  c++  java
  • 游标

    复制代码
    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;
    复制代码
  • 相关阅读:
    前插法创建带头节点的单链表
    利用Oracle数据库发送邮件
    关于如何给C#中的ListBox控件添加双击事件
    类似QQ表情的功能,包括动态绑定图片
    ASP.NET 缓存技术总结
    VS2008 快捷键大全
    Oracle 中union的用法
    C#找到Excel中的所有Sheetname的方法
    推荐一个学习XPath的网站
    Oracle游标使用大全
  • 原文地址:https://www.cnblogs.com/yujihaia/p/7367672.html
Copyright © 2011-2022 走看看