一函数的基本应用
<wbr></wbr>
1 创建函数(SQL窗口中)
create or replace function get_hello_msg
return varchar2 as
begin
<wbr><wbr><wbr><wbr><wbr><wbr> return 'hello world';<br> end get_hello_msg;</wbr></wbr></wbr></wbr></wbr></wbr>
函数必须有返回值,该函数的返回值是varchar2类型。
<wbr></wbr>
2 在数据字典查看函数信息(SQL窗口)
select object_name,object_type,status from user_objects where lower(object_name) = 'get_hello_msg'
注意看status这一栏,若显示VALID说明该函数可用;若显示INVALID则说明该函数不合法。
不可用的原因可能是语法错误,比如在创建函数时少了分号,记住每一个end后面都要有分号。
<wbr></wbr>
3 查看函数返回值(Command窗口)
set serverout on;
declare msg varchar2(20);
begin
<wbr>msg:=get_hello_msg;<br><wbr>dbms_output.put_line(msg);<br> end;<br> /</wbr></wbr>
其中set serverout on语句表示在窗口中显示服务器输出信息。
<wbr></wbr>
二带参数的函数
1 创建函数(SQL窗口)
create or replace function get_stu_grade(stu_grade number) return number as
begin
<wbr><wbr><wbr><wbr><wbr><wbr> declare standard_grade number;<br><wbr><wbr><wbr><wbr><wbr><wbr> begin<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> standard_grade:=stu_grade - 60;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> if standard_grade < 0 then<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> return 0;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> end if;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> return 1;<br><wbr><wbr><wbr><wbr><wbr><wbr> end;<br> end get_stu_grade;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
2 调用函数(Command窗口或SQL窗口)
select get_stu_grade(90) from dual; // 1
select get_stu_grade(60) from dual; // 1
select get_stu_grade(59) from dual; // 0
<wbr></wbr>
<wbr></wbr>
三函数的确定性
create or replace function get_stu_grade(stu_grade number) return number
deterministic as
begin
<wbr><wbr><wbr><wbr><wbr><wbr> declare standard_grade number;<br><wbr><wbr><wbr><wbr><wbr><wbr> begin<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> standard_grade:=stu_grade - 60;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> if standard_grade <=0 then<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> return 0;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> end if;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> return 1;<br><wbr><wbr><wbr><wbr><wbr><wbr> end;<br> end get_stu_grade;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
deterministic增加了函数的确定性。意思就是我们输入相同的一个分数,其返回的结果应该一致。如果第一次输入了一个90分,第二次再输入90分的时候返回值肯定与第一次一样,那么oracle就会直接拿到第一次的结果,不再重复执行该函数,提高的效率。什么时候不能用该关键字呢?比如该函数使用了系统时间而系统时间影响了返回值。那么每一次执行系统时间理论上是不一样的,所以不能直接拿上次的结果。