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;

  • 相关阅读:
    转载: JS 中 new 操作符
    转载: js的值,对象,原型
    php setcookie(name, value, expires, path, domain, secure) 参数详解
    转载:js数组对象操作
    转载: js数组与 json 的区别
    转载: js的Prototype属性 解释及常用方法
    行人检测程序对接景区测试人数比对数据库切换时间不准确排查
    TSINGSEE青犀视频行人检测集成票务系统读取票务系统数据库为空的问题
    EasyWasmPlayer播放视频报错Uncaught (in promise)DOMException
    名胜景区部署TSINGSEE青犀视频监控具备哪些现实意义?
  • 原文地址:https://www.cnblogs.com/trister/p/4631307.html
Copyright © 2011-2022 走看看