Oracle 自定义函数Function

时间:2021-10-11 04:44:12

        函数用于返回特定数据,执行时得找一个变量接收函数的返回值。转换大写的金额,相信有不少人用过,网上例子也有不少,这次在项目中运用到,就顺便做下记录,方便查找,以备不时之需。我工作的项目中常常用到ireport 工具来进行报表开发,报表内容涉及较多的就是大写金额,这也是我首次使用Oracle自定义函数,这时我们需要在Oracle中自定义一个function。函数的参数有3种类型:

(1)in参数类型:表示输入给函数的参数,该参数只能用于传值,不能被赋值。

(2)out参数类型:表示参数在函数中被赋值,可以传给函数调用程序,该参数只能用于赋值,不能用于传值。

(3)in out参数类型:表示参数既可以传值,也可以被赋值。

多个参数之间用“,”隔开;多个变量之间用“;”隔开。

1.函数的创建

语法格式:

CREATE OR REPLACE FUNCTION function_name         /*函数名称*/
(
    parameter1 parameterType1 dataType1,     /*参数定义部分*/
    parameter2 parameterType2 dataType2,
    parameter3 parameterType3 dataType3
    …
)
RETURN return_datatype                /*定义返回值类型*/
IS/AS
BEGIN
      Function_body                  /*函数体部分*/
      RETURN scalar_expression                  /*返回语句*/
END function_name;

----------
其中,参数定义为(parameter*)、参数类型(parameterType*)、参数的数据类型(dataType*)

 

下面,是我在网上找到的例子,略加修改更符合我的项目需求,具体如下:

CREATE OR REPLACE Function FUN_CONVERT_MONEY(Money In Number)
  Return Varchar2 Is
  strYuan       Varchar2(150);
  strYuanFen    Varchar2(152);
  numLenYuan    Number;
  numLenYuanFen Number;
  strRstYuan    Varchar2(600);
  strRstFen     Varchar2(200);
  strRst        Varchar2(800);
  Type typeTabMapping Is Table Of Varchar2(2) Index By Binary_Integer;
  tabNumMapping  typeTabMapping;
  tabUnitMapping typeTabMapping;
  numUnitIndex   Number;
  i              Number;
  j              Number;
  charCurrentNum Char(1);
Begin
  If Money Is Null Then
    Return Null;
  End If;
  IF Money < 0 THEN
    strYuan := TO_CHAR(FLOOR(0 - Money));
  ELSE
    strYuan := TO_CHAR(FLOOR(Money));
  END IF;  If strYuan = '0' Then
    numLenYuan := 0;
    strYuanFen := lpad(TO_CHAR(FLOOR(Money * 100)), 2, '0');
  Else
    numLenYuan := length(strYuan);
    strYuanFen := TO_CHAR(FLOOR(Money * 100));
  End If;
  If strYuanFen = '0' Then
    numLenYuanFen := 0;
  Else
    numLenYuanFen := length(strYuanFen);
  End If;
  If numLenYuan = 0 Or numLenYuanFen = 0 Then
    strRst := '零元整';
    Return strRst;
  End If;
  tabNumMapping(0) := '零';
  tabNumMapping(1) := '壹';
  tabNumMapping(2) := '贰';
  tabNumMapping(3) := '叁';
  tabNumMapping(4) := '肆';
  tabNumMapping(5) := '伍';
  tabNumMapping(6) := '陆';
  tabNumMapping(7) := '柒';
  tabNumMapping(8) := '捌';
  tabNumMapping(9) := '玖';
  tabUnitMapping(-2) := '分';
  tabUnitMapping(-1) := '角';
  tabUnitMapping(1) := '';
  tabUnitMapping(2) := '拾';
  tabUnitMapping(3) := '佰';
  tabUnitMapping(4) := '仟';
  tabUnitMapping(5) := '万';
  tabUnitMapping(6) := '拾';
  tabUnitMapping(7) := '佰';
  tabUnitMapping(8) := '仟';
  tabUnitMapping(9) := '亿';
  For i In 1 .. numLenYuan Loop
    j            := numLenYuan - i + 1;
    numUnitIndex := Mod(i, 8);
    If numUnitIndex = 0 Then
      numUnitIndex := 8;
    End If;
    If numUnitIndex = 1 And i > 1 Then
      strRstYuan := tabUnitMapping(9) || strRstYuan;
    End If;
    charCurrentNum := substr(strYuan, j, 1);
    If charCurrentNum <> 0 Then
      strRstYuan := tabNumMapping(charCurrentNum) ||
                    tabUnitMapping(numUnitIndex) || strRstYuan;
    Else
      If (i = 1 Or i = 5) Then
        If substr(strYuan, j - 3, 4) <> '0000' Then
          strRstYuan := tabUnitMapping(numUnitIndex) || strRstYuan;
        End If;
      Else
        If substr(strYuan, j + 1, 1) <> '0' Then
          strRstYuan := tabNumMapping(charCurrentNum) || strRstYuan;
        End If;
      End If;
    End If;
  End Loop;
  For i In -2 .. -1 Loop
    j:= numLenYuan - i;
    charCurrentNum := substr(strYuanFen, j, 1);
    If charCurrentNum <> '0' Then
      strRstFen := tabNumMapping(charCurrentNum) || tabUnitMapping(i) ||
                   strRstFen;
    End If;
  End Loop;
  If strRstYuan Is Not Null Then
    strRstYuan := strRstYuan || '元';
  End If;
  If strRstFen Is Null Then
    strRstYuan := strRstYuan || '整';
  Elsif length(strRstFen) = 2 And substr(strRstFen, 2) = '角' Then
    strRstFen := strRstFen || '整';
  End If;
  strRst := strRstYuan || strRstFen;
  IF Money < 0 THEN
    Return '(负数)'||strRst;
  else
    Return strRst;
  end if;
End FUN_CONVERT_MONEY;

------自定义函数的调用方法和Oracle的其它内部函数是一样的。

最终结果:

select fun_convert_money(1111.11) as 金额  from dual;

Oracle 自定义函数Function

以上就是我对Oracle自定义function的总结,好了,就暂时到这里了,在2014最后的一天,祝大家在新的一年里好运连连,幸福绵绵!