存儲過程常用技巧4

上面的兩種方式都是非常的複雜,如果僅僅是需要返回一個結果集,那就完全可以使用函數來實現了。
Java代碼
create or replace package procpkg is  
   type refcursor is ref cursor;  
   procedure procrefcursor(p varchar2, p_ref_postypeList  out refcursor);  
   function procpostype(p varchar2) return PosTypeTable;   
end procpkg;  
 
create or replace package body procpkg is  
  procedure procrefcursor(p varchar2, p_ref_postypeList out  refcursor)  
  is  
    v_posTypeList PosTypeTable;  
  begin  
    v_posTypeList :=PosTypeTable();--初始化嵌套表  
    v_posTypeList.extend;  
    v_posTypeList(1) := PosType('A001','客戶資料變更');  
    v_posTypeList.extend;  
    v_posTypeList(2) := PosType('A002','團體資料變更');  
    v_posTypeList.extend;  
    v_posTypeList(3) := PosType('A003','受益人變更');  
    v_posTypeList.extend;  
    v_posTypeList(4) := PosType('A004','續期交費方式變更');  
    open p_ref_postypeList for  select * from table(cast (v_posTypeList as PosTypeTable));  
  end;  
 
  function procpostype(p varchar2) return PosTypeTable  
  as  
   v_posTypeList PosTypeTable;  
  begin  
      v_posTypeList :=PosTypeTable();--初始化嵌套表  
    v_posTypeList.extend;  
    v_posTypeList(1) := PosType('A001','客戶資料變更');  
    v_posTypeList.extend;  
    v_posTypeList(2) := PosType('A002','團體資料變更');  
    v_posTypeList.extend;  
    v_posTypeList(3) := PosType('A003','受益人變更');  
    v_posTypeList.extend;  
    v_posTypeList(4) := PosType('A004','續期交費方式變更');  
    return  v_posTypeList;  
  end;  
end procpkg; 

create or replace package procpkg is
   type refcursor is ref cursor;
   procedure procrefcursor(p varchar2, p_ref_postypeList  out refcursor);
   function procpostype(p varchar2) return PosTypeTable;
end procpkg;

create or replace package body procpkg is
  procedure procrefcursor(p varchar2, p_ref_postypeList out  refcursor)
  is
    v_posTypeList PosTypeTable;
  begin
    v_posTypeList :=PosTypeTable();--初始化嵌套表
    v_posTypeList.extend;
    v_posTypeList(1) := PosType('A001','客戶資料變更');
    v_posTypeList.extend;
    v_posTypeList(2) := PosType('A002','團體資料變更');
    v_posTypeList.extend;
    v_posTypeList(3) := PosType('A003','受益人變更');
    v_posTypeList.extend;
    v_posTypeList(4) := PosType('A004','續期交費方式變更');
    open p_ref_postypeList for  select * from table(cast (v_posTypeList as PosTypeTable));
  end;

  function procpostype(p varchar2) return PosTypeTable
  as
   v_posTypeList PosTypeTable;
  begin
      v_posTypeList :=PosTypeTable();--初始化嵌套表
    v_posTypeList.extend;
    v_posTypeList(1) := PosType('A001','客戶資料變更');
    v_posTypeList.extend;
    v_posTypeList(2) := PosType('A002','團體資料變更');
    v_posTypeList.extend;
    v_posTypeList(3) := PosType('A003','受益人變更');
    v_posTypeList.extend;
    v_posTypeList(4) := PosType('A004','續期交費方式變更');
    return  v_posTypeList;
  end;
end procpkg;
ibatis配置
Java代碼
<resultMap id="posTypeResultMap" class="com.palic.elis.pos.common.dto.CodeTableItemDTO">  
   <result property="code" column="posType"/>  
   <result property="description" column="description"/>  
 </resultMap>  
 
  <select id="procPostype" resultMap="posTypeResultMap">  
    select * from table(cast (procpkg.procpostype(#value#) as PosTypeTable))  
  </select> 

<resultMap id="posTypeResultMap" class="com.palic.elis.pos.common.dto.CodeTableItemDTO">
   <result property="code" column="posType"/>
   <result property="description" column="description"/>
 </resultMap>

  <select id="procPostype" resultMap="posTypeResultMap">
    select * from table(cast (procpkg.procpostype(#value#) as PosTypeTable))
  </select>Dao的寫法跟普通查詢一樣
Java代碼
public List queryPostype() {  
  return this.getSqlMapClientTemplate().queryForList("pos_dayprocset.procPostype", null);  

public List queryPostype() {
  return this.getSqlMapClientTemplate().queryForList("pos_dayprocset.procPostype", null);
}
有幾點需要注意,這裏不能使用索引表,而是嵌套表。
另外就是把嵌套表強制轉換爲普通表。

 

發佈了66 篇原創文章 · 獲贊 1 · 訪問量 2067
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章