zoukankan      html  css  js  c++  java
  • oracle学习笔记(二十二) REF 动态游标

    动态游标

    定义语法

    --声明
    $cursor_name$ sys_refcursor
    
    --打开动态游标
    open $cursor_name$ is 查询语句;
    
    --关闭游标
    close $cursor_name$;
    
    --声明动态游标类型
    type $type_name$ is ref cursor;
    
    --声明一个动态游标变量
    $v_cursor_name$ type_my_ref;
    
    

    使用

    动态游标可以获得不同的结果集,可以设置条件,返回不同的结果集,一般和过程一起使用

    --创建过程list
    create or replace procedure list(result_set in out sys_refcursor, which in number)
    is   
    begin
      --打开动态游标时在为它指定查询语句
      --1就是返回员工表,其他就返回部门表
      if which=1 then
        open result_set for select * from employee;
      else
        open result_set for select * from department;
      end if;
    end;
    /
    
    --调用list过程,根据输入的数字输出某个表的全部信息
    declare
       --声明一个动态游标类型
       type type_my_ref is ref cursor;
       --声明一个动态游标变量
       my_ref type_my_ref;
       emp employee%rowtype;
       dept department%rowtype;
       
       which number default &请输入;
    begin
      --得到一个查询结果集
      list(my_ref, which);
     loop
       if which=1 then /* handle employee */
          fetch my_ref into emp;
          exit when my_ref%notfound;
          dbms_output.put_line(emp.empno||','||emp.ename||','||emp.job||','||emp.sal);
          
       else            /* handle department */
          fetch my_ref into dept;
          exit when my_ref%notfound;
          dbms_output.put_line(dept.deptno||','||dept.dname||','||dept.loc);
       end if;
     end loop;
     --close cursor
     close my_ref;
    end;
    /
    
  • 相关阅读:
    [BZOJ1004] [HNOI2008]Cards解题报告(Burnside引理)
    [POJ1286&POJ2154&POJ2409]Polya定理
    monkey工具介绍及用法
    adb 命令使用与解释
    android-sdk的安装及配置
    spring-boot 加入拦截器Interceptor
    对spring boot 之AutoConfiguration 的理解
    java 集合操作小结
    java -d . **.java 与 java **.java 的区别
    关于Eclipse SVN 分支 与主干 小结
  • 原文地址:https://www.cnblogs.com/stars-one/p/10970095.html
Copyright © 2011-2022 走看看