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;
    复制代码
  • 相关阅读:
    微信小程序发送模板消息
    Swoole-WebSocket服务端主动推送消息
    git 批量删除分支
    RdKafka使用
    Kakfa安装,PHP安装RdKafka扩展
    Zookeeper安装、启动、启动失败原因
    Hyperf-事件机制+异常处理
    Hyperf-JsonRpc使用
    hyperf-环境搭建
    CGI、FastCGI、PHPFPM
  • 原文地址:https://www.cnblogs.com/yujihaia/p/7367672.html
Copyright © 2011-2022 走看看