zoukankan      html  css  js  c++  java
  • oracle 之 游标

     

    本期主题  灰蓝

        游标用来处理从数据库中检索的多行记录(使用SELECT语句)存放的是select 的结果

      利用游标,程序可以逐个地处理和遍历一次检索返回的整个记录集

    --隐式游标 

    begin

      update v_emp set ename='陈明羽' where empno = 7369;

      if SQL%found then

        dbms_output.put_line('隐式游标被找到');

      end if;

    end;

    -- 显示游标被用于处理返回多行数据的SELECT 语句 

    -- 方式1  

    declare

      v_row v_emp%rowtype;

      --创建游标

      cursor c_s is 

          select * from v_emp;

      begin

      --打开游标

      open c_s;

      loop

        fetch c_s

          into v_row;

        exit when c_s%notfound;

          dbms_output.put_line(v_row.ename);

          dbms_output.put_line(v_row.sal);

        end loop;

        --关闭游标

      close c_s;

    end;

    -- 方式2

    declare

      v_row v_emp%rowtype;

      cursor cur is select * from v_emp;

      begin

      -- for循环 不用打开和关闭游标

      for v_row in cur loop

        dbms_output.put_line(v_row.ename);

        dbms_output.put_line(v_row.sal);

      end loop;

    end;

    -- 方式3

    declare

      v_row v_emp %rowtype;

      cursor cur is

        select * from v_emp;

      begin

      open cur;

      --赋值到一个变量

      fetch cur

        into v_row;

      while cur%found loop

        dbms_output.put_line(v_row.ename);

        dbms_output.put_line(v_row.sal);

        fetch cur

          into v_row;

      end loop;

      close cur;

    end;

    -- ref游标 动态sql查询 有return a_rows%rowtype的是强类型的

    declare

      type cur is ref cursor;

      a_cur cur;

      a_row v_emp%rowtype;

     begin

      open a_cur for

        select * from v_emp;

        fetch a_cur into a_row;

       while a_cur%found loop

          dbms_output.put_line(a_row.ename);

          dbms_output.put_line(a_row.sal);

          fetch a_cur

            into a_row;

        end loop;

        close a_cur;

     end;

  • 相关阅读:
    Azure Active Directory document ---reading notes
    tcp/ip 三次握手和4次挥手
    why microsoft named their cloud service Azure?
    Azure VMs
    Leetcode (C++) 9/20/2017开始
    How do you make an object in C? Used in RTOS.
    HF-DP1: strategy pattern
    确定一下学习的主要参考书
    记一下Thoratec的面试
    《Algorithm in C》by Sedgewick 读书笔记
  • 原文地址:https://www.cnblogs.com/cmyxn/p/5877233.html
Copyright © 2011-2022 走看看