oracle定义常用函数的过程

1 函数的创建、调用和权限 1 1 创建函数 create or replace function 函数名(参数1 模式 数据类型, ) return

1 函数的创建、调用和权限

1.1 创建函数

create or replace function 函数名(参数1 模式 数据类型,......) return 数据类型
as
  -- 定义局部变量。
  变量1 数据类型;
  ......
begin
  -- 实现函数功能的PL/SQL代码。
  ......
  exception
  -- 异常处理的PL/SQL代码。
  ......
end;

说明:

1)参数的模式有三种:

in:只读模式,在函数中,参数只能被引用/读取,不能改变它的值;

out:只写模式,参数只能被赋值,不能被引用/读取;

in out:可读可写。

注:参数的模式可以不写,缺省为in,out和in out两种模式极少使用。

2)as/is二选一,在这里没有区别;

3)可以不定义局部变量;

4)可以没有异常(exception)处理代码段;

--示例,创建自定义函数maxvalue,用于比较两个数字的大小,返回较大值;
create or replace function maxvalue(val1 number,val2 number) return number
as
  val number;   -- 定义局部变量,存放返回值。
begin
  if (val1>val2) then    -- 判断传入参数的大小。
      val:=val1;         -- 赋值是":=",不是"="。
  else
      val:=val2;
  end if;
  return val;  -- 返回
end;

1.2 函数的调用

自定义函数的调用与Oracle数据库自带的函数调用的方法相同。

select maxvalue(10, 20) from dual;

1.3 函数的权限

自定义函数是数据库对象,oracle对它权限管理方式与其他数据库对象相同;

如果maxvalue函数是用户scott用户创建的,其他用户调用时需要加scott用户名前缀,并且具备相应的权限,否则会出现“ ORA-00904:"MAXVALUE": 标识符无效 ”的错误;

1.4 删除自定义函数

drop function 函数名;
--eg:
drop function maxvalue;

2 MD5加密函数

create or replace function md5(passwd in varchar2) return varchar2 is
  retval varchar2(32);
begin
  retval := utl_raw.cast_to_raw(dbms_obfuscation_toolkit.md5(input_string => passwd));
  return retval;
end;

注:复制该语句,直接在plsql的SQL窗口中执行即可完成创建;

使用:

select lower(用户.md5('1234')) as 密文 from dual;

3 获取汉字首字母拼音的函数

create or replace function fgetpy(v_str varchar2) return varchar2 as
  v_strlen int;
  v_return varchar2(500);
  v_ii     int;
  v_n      int;
  v_c      varchar2(2);
  v_chn    varchar2(2);
  v_rc     varchar2(500);
  /************************************************************************* 
  生成汉字拼音码的函数。 2009-06-21  
  **************************************************************************/
begin
  --dbms_output.put_line(v_str); 
  v_rc     := v_str;
  v_strlen := length(v_rc);
  v_return := '';
  v_ii     := 0;
  while v_ii < v_strlen loop
    v_ii := v_ii + 1;
    v_n  := 63;
    select substr(v_rc, v_ii, 1) into v_chn from dual;
    select v_n + max(rowsf)
      into v_n
      from (select chn, rownum rowsf
              from (select chn
                      from (select '吖' chn
                              from dual
                            union
                            select '八'
                              from dual
                            union all
                            select '嚓'
                              from dual
                            union all
                            select '咑'
                              from dual
                            union all
                            select '妸'
                              from dual
                            union all
                            select '发'
                              from dual
                            union all
                            select '旮'
                              from dual
                            union all
                            select '铪'
                              from dual
                            union all
                            select '丌'
                              from dual --because have no 'i' 
                            union all
                            select '丌'
                              from dual
                            union all
                            select '咔'
                              from dual
                            union all
                            select '垃'
                              from dual
                            union all
                            select '嘸'
                              from dual
                            union all
                            select '拏'
                              from dual
                            union all
                            select '噢'
                              from dual
                            union all
                            select '妑'
                              from dual
                            union all
                            select '七'
                              from dual
                            union all
                            select '呥'
                              from dual
                            union all
                            select '仨'
                              from dual
                            union all
                            select '他'
                              from dual
                            union all
                            select '屲'
                              from dual
                            union all
                            select '屲'
                              from dual
                            union all
                            select '屲'
                              from dual
                            union all
                            select '夕'
                              from dual
                            union all
                            select '丫'
                              from dual
                            union all
                            select '帀'
                              from dual
                            union all
                            select v_chn
                              from dual) a
                     order by nlssort(chn, 'NLS_SORT=SCHINESE_PINYIN_M')) c) b
     where chn = v_chn;
    v_c := chr(v_n);
    if chr(v_n) = '@' then
      --英文直接返回 
      v_c := v_chn;
    end if;
    v_return := v_return || v_c;
    v_return := lower(v_return);
  end loop;
  return v_return;
end fgetpy;

注:复制该语句,直接在plsql的SQL窗口中执行即可完成创建;

使用:

select fgetpy('张三,李四,ww') from dual;

到此这篇关于oracle定义常用函数的文章就介绍到这了,更多相关oracle定义函数内容请搜索好代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好代码网!

标签: oracle 定义函数