手机版
你好,游客 登录 注册
背景:
阅读新闻

Oracle 创建函数实例

[日期:2012-07-26] 来源:Linux社区  作者:ithead [字体:大 中 小]

1.Oracle 创建函数

  1. create [or replace] function function_name   
  2. [(parameter_name [in | out | in out] type [,...])]   
  3. retutn type   
  4. is|as  
  5. begin  
  6.  function_body;   
  7. end;  

说明:函数必须有返回值。
or replace:如果函数已经存在,则替换现有的函数;
function_name:函数名;
parameter_name :参数名;
in | out | in out:指定参数的模式;
function_body:包含执行任务函数的SQL和PL/SQL语句。

2、函数实例

  1. create or replace function get_publicholidaytime(fromtime in Date, totime in Date, isAvailable in number) return number    
  2. as  
  3.   --定义变量   
  4.   free_day number :=0;   
  5.   mindate Date;   
  6.   maxdate Date;   
  7.   total number :=0;   
  8.   fromdate Date := TO_DATE(to_char(fromtime,'yyyy/MM/dd'),'yyyy/MM/dd');   
  9.   todate Date := TO_DATE(to_char(totime,'yyyy/MM/dd'),'yyyy/MM/dd');   
  10. begin  
  11.   --if语句   
  12.   if isAvailable=1 then  
  13.     --sql语句   
  14.     select min(calendar_date) ,max(calendar_date),count(calendar_date) into mindate,maxdate,total    
  15.     from T_BI_TNT_DATE    
  16.     where calendar_date between fromdate and todate   
  17.     and IS_PUBLIC_HOLIDAY=1 ;   
  18.     if mindate = fromdate then    
  19.        total := total - 1;free_day := free_day + ((mindate + 1) - fromtime);   
  20.     end if;   
  21.     if maxdate = todate then  
  22.        total := total - 1;free_day := free_day + (totime - maxdate);   
  23.     end if;   
  24.     if mindate = maxdate then    
  25.        free_day := totime - fromtime;   
  26.     else  
  27.        free_day := free_day + total;   
  28.     end if;   
  29.   end if;   
  30.   return free_day;   
  31. end get_publicholidaytime;  

3、调用函数

select get_publicholidaytime(sysdate,sysdate + 1,1) from dual;  

4、删除函数

drop function get_publicholidaytime;  

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款