zoukankan      html  css  js  c++  java
  • 游标

      1.隐式游标:用户不用定义,就可以直接使用的游标,名称叫SQL

          2.显示的游标:用户自定义的游标

    1)静态的游标:

      declare 

        --1.声明静态的游标,为查询stud表

        cursor mycur is select id,name,age from from stud;

        --定义一个变量,用于每次从游标中获取三个值

        v_stud  stud%rowType;

      begin

        --2.打开游标

        open  mycur;

        --遍历游标获取数据

        loop

          fetch  mycur  into  v_stud;

          --如果里面没有获取到数据则退出loop

          exit when mycur%notfound;

          --如果有数据就显示

          dbms_output.put_line('id is' || v_stud.id || 'name is:' || v_stud.name); 

        end  loop;

        --关闭游标

        close mycur;

        --保证游标一定要关闭

        exception

          when others then

            if mycur%isopen then 

              close mycur;

            end if;

      end;

    2)接受参数的游标

    declare

      --1.声明静态的游标为查询stud表

      cursor mycur(p_id int) is select id,name,age from stud where id > p_id;

      v_stud  stud%rowType;

    begin 

      --2.打开游标

      open mycur(&id);

      --3.遍历游标获取数据

      loop

        fetch mycur into v_stud;

        --如果没有获取到数据,就退出loop

        exit when mycur%notfound;

        --如果有就显示数据,

        dbms_output.put_line('id is:'||v_stud.id||'name is '|| v_stud.name);

      end loop;

      --4.关闭游标

      close mycur;

      exception

        when others then

          if mycur%isopen then

            close mycur;

          end if;

    end;

    3)动态的游标:

    declare 

      --先定义一个动态的游标的数据类型

      --相当于java -- class Person()

      TYPE cur IS REF CURSOR;

      --定义一个自己的类型的变量

      mycur cur;     --Person  mycur

      --定义一个变量用于获取值

      v_stud  stud%rowType;

    begin 

      --打开游标,设置打开的字符串

      --查询语句

      open mycur for 'select id,name,age from stud';

      loop

        fetch mycur into v_stud;

        exit when mycur%notfound;

        dbms_output.put_line('id is:'||v_stud.id||'  '||v_stud.name);

      end loop;

      close mycur;

    end;

  • 相关阅读:
    git学习笔记
    ExtJs自学教程(1):一切从API開始
    Floodlight 处理交换机增加/移除过程
    飘逸的python
    Mapreduce运行过程分析(基于Hadoop2.4)——(三)
    oracle中LAG()和LEAD()等分析统计函数的使用方法(统计月增长率)
    Linux学习笔记总结
    看完锤子手机公布会直播 有感
    iOS iOS8中 问题"registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later" 解决方式
    读书笔记-HBase in Action-第二部分Advanced concepts-(3)非Javaclient
  • 原文地址:https://www.cnblogs.com/trister/p/4631307.html
Copyright © 2011-2022 走看看