日期:2014-05-17  浏览次数:20857 次

oracle用户自定义函数问题,望求助!!
各位好,因本人对oracle是初学者在其方面上欠缺还很多,所以望给予指教:
  下面是在oracle中增加自定义函数 fn_getdays 但是在if... then ... else if ... ...方面出现了问题,经努力还是没找到原因。
望在行的人帮忙看下,指点一下,感激不尽,下面是我修改过的,但是有问题给予改正。该函数是为求本月的实际天数,p1参数是传入的日期比如:“20040101”
------------------------------
  create or replace function fn_getdays(p1 varchar2) return integer
is
  v_year varchar(4);
  v_month varchar2(2);
  v_return int;
--Result integer;
begin
  v_year := substring(p1,1,4);
  v_month := substring(p1,5,2);
   
  if v_month in ('01','03','05','07','08','10','12') then --如果是31天的月份
  v_return := 31 
  else if v_month in ('04','06','09','11') then --如果是30天的月份
  v_return := 30
  end if;
  else if v_month in ('02') then --如果是2月份
  begin
if cast(v_year as int) / 4 = 0 then --如果整除4,则是闰年
v_return := 29
else
v_return := 28  
  end if;  
  end if;
  end;
  end if;
  end if;
  return(v_return);
end fn_getdays;

------解决方案--------------------
这个是IF的语法.
IF condition1 THEN
 statement1;
ELSIF condition2 THEN
 statement2;
ELSIF condition3 THEN
 statement3;
ELSE
 statement4;
END IF;
 



------解决方案--------------------
只有两个if,却有5个end if
这么明显的错误,楼主居然都发现不了
SQL code

create or replace function fn_getdays(p1 varchar2) return integer 
is 
    v_year  varchar(4); 
    v_month varchar2(2); 
    v_return int; --Result integer; 
begin 
    v_year := substring(p1,1,4); 
    v_month := substring(p1,5,2); 
  
    if v_month in ('01','03','05','07','08','10','12') then  --如果是31天的月份 
        v_return := 31 
    else if v_month in ('04','06','09','11') then  --如果是30天的月份 
         v_return := 30  
    else if v_month in ('02') then --如果是2月份 
    begin 
        if cast(v_year as int) / 4 = 0 then --如果整除4,则是闰年 
              v_return := 29 
        else 
            v_return := 28      
        end if;    
    end; 
    end if; 
    return(v_return); 
end fn_getdays;