oracle存储函数

create or replace FUNCTION CAIJITONGJI(curdate in VARCHAR2) RETURN VARCHAR2 AS
len NUMBER :=0;--存储传入日期长度
i number :=1;
con varchar2(20) :='';--每次循环的不同条件
defaultStr char(1) :=' ';--默认连接符
endNum NUMBER :=24;--默认循环次数
format VARCHAR2(15) :='yyyy-MM-dd hh24';--日期的默认格式化形式
num number :='';--统计数量
str varchar2(255) :='';--函数返回值
BEGIN
--修改循环次数等变量的值
select length(curdate) into len from dual;
if (len=7) then endNum :=31; format :='yyyy-MM-dd'; defaultstr :='-';
end if;
loop
exit when i>endNum;
if(i<10) then con :='0'||i; --不能用+连接
elsif (endnum=24 and i=24) then con :='00';
else con :=i;
end if;
select count(*) into num from t_Iopenet_Real_Data ra where to_char(ra.occurtime,format)=(select curdate||defaultstr||con from dual);
select str||'-'||num into str from dual;
i := i+1;
end loop;
RETURN str;
END CAIJITONGJI;

原文地址:https://www.cnblogs.com/shenjun/p/3084118.html