ITPub博客

首页 > 数据库 > Oracle > oracle函数初次尝试

oracle函数初次尝试

Oracle 作者:shoujdX1 时间:2016-11-25 04:27:57 0 删除 编辑
        初次着手写function,确实费了好长时间,一个if就需要有个end  if ,并且else必须和if一起使用,可以直接对传进来的参数使用oracle自带的函数进行处理,并直接做做判断条件。
需求根据如下java代码,创建一个函数来实现相同的功能:
public static int processCompanyName(String company_name){
        String pre_log_n = pre_log+"processCompanyName(String company_name)";
        System.out.println( pre_log_n+"::company_name="+company_name );
        if( company_name==null )
            return 99;
        if( "".equals(company_name.trim()) )
            return 99;
        if( company_name.startsWith("[体验]") )
            return 2;
        else if( company_name.startsWith("[微信]") )
            return 3;
        else if( company_name.startsWith("[体验移动") )
            return 4;
        else if( company_name.startsWith("[采招圈") )
            return 5;
        else if( company_name.startsWith("[cn]") )
            return 6;
        else if( company_name.startsWith("[快速]") )
            return 7;
        else if( company_name.startsWith("[风向标") )
            return 8;
        else
            return 1;
    }
通过上面代码可知,需要通过判断company_name(可以理解为一个参数)开头的几个字,来输出特定的阿拉伯数字。


首先oracle函数的创建语句格式:


CREATE OR REPLACE FUNCTION 函数名 
RETURN 返回值类型 
IS 
    声明部分; 
BEGIN 
    函数体; 
  RETURN 变量; 
END;


下面展示我写的案例:
形式1:声明一个参数(category_2),然后把传进来的数值(category)赋给该参数,然后通过判断参数category_2,来得到结果。
create or replace function get_source_id1
( category    in varchar2)
return  number
is
category_2   VARCHAR2(50);


category_source    number(10);
begin
category_2:=category;


if  substr(category_2,1,4)='[采招圈' then
category_source:=5;
END IF;
if substr(category_2,1,4)='[风向标' then
category_source:=8;
END IF;
if substr(category_2,1,5)='[体验移动' then
category_source:=4;
END IF;
if substr(category_2,1,4)='[cn]' then
category_source:=6;
END IF;
if substr(category_2,1,4)='[体验]' then
category_source:=2;
END IF;
if substr(category_2,1,4)='[微信]' then
category_source:=3;
END IF;
if substr(category_2,1,4)='[快速]' then
category_source:=7;
END IF;
if category_2 is null then 
category_source:=99;
END IF;
if substr(category_2,1,4) not in ('[快速]','[微信]','[体验]','[cn]','[风向标','[采招圈') and substr(category_2,1,5)!='[体验移动' 
then 
category_source:=1;
END IF;
RETURN category_source;
end ;




形式2:直接判断传进来的参数。
create or replace function get_source_id
( category    in varchar2)
return  number
is
category_source    number(10);
begin


if  substr(category,1,4)='[采招圈' then
category_source:=5;
END IF;
if substr(category,1,4)='[风向标' then
category_source:=8;
END IF;
if substr(category,1,5)='[体验移动' then
category_source:=4;
END IF;
if substr(category,1,4)='[cn]' then
category_source:=6;
END IF;
if substr(category,1,4)='[体验]' then
category_source:=2;
END IF;
if substr(category,1,4)='[微信]' then
category_source:=3;
END IF;
if substr(category,1,4)='[快速]' then
category_source:=7;
END IF;
if category is null then
category_source:=99;
END IF;
if substr(category,1,4) not in ('[快速]','[微信]','[体验]','[cn]','[风向标','[采招圈') and substr(category,1,5)!='[体验移动'
then
category_source:=1;
END IF;
RETURN category_source;
end ;


形式3:也可以定义两个参数,用来接收传进来的参数,通过判断这两个参数,来得到结果。
create or replace function get_source_id
( category    in varchar2)
return  number
is
category_5   VARCHAR2(50);
category_4   VARCHAR2(50);
category_source    number(10);
begin
category_5:=substr(category,1,5);
category_4:=substr(category,1,4);
if category_4='[采招圈' then
category_source:=5;
END IF;
if category_4='[风向标' then
category_source:=8;
END IF;
if category is null then
category_source:=99;
end if ;
if category_4='[cn]' then
category_source:=6;
END IF;
if category_4='[体验]' then
category_source:=2;
END IF;
if category_4='[微信]' then
category_source:=3;
END IF;
if category_4='[快速]' then
category_source:=7;
end if;
if category_5='[体验移动' then
category_source:=4;
end if;
if category_4 not in ('[快速]','[微信]','[体验]','[cn]','[风向标','[采招圈') and category_5 !='[体验移动]'then
category_source:=1;
end if;
return category_source;
end;




对于oracle 函数的初次体会:
1.在我的例子中category这个是定义个输入值,它是一个一个去传进来的。
2.oracle 函数区别去存储过程,前者必须有一个返回值,存储过程非必需。
3.oracle函数中没出现一个if,就需要有个end if和他匹配。
4.对于无参函数的定义和调用都没有圆括号,但无参存储过程需要,例如我的category    in varchar2   ,而不是category    in varchar2(10)。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29266954/viewspace-2129035/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2014-11-09

  • 博文量
    16
  • 访问量
    14633