1. 程式人生 > >oracle 自定義儲存過程

oracle 自定義儲存過程

語法

建立 TYPE 型別 atrr_type

CREATE OR REPLACE TYPE atrr_type  AS OBJECT
(
       attrId varchar2(40),
       objType varchar2(40)
);

將 TYPE 型別 atrr_type 定義為表, 用做接收返回值

CREATE OR REPLACE TYPE attr_table AS TABLE of atrr_type;

定義儲存過程 入參為 objt 出參為 一張表

create or replace procedure selectAttr(objt in varchar2, attr out attr_table) is 
  type_row atrr_type; 
begin
  attr := attr_table();		-- 初始化返回結果
  for thisrow in (select attr_id as attrId, obj_type as objType from CPS_OBJ_ATTR where obj_type = objt) 
    loop 
      type_row := atrr_type(thisrow.attrId, thisrow.objType); 
	  attr.extend;
      attr(attr.count) := type_row; 
    end loop;
end selectAttr;

儲存過程的使用 定義一個函式來試用它

create or replace function testPro(objType in varchar2) return attr_table is
   attr attr_table:=attr_table();
begin
  selectAttr(objType, attr);
  return(attr);
end testPro;