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;

  • 相关阅读:
    Spring
    linux下jdk多版本管理
    linux集群管理
    python多任务处理
    Web框架的引入
    Git命令大全
    Git远程仓库--GitHub
    基于python实现简单web服务器
    python文件读写方式
    几句话搞懂URI、URL、URN之间的关系
  • 原文地址:https://www.cnblogs.com/trister/p/4631307.html
Copyright © 2011-2022 走看看