建立临时表和查找断号

把自定义的查询结果集放到临时表,然后操作该临时表的数据;


  type table_type is table of my_table%rowtype index by binary_integer;
  tmp_table table_type;--创建一个my_table(自己表)类型相同的临时集合

v_insert_num integer default 0;--成功插入的数量

--循环插入数据到临时表

       for x in ( SELECT Zyfxdm,Zyfxmc,Sfxtzfx,Xz,Pycc FROM XTGL_ZYFXXXSJZL a where a.zyfxdm=i_zyfxdmArr(i) )loop
         tmp_table(v_insert_num).Zyfxdm:=x.Zyfxdm;
         tmp_table(v_insert_num).Sfxtzfx:=x.Sfxtzfx;
         v_insert_num:=v_insert_num+1;

      end loop;

--然后查询这个临时表数据时(报错),因为这不是索引表,该方法行不通

select * from tmp_table

正确做法(方法之一):



1.先在Types里面定义临时表的结构;

2.存储过程中把数据插入该临时表;

 
CREATE OR REPLACE PROCEDURE jxgl_zwh(i_zphzj  in varchar2,
                                     o_zwh    out integer,
                                     o_errMsg out varchar2) IS
  /* 就业管理_展位号
  *  根据招聘会主键查找该招聘会回执企业中是否存在展位号数据,
  *  如果没有则返回1作为展位号,如果有数据则查找断位号
  
  i_zphzj:招聘会主键
  o_zwh:返回展位号
  o_errMsg:返回的错误信息
  */

  v_count_num  varchar2(10); --表数据
  v_count_zwh  varchar2(10); --断位号数
  arr_zwh      TYPE_Zwh_TABLE := TYPE_Zwh_TABLE();
  v_num integer default 1; --成功插入的数量

BEGIN

  for x in (SELECT zwh
              FROM jygl_chqyhz
             WHERE zphzj = i_zphzj
               and zwh is not null
             order by zwh) loop
    arr_zwh.extend;
    arr_zwh(v_num) := TYPE_Zwh(null);
    arr_zwh(v_num).zwh := x.zwh;
    v_num := v_num + 1;
  end loop;

  select count(1) into v_count_num from table(arr_zwh);
  SELECT COUNT(1)
    into v_count_zwh
    FROM (select n
            from (select rownum n
                    from table(arr_zwh) a, table(arr_zwh) b
                   where rownum < (select max(zwh) from table(arr_zwh))
                  minus
                  select zwh from table(arr_zwh))
           where n > 0
             and rownum = 1); --查询展位号有没有断号

  if v_count_num = 0 then
    --表中没有数据则从1开始
    o_zwh := 1;
  elsif v_count_num != 0 and v_count_zwh = 0 then
    --没有断号则查找最大值
    select (nvl2(max(zwh), max(zwh), 0) + 1)
      into o_zwh
      from table(arr_zwh);
  
  else
    --从1开始查找断号
    select n
      into o_zwh
      from (select rownum n
              from table(arr_zwh) a, table(arr_zwh) b
             where rownum < (select max(zwh) from table(arr_zwh))
            minus
            select zwh from table(arr_zwh))
     where n > 0
       and rownum = 1;
  end if;

exception
  when others then
    o_errMsg := o_errMsg || ':程序运行出现内部错误,请联系管理员。' ||
                dbms_utility.format_error_backtrace() || SQLCODE || '---' ||
                SQLERRM;
END jxgl_zwh;

 o_errMsg 中dbms_utility.format_error_backtrace()     --返回报错行数

                      SQLCODE    --报错代码

                      SQLERRM;   --报错问题

------------------------------------------------------------------------------------------------------------

查找断位号语句

select f
  from (select rownum f
          from test a, test b
         where rownum < (select max(id) from test)
        minus
        select id from test)
 where f > 0 and rownum=1;  --查找断号

--功能摘自:就业管理,招聘会管理

猜你喜欢

转载自563432906.iteye.com/blog/2282616