用户定义函数是存储在数据库中的代码块,可以把值返回到调用程序。调用时如同系统函数一样,如max(value)函数,其中,value被称为参数。函数参数有3种类型。
IN 参数类型:表示输入给函数的参数。
OUT 参数类型:表示参数在函数中被赋值,可以传给函数调用程序。
IN OUT参数类型:表示参数既可以传值也可以被赋值。
1、语法格式:
SQL语法方式创建的语法格式为:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
说明:
function_name::用户定义的函数名。函数名必须符合标示符的定义规则,对其所有者来说,该名在数据库中是唯一的。
parameter:用户定义的参数。用户可以定义一个或多个参数。
mode:参数类型。
datatype:用户定义参数的数据类型。
return_type::用户返回值的数据类型。
函数返回scalar_expression表达式的值,function_body函数体由pl/sql语句构成。
例:
create or replace function get_empname(v_id in number) return varchar2 as
v_name varchar2(50);
begin
select name into v_name from employee where id = v_id;
return v_name;
exception
when no_data_found then
raise_application_error(-20001, '你输入的ID无效!');
end get_empname;
v_name varchar2(50);
begin
select name into v_name from employee where id = v_id;
return v_name;
exception
when no_data_found then
raise_application_error(-20001, '你输入的ID无效!');
end get_empname;