Oracle函式返回陣列

2021-09-01 19:04:08 字數 3913 閱讀 3638

create or replace type defect_number3 as object(

id varchar2(100),

urgent number(10,2),

major number(10,2),

norm number(10,2),

total number(10,2) )

create or replace type defect_number_array as table of defect_number3 

以前專案中寫過的乙個函式:

create or replace function f_get_defect_total_onerate(timerangestart  in varchar2,

timerangeend    in varchar2,

deviceclassify  in varchar2,

voltagelevel    in varchar2,

defectstatus    in varchar2,

company         in varchar2,

vyear           in varchar2,

devicepart      in varchar2,

devicefactory   in varchar2,

defectclass     in varchar2,

deviceplantyear in varchar2,

devicetype in varchar2,

defectlevel in varchar2)

return defect_number_array is

--在年份不確定的條件下,該函式的最大年份為2011

--timerangestart  開始時間

--timerangeend    結束時間

--voltagelevel    電壓等級  該項可以傳入多個值,每個值之間使用英文逗號隔開「,」

--company         供電局id

--deviceclassify    裝置類別

--defectstatus      缺陷等級

---使用該函式的時候,需要判斷返回結果是否為空,可參考以下

----select  * from table(f_pub_get_defect_rate('20120101','20120131','20024','13, 12, 11, 10','','1909','22'));

var_out      defect_number3 := defect_number3(-1, -1, -1, -1, -1);

var_outt      defect_number3 := defect_number3(-1, -1, -1, -1, -1);

resultdefect defect_number_array := defect_number_array();

minyear          number;

maxyear          number;

vtimerangestart  varchar2(512);

vtimerangeend    varchar2(512);

v2timerangestart varchar2(512);

v2timerangeend   varchar2(512);

urgent number(10,2);

major number(10,2);

norm number(10,2);

total number(10,2);

years number(10);

begin

if ('empty' != nvl(vyear, 'empty')) then

--電壓等級

minyear := substr(timerangestart, 0, 4);

select f_get_defect_rate(timerangestart,

timerangeend,

deviceclassify,

voltagelevel,

defectstatus,

company,

minyear,

devicepart,

devicefactory,

defectclass,deviceplantyear,devicetype,defectlevel)

into var_out

from dual;

resultdefect.extend();

resultdefect(resultdefect.last) := var_out;

else

vtimerangestart := timerangestart;

vtimerangeend   := timerangeend;

if ('empty' = nvl(timerangestart, 'empty')) then

vtimerangestart := '0101';

vtimerangeend   := '1231';

end if;

if('empty' != nvl(company, 'empty')) then

select min(record_defect_year) into minyear from dim_operation_unit where id = company;

else

select min(record_defect_year) into minyear from dim_operation_unit;

end if;

select to_char(sysdate-365, 'yyyy') into maxyear from dual;

years:=maxyear - minyear + 1;

urgent:=0;major:=0;norm:=0;total:=0;

for i in 1 .. (maxyear - minyear + 1) loop

v2timerangestart := minyear || vtimerangestart;

v2timerangeend   := minyear || vtimerangeend;

select f_get_defect_rate(v2timerangestart,

v2timerangeend,

deviceclassify,

voltagelevel,

defectstatus,

company,

minyear,

devicepart,

devicefactory,

defectclass,deviceplantyear,devicetype,defectlevel)

into var_out

from dual;

urgent:=urgent+ var_out.urgent;

major:=major+var_out.major;

norm:=norm+var_out.norm;

total:=total+var_out.total;

minyear := minyear + 1;

end loop;

var_outt.urgent:=urgent/years;

var_outt.major:=major/years;

var_outt.norm:=norm/years;

var_outt.total:=total/years;

resultdefect.extend();

resultdefect(resultdefect.last) := var_outt;

end if;

return resultdefect;

end f_get_defect_total_onerate;

Oracle 實現 split 函式 返回陣列)

功能描述 用指定分隔符切割輸入的字串,返回一維陣列,每個陣列元素為乙個子串。源 create or replace type ty str split is table of varchar2 4000 create or replace function fn split p str in var...

函式返回陣列

函式是不能返回陣列的,因此很直接的就想到的是返回指標,指標的宣告位置也要注意,防止在子函式中記憶體釋放掉了,因此用new進行動態分配記憶體,最後注意記憶體的釋放 陣列的釋放與動態分配的變數的釋放方式還有所不同 錯誤的 如下 include using namespace std float mult...

c 函式返回陣列

c 語言 不允許返回乙個完整的陣列作為函式的引數 但是,您 可以通過指定不帶索引的陣列名來返回乙個指向陣列的指標 我們將在下一章中講解有關指標的知識,您可以先跳過本章,等了解了 c 指標的概念之後,再來學習本章的內容。如果您想要從函式返回乙個一維陣列,您必須宣告乙個返回指標的函式,如下 int my...