`

存储过程常用技巧2

阅读更多

2. 存储过程内部块
2.1 内部块
我们知道了存储过程的结构,语句块由begin开始,以end结束。这些块是可以嵌套。在语句块中可以嵌套任何以下的块。

Java代码 复制代码
  1. Declare … begin … exception … end;   
  2. create or replace procedure innerBlock(p1 varchar2)   
  3. as    
  4.   o1 varchar2(10) := 'out1';   
  5. begin   
  6.   dbms_output.put_line(o1);   
  7.   declare    
  8.     inner1 varchar2(20);   
  9.   begin   
  10.     inner1 :='inner1';   
  11.     dbms_output.put_line(inner1);   
  12.   
  13.     declare    
  14.       inner2 varchar2(20);   
  15.     begin   
  16.       inner2 := 'inner2';   
  17.       dbms_output.put_line(inner2);   
  18.     end;   
  19.   exception    
  20.     when others then   
  21.       null;   
  22.   end;   
  23. end;  
Declare … begin … exception … end;
create or replace procedure innerBlock(p1 varchar2)
as 
  o1 varchar2(10) := 'out1';
begin
  dbms_output.put_line(o1);
  declare 
    inner1 varchar2(20);
  begin
    inner1 :='inner1';
    dbms_output.put_line(inner1);

    declare 
      inner2 varchar2(20);
    begin
      inner2 := 'inner2';
      dbms_output.put_line(inner2);
    end;
  exception 
    when others then
      null;
  end;
end;

需要注意变量的作用域。

3.存储过程的常用技巧
3.1 哪种集合?
我们在使用存储过程的时候经常需要处理记录集,也就是多条数据记录。分为单列多行和多列多行,这些类型都可以称为集合类型。我们在这里进行比较这些集合类型,以便于在编程时做出正确的选择。
索引表,也称为pl/sql表,不能存储于数据库中,元素的个数没有限制,下标可以为负值。

Java代码 复制代码
  1. type t_table is table of varchar2(20) index by binary_integer;   
  2.  v_student t_table;  
type t_table is table of varchar2(20) index by binary_integer;
 v_student t_table;

varchar2(20)表示存放元素的数据类型,binary_integer表示元素下标的数据类型。
嵌套表,索引表没有 index by子句就是嵌套表,它可以存放于数据中,元素个数无限,下标从1开始,并且需要初始化

Java代码 复制代码
  1. type t_nestTable is table of varchar2(20);   
  2. v_class t_nestTable ;  
type t_nestTable is table of varchar2(20);
v_class t_nestTable ;

仅是这样声明是不能使用的,必须对嵌套表进行初始化,对嵌套表进行初始化可以使用它的构造函数

Java代码 复制代码
  1. v_class :=t_nestTable('a','b','c');  
v_class :=t_nestTable('a','b','c');

变长数组,变长数组与高级语言的数组类型非常相似,下标以1开始,元素个数有限。

Java代码 复制代码
  1. type t_array is varray (20) of varchar2(20);  
type t_array is varray (20) of varchar2(20);


varray(20)就定义了变长数组的最大元素个数是20个
变长数组与嵌套表一样,也可以是数据表列的数据类型。
同时,变长数组的使用也需要事先初始化。

类型 可存储于数据库 元素个数 是否需初始化 初始下标值
索引表 否 无限 不需
嵌套表 可 无限 需 1
可变数组 可 有限(自定义) 需 1

由此可见,如果仅仅是在存储过程中当作集合变量使用,索引表是最好的选择。

3.2 选用何种游标?
显示游标分为:普通游标,参数化游标和游标变量三种。
下面以一个过程来进行说明

Java代码 复制代码
  1. create or replace procedure proccursor(p varchar2)   
  2. as    
  3. v_rownum number(10) := 1;   
  4. cursor c_postype is select pos_type from pos_type_tbl where rownum =1;   
  5. cursor c_postype1 is select pos_type from pos_type_tbl where rownum = v_rownum;   
  6. cursor c_postype2(p_rownum number) is select pos_type from pos_type_tbl where rownum = p_rownum;   
  7. type t_postype is ref cursor ;   
  8. c_postype3 t_postype;   
  9. v_postype varchar2(20);   
  10. begin   
  11.   open c_postype;   
  12.   fetch c_postype into v_postype;   
  13.   dbms_output.put_line(v_postype);   
  14.   close c_postype;   
  15.   open c_postype1;   
  16.   fetch c_postype1 into v_postype;   
  17.   dbms_output.put_line(v_postype);   
  18.   close c_postype1;   
  19.   open c_postype2(1);   
  20.   fetch c_postype2 into v_postype;   
  21.   dbms_output.put_line(v_postype);   
  22.   close c_postype2;   
  23.   open c_postype3 for select pos_type from pos_type_tbl where rownum =1;   
  24.   fetch c_postype3 into v_postype;   
  25.   dbms_output.put_line(v_postype);   
  26.   close c_postype3;   
  27. end;  
create or replace procedure proccursor(p varchar2)
as 
v_rownum number(10) := 1;
cursor c_postype is select pos_type from pos_type_tbl where rownum =1;
cursor c_postype1 is select pos_type from pos_type_tbl where rownum = v_rownum;
cursor c_postype2(p_rownum number) is select pos_type from pos_type_tbl where rownum = p_rownum;
type t_postype is ref cursor ;
c_postype3 t_postype;
v_postype varchar2(20);
begin
  open c_postype;
  fetch c_postype into v_postype;
  dbms_output.put_line(v_postype);
  close c_postype;
  open c_postype1;
  fetch c_postype1 into v_postype;
  dbms_output.put_line(v_postype);
  close c_postype1;
  open c_postype2(1);
  fetch c_postype2 into v_postype;
  dbms_output.put_line(v_postype);
  close c_postype2;
  open c_postype3 for select pos_type from pos_type_tbl where rownum =1;
  fetch c_postype3 into v_postype;
  dbms_output.put_line(v_postype);
  close c_postype3;
end;


cursor c_postype is select pos_type from pos_type_tbl where rownum =1
这一句是定义了一个最普通的游标,把整个查询已经写死,调用时不可以作任何改变。
cursor c_postype1 is select pos_type from pos_type_tbl where rownum = v_rownum;
这一句并没有写死,查询参数由变量v_rownum来决定。需要注意的是v_rownum必须在这个游标定义之前声明。
cursor c_postype2(p_rownum number) is select pos_type from pos_type_tbl where rownum = p_rownum;
这一条语句与第二条作用相似,都是可以为游标实现动态的查询。但是它进一步的缩小了参数的作用域范围。但是可读性降低了不少。
type t_postype is ref cursor ;
c_postype3 t_postype;
先定义了一个引用游标类型,然后再声明了一个游标变量。
open c_postype3 for select pos_type from pos_type_tbl where rownum =1;
然后再用open for 来打开一个查询。需要注意的是它可以多次使用,用来打开不同的查询。
从动态性来说,游标变量是最好用的,但是阅读性也是最差的。
注意,游标的定义只能用使关键字IS,它与AS不通用。

3.3 游标循环最佳策略
我们在进行PL/SQL编程时,经常需要循环读取结果集的数据。进行逐行处理,这个过程就需要对游标进行循环。对游标进行循环的方法有多种,我们在此一一分析。

Java代码 复制代码
  1. create or replace procedure proccycle(p varchar2)   
  2. as    
  3. cursor c_postype is select pos_type, description from pos_type_tbl where rownum < 6;   
  4. v_postype varchar2(20);   
  5. v_description varchar2(50);   
  6. begin   
  7. open c_postype;   
  8.   if c_postype%found then   
  9.     dbms_output.put_line('found true');   
  10.   elsif c_postype%found = false then   
  11.     dbms_output.put_line('found false');   
  12.   else  
  13.     dbms_output.put_line('found null');   
  14.   end if;   
  15.   loop   
  16.    fetch c_postype into v_postype,v_description ;   
  17.    exit when c_postype%notfound;   
  18.    dbms_output.put_line('postype:'||v_postype||',description:'||v_description);   
  19.   end loop;   
  20.   close c_postype;   
  21. dbms_output.put_line('---loop end---');   
  22.   open c_postype;   
  23.     fetch c_postype into v_postype,v_description;   
  24.     while c_postype%found loop   
  25.       dbms_output.put_line('postype:'||v_postype||',description:'||v_description);   
  26.       fetch c_postype into v_postype,v_description ;   
  27.     end loop;   
  28.   
  29.   close c_postype;   
  30. dbms_output.put_line('---while end---');   
  31.   for v_pos in c_postype loop   
  32.     v_postype := v_pos.pos_type;   
  33.     v_description := v_pos.description;   
  34.     dbms_output.put_line('postype:'||v_postype||',description:'||v_description);   
  35.   end loop;   
  36.   dbms_output.put_line('---for end---');   
  37. end;  
create or replace procedure proccycle(p varchar2)
as 
cursor c_postype is select pos_type, description from pos_type_tbl where rownum < 6;
v_postype varchar2(20);
v_description varchar2(50);
begin
open c_postype;
  if c_postype%found then
    dbms_output.put_line('found true');
  elsif c_postype%found = false then
    dbms_output.put_line('found false');
  else
    dbms_output.put_line('found null');
  end if;
  loop
   fetch c_postype into v_postype,v_description ;
   exit when c_postype%notfound;
   dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
  end loop;
  close c_postype;
dbms_output.put_line('---loop end---');
  open c_postype;
    fetch c_postype into v_postype,v_description;
    while c_postype%found loop
      dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
      fetch c_postype into v_postype,v_description ;
    end loop;

  close c_postype;
dbms_output.put_line('---while end---');
  for v_pos in c_postype loop
    v_postype := v_pos.pos_type;
    v_description := v_pos.description;
    dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
  end loop;
  dbms_output.put_line('---for end---');
end;


使用游标之前需要开打游标,open cursor,循环完后再关闭游标close cursor.
这是使用游标应该慎记于心的法则。
上面的过程演示了游标循环的三种方法。
在讨论循环方法之前,我们先看看%found和%notfound这些游标的属性。

Java代码 复制代码
  1. open c_postype;   
  2.  if c_postype%found then   
  3.    dbms_output.put_line('found true');   
  4.  elsif c_postype%found = false then   
  5.    dbms_output.put_line('found false');   
  6.  else  
  7.    dbms_output.put_line('found null');   
  8.  end if;  
 open c_postype;
  if c_postype%found then
    dbms_output.put_line('found true');
  elsif c_postype%found = false then
    dbms_output.put_line('found false');
  else
    dbms_output.put_line('found null');
  end if;

在打开一个游标之后,马上检查它的%found或%notfound属性,它得到的结果即不是true也不是false.而是null.必须执行一条fetch语句后,这些属性才有值。

第一种使用loop 循环

Java代码 复制代码
  1. loop   
  2.    fetch c_postype into v_postype,v_description ;   
  3.    exit when c_postype%notfound;   
  4.    ……   
  5. end loop  
loop
   fetch c_postype into v_postype,v_description ;
   exit when c_postype%notfound;
   ……
end loop

这里需要注意,exit when语句一定要紧跟在fetch之后。必避免多余的数据处理。
处理逻辑需要跟在exit when之后。这一点需要多加小心。
循环结束后要记得关闭游标。

第二种使用while循环。

Java代码 复制代码
  1.    fetch c_postype into v_postype,v_description;   
  2. while c_postype%found loop   
  3.    ……   
  4.       fetch c_postype into v_postype,v_description ;   
  5. end loop;  
   fetch c_postype into v_postype,v_description;
while c_postype%found loop
   ……
      fetch c_postype into v_postype,v_description ;
end loop;


我们知道了一个游标打开后,必须执行一次fetch语句,游标的属性才会起作用。所以使用while 循环时,就需要在循环之前进行一次fetch动作。
而且数据处理动作必须放在循环体内的fetch方法之前。循环体内的fetch方法要放在最后。否则就会多处理一次。这一点也要非常的小心。
总之,使用while来循环处理游标是最复杂的方法。

第三种 for循环

Java代码 复制代码
  1. for v_pos in c_postype loop   
  2.    v_postype := v_pos.pos_type;   
  3.    v_description := v_pos.description;   
  4.    …   
  5.  end loop;  
 for v_pos in c_postype loop
    v_postype := v_pos.pos_type;
    v_description := v_pos.description;
    …
  end loop;

可见for循环是比较简单实用的方法。
首先,它会自动open和close游标。解决了你忘记打开或关闭游标的烦恼。
其它,自动定义了一个记录类型及声明该类型的变量,并自动fetch数据到这个变量中。
我们需要注意v_pos 这个变量无需要在循环外进行声明,无需要为其指定数据类型。
它应该是一个记录类型,具体的结构是由游标决定的。
这个变量的作用域仅仅是在循环体内。
把v_pos看作一个记录变量就可以了,如果要获得某一个值就像调用记录一样就可以了。
如v_pos.pos_type
由此可见,for循环是用来循环游标的最好方法。高效,简洁,安全。
但遗憾的是,常常见到的却是第一种方法。所以从今之后得改变这个习惯了。

分享到:
评论

相关推荐

    oracle存储过程常用技巧

    oracle学习中存储过程常用技巧,简单易懂。适合初学者

    oracle存储过程常用的一些功能

    介绍oracle存储过程的常用使用技巧

    oracle存储过程常用的技巧(详)

    存储过程是在大型数据库系统中存储过程在数据库中经过第一次编译后就不需要再次编译,用户通过指定存储过程的名字并给出参数来,通过本篇文章带领大家去学习oracle存储过程常用的技巧,感兴趣的朋友一起来学习吧

    VB常用技巧合集

    VB常用技巧合集 chm全集,比如ActiveX .Exe .Dll Server的多执行绪,ADO/DAO Bolb资料的存取,ADO/RDO Concurrency for SQL 的比较,ADO 连线的其他注意事项,ADO阶层式资料库表达,ADO设定独占性的资料库,Check两...

    oracle数据库中查看系统存储过程的方法

    您可能感兴趣的文章:Oracle存储过程游标用法分析oracle存储过程常用的技巧(详)asp.net中调用oracle存储过程的方法C#调用Oracle存储过程的方法oracle 存储过程详细介绍(创建,删除存储过程,参数传递等)Oracle存储...

    Oracle常用代码技巧

    java调用Oracle的存储过程 Oracle实现分页

    Cognos常用开发技巧及答疑

    主要内容: Framework Manager 分层设计思想、存储过程运用 Report Studio 报表类型选择、条件变量运用、聚合方式选择、报表数据处理 RIDE 特殊参数设计、报表调用

    ASP.NET技巧收集

    Asp.net页面内传参数方法,调用存储过程(两种方法比较),调用存储过程通用类DBHelper,加密解密,SQL常用DBHelper,Asp.net存储过程无限分类,TreeView无限分类,无限分类MVC,荧光棒效果 获取控件上全选,回车转换...

    MySQL存储过程概念、原理与常见用法详解

    主要介绍了MySQL存储过程概念、原理与常见用法,结合实例形式详细分析了mysql存储过程的概念、原理、创建、删除、调用等各种常用技巧与相关注意事项,需要的朋友可以参考下

    常用SQL 语句大全

    11:查看与某一个表相关的视图、存储过程、函数 12:查看当前数据库中所有存储过程 13:查询用户创建的所有数据库 14:查询某一个表的字段和数据类型 15:不同服务器数据库之间的数据操作 SQL Server基本函数 ...

    MySQL数据库常用操作技巧总结

    主要介绍了MySQL数据库常用操作技巧,结合实例形式总结分析了mysql查询、存储过程、字符串截取、时间、排序等常用操作技巧,需要的朋友可以参考下

    asp.net开发常用整理集合

     调用存储过程(两种方法比较)  调用存储过程通用类DBHelper  加密解密  SQL常用DBHelper  Asp.net存储过程无限分类  TreeView无限分类  无限分类MVC  荧光棒效果 获取控件上全选  回车转换成Tab ...

    C#开发经验技巧宝典

    0925 如何获取数据库中的全部存储过程 540 0926 如何正确认识触发器 541 0927 Update触发器在系统日志中的应用 542 0928 触发器的嵌套使用 542 0929 获取数据库中的触发器 543 19.12 其他 544 0930 在...

    sql 常用技巧整理

    ALTER TABLE –修改数据库表结构 CREATE VIEW –创建一个视图 DROP VIEW –从数据库中删除视图 CREATE INDEX –为数据库表创建一个索引 DROP INDEX –从数据库中删除索引 CREATE PROCEDURE –创建一个存储过程 DROP ...

    [Visual.C..编程技巧精选500例]源代码.随书附盘.pdf

    第2章 常用控件 第3章 通用对话框 第4章 标题栏与菜单栏 第5章 工具栏与状态栏 第6章 图标与光标 第7章 程序窗口 第8章 程序控制 第9章 进程与线程 第10章 字符串 第11章 文件读写操作 第12章 文件与文件夹属性操作 ...

    oracle .

    常用技巧 Oracle PL/SQL基础 PL/SQL 块结构和组成元素 PL/SQL 处理流程 光标的使用 错误处理 存储过程和函数 创建包和使用包 触发器 外部存储过程 会话间通信 数据库作业和文件I/O 在PL/SQL 使用SQL语句 PL/SQL程序...

    Oracle优化日记:一个金牌DBA的故事 白鳝.扫描版

    自动段存储空间管理35月27日 无奈今日点评优化小技巧 临时表空间和排序优化小技巧 undo表空间和回滚段5月28日 BBED的妙用今日点评优化小技巧 如何启用BBED优化小技巧 模拟ORA-8102及处理过程优化小技巧 如何计算数据...

Global site tag (gtag.js) - Google Analytics