oracle中求n的阶乘,怎么写啊

解决方案 »

  1.   

    create or replace function fun_n_jc (i in number)
    return numberis
    n number;
    Begin
      for c in 1..i loop
        n:=n*c;
      end loop;
      return n;
    end;
      

  2.   


    create or replace function fun_factorial(num number)
    return number 
    is 
      result number:=1;
    begin   
       if num<0 then
          raise_application_error(-20012,' function canshu can not <0') ;
       elsif num=0 or num=1 then 
          return 1;
       else
          for i in 2..num loop
              result:=i*result;
          end loop;
          return result;
       end if;
    end fun_factorial;
    SQL> select fun_factorial(5) from dual;
     
    FUN_FACTORIAL(5)
    ----------------
                 120
     
    SQL> select fun_factorial(6) from dual;
     
    FUN_FACTORIAL(6)
    ----------------
                 720
     参考:http://topic.csdn.net/u/20110516/16/b3761513-8ddd-4668-bc5d-cfb970e827c4.html?51453