当前位置: 移动技术网 > IT编程>数据库>Oracle > Oracle存储过程、包、方法使用总结(推荐)

Oracle存储过程、包、方法使用总结(推荐)

2017年12月08日  | 移动技术网IT编程  | 我要评论

快乐男声快乐大本营,我看你有戏吴亦凡,张冬玲图片

oracle存储过程、包、方法使用总结,具体代码如示:

/**
 *@author:zhengwei
 *@date:2017-04-28
 *@desc:存储过程用法总结
 */
 create or replace procedure myprocedure(p_id   in varchar,
                     p_status out varchar) --p_id为输入参数 ,p_status为输出参数
 as
 ---变量声明
 t_status   varchar2(20);
 t_id     number;
 v_postype   varchar2(20);
 v_description varchar2(20);
 ---对象变量定义和声明
 type xrecord is record(
  fund   varchar2(50),
  batch_no varchar2(50),
  tran_amt number,
  end_bal  number,
  tran_date varchar2(50),
  tran_time varchar2(50),
  sub_water number);
 xwater xrecord;
 ---游标声明,并填充数据
 cursor my_cur is
  select pos_type, description from votemaster;
begin
 ---变量赋值(注意:in类型的参数不能直接赋值)
 t_status := '1';
 p_status := t_status;
 dbms_output.put_line('p_status:' || p_status);
 begin
  ---循环游标,使用游标
  for v_row in my_cur loop
   begin
    v_postype   := v_row.pos_type;
    v_description := v_row.description;
    dbms_output.put_line('postype:' || v_postype || ',description:' ||
               v_description);
   end;
  end loop;
 end;
 ---while循环用法
 begin
  while i < 10 loop
   begin
    i := i + 1;
   end;
  end loop;
 end;
 --将select查询的结果存入到变量中,可以同时将多个列存储多个变量中,必须有一条记录,否则抛出异常(如果没有记录抛出no_data_found)
 begin
  select col1, col2 into 变量1, 变量2 from typestruct where xxx;
 exception
  when no_data_found then
   xxxx;
 end;
 ---if判断语句用法
 begin
  select votetitle, vatesum
   into t_name, t_count
   from votemaster
   where id = p_id;
  if t_count <= 0 then
   p_status := t_name || ':差';
  elsif t_count > 0 and t_count < 3 then
   p_status := t_name || ':良好';
  else
   p_status := t_name || ':优秀';
  end if;
 end;
 ---对象变量赋值
 begin
  select fund,
      batch_no,
      tran_amt,
      end_bal,
      tran_date,
      tran_time,
      sub_water
   into xrecord
   from acct_water
   where fund = p_id;
  --对象变量的使用
  dbms_output.put_line(xrecord.batch_no || xrecord.fund);
 end;
 ---索引表
 ---我们在使用存储过程的时候经常需要处理记录集,也就是多条数据记录。分为单列多行和多列多行,这些类型都可以称为集合类型。索引表就是集合类型中的一种。 
 ---索引表,也称为pl/sql表,不能存储于数据库中,元素的个数没有限制,下标可以为负值。
 ---使用场景:如果仅仅是在存储过程中当作集合变量使用,索引表是最好的选择。(也可以通过创建临时表替代,但就不那么科学了,而且后期还得维护临时表)
 ---索引表对象使用方案1:
 begin
  ---索引表对象声明、定义、使用
  declare
   type acct_table_type is table of acct%rowtype index by binary_integer;
   ---定义了一个索引表v_acct_table,其表中的每行记录是acct表中的一行记录
   v_acct_table acct_table_type;
  begin
   select * bulk collect ---bulk collect into指是一个成批聚合类型, 简单的来说 , 它可以存储一个多行多列存储类型
    into v_acct_table
    from acct
    where acct_type = '570'
     and rownum < 5;
   for i in 1 .. v_acct_table.count loop
    dbms_output.put_line('acct:' || v_acct_table(i).fund || ',' || v_acct_table(i).bal || ',' || v_acct_table(i)
               .real_nmbr);
   end loop;
  end;
 end;
 ---索引表对象使用方案2:
 begin
  --例子:利用记录record可用整体赋值的特性来填充pl/sql表
  declare
   type rectype is record(
    fund   acct.fund%type,, ---表示定义的变量的类型为表acct的fund字段的同样数据类型
    bal    acct.bal%type,
    owner   acct.owner%type,
    real_nmbr varchar(30));
   ---定义了一个索引表mytab,其表中的每行记录是record 
   type tabtype is table of rectype index by binary_integer;
   mytab tabtype;
   vn  number;
  begin
   --填充 
   vn := 1;
   for varr in (select fund, bal, owner, real_nmbr
           from acct
          where rownum <= 15
          order by fund asc) loop
    mytab(vn) := varr; --记录整体赋值  
    vn := vn + 1;
   end loop;
   --访问
   vn := mytab.first;
   for varr in vn .. mytab.count loop
    dbms_output.put_line(vn || '  ' || mytab(vn).fund || '  ' || mytab(vn).bal ||
               '  ' || mytab(vn).owner || '  ' || mytab(vn)
               .real_nmbr);
    vn := mytab.next(vn);
   end loop;
  end;
 end;

以上所述是小编给大家介绍的oracle存储过程、包、方法使用总结(推荐),希望对大家有所帮助,如果大家有所疑问欢迎给我留言,小编会及时回复大家的!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网