<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
--=============================
--author:yeeXun
--date:发表于 @ 2010年12月25日 10:54:00
--address:Jau 17-304
--==============================
Oracle函数,分为系统函数和自定义函数,这里介绍自定义函数。
函数用于返回特定数据,可以返回一个或多个值。在一个函数中必须包含一个或多个RETURN 语句,函数调用是 PL/SQL 表达式的一部分,而过程调用可以是一个独立的 PL/SQL 语句。
创建自定义函数的语法如下:
Create[orreplace]function函数名
[(参数 [{in|out|inout}] 数据类型 ,......)]
Return返回类型
[authid{current_user|designer}]
{is|as}
Begin
函数体
End函数名 ;
Orreplace:如果要创建的函数存在,则先删除此函数,再重建此函数,也就是将撤销和重建这两个步骤合为一步操作。
In|out|inout:参数的模式。
authidcurren_user :在调用时, oracle 使用调用该过程的用户权限域执行该过程,此时调用者必须有权限访问存储过程中所涉及到的所有数据库对象的权限。
authiddesigner :为默认选线, oracle 将使用过程所有者的权限来执行 .
下面是一个例子:
SQL>createfunctionleap_or_common_year
2(yearininteger)
3returnvarchar2is
4retvalvarchar2(30);
5begin
6if(yearmod400)=0or((yearmod100)!=0and(yearmod 4)=0)then
7retval:=year||'isaleapyear;';
8else
9retval:=year||'isacommonyear;';
10endif;
11returnretval;
12endleap_or_common_year;
接下来我们调用这个函数:
SQL>selectleap_or_common_year(2010)fromdual;
LEAP_OR_COMMON_YEAR(2010)
--------------------------------------------------------------------------------
2010isacommonyear;
SQL>selectleap_or_common_year(2012)fromdual;
LEAP_OR_COMMON_YEAR(2012)
--------------------------------------------------------------------------------
2012isaleapyear;
函数的删除:
Dropfunction函数名;
例如:
SQL>dropfunctionleap_or_common_year;
Functiondropped
--the end--