现在的位置: 首页 > 综合 > 正文

关于游标cursor、游标引用 ref cursor

2018年01月16日 ⁄ 综合 ⁄ 共 1492字 ⁄ 字号 评论关闭
 

游标 cursor:查询返回的结果集  

1、静态游标,与定义好的查询关联;

   a、显式游标
      type t_type is record(                      --    定义保存结果的类型
                       pk     t.pk%type
                      ,name1  t.name1%type
                      ,name2  t.name2%type
                      );
      v_t t_type;                                 --    定义结果变量,行记录。当然也可以不这样定义,而一个个指定字段变量。
  
      cursor c for select * from t where pk=1;    --    静态游标:1、定义游标
      open c;                                     --    静态游标:2、打开游标
      fetch c into v_t;                           --    
      while c%FOUND loop                          --    静态游标:3、遍历游标   
          fetch c into v_t;
          [ some DML ... with v_t.pk,v_t.name1,v_t.name2 ... ]
      end loop;
      close c;                                    --    静态游标:4、关闭游标  
      
      
   b、隐式游标
      for x in (select * from t where pk=t) loop  --    隐式游标,相对于显示游标,结构简单,推荐使用
          [ some DML ...with x.pk,x.name1,x.name2 ... ]
      end loop;

2、动态游标:ref cursor 查询语句可以动态指定

   a、弱类型游标
      type refcur_t is ref cursor;                                  --   弱游标类型定义不指定具体的返回类型

      refcur  refcur_t;                                             --   弱游标变量
      ----  使用方式同显示静态游标: 
      open refcur for select ... ;                                  --   关联查询(可以多次关联不同的查询)
      fetch refcur into ... 
      while refcur%FOUND loop
          ...
      end loop;
      close refcur;
      ----

   b、强类型游标
      type emp_refcur_t is ref cursor return employees%rowtype;     --   指定返回类型 

      使用方式同上

 

  

抱歉!评论已关闭.