oracle中的函数使用

阅读原文请点击: http://click.aliyun.com/m/23107/
摘要: 一函数的基本应用   1 创建函数(SQL窗口中) create or replace function get_hello_msg return varchar2 as begin        return 'hello world'; end get_hello_msg; 函数必须有返回值,该函数的返回值是varchar2类型。   2 在数据字典查看函数信息(S

一函数的基本应用



1 创建函数(SQL窗口中)

create or replace function get_hello_msg
return varchar2 as
begin
       return 'hello world';
end get_hello_msg;

函数必须有返回值,该函数的返回值是varchar2类型。



2 在数据字典查看函数信息(SQL窗口)

select object_name,object_type,status from user_objects where lower(object_name) = 'get_hello_msg'

注意看status这一栏,若显示VALID说明该函数可用;若显示INVALID则说明该函数不合法。

不可用的原因可能是语法错误,比如在创建函数时少了分号,记住每一个end后面都要有分号。



3 查看函数返回值(Command窗口)

set serverout on;
declare msg varchar2(20);
begin
msg:=get_hello_msg;
dbms_output.put_line(msg);
end;
/

其中set serverout on语句表示在窗口中显示服务器输出信息。




二带参数的函数


1 创建函数(SQL窗口)

create or replace function get_stu_grade(stu_grade number) return number as
begin
       declare standard_grade number;
       begin
               standard_grade:=stu_grade - 60;
               if standard_grade < 0 then
                  return 0;
               end if;
               return 1;
       end;
end get_stu_grade;


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





三函数的确定性

create or replace function get_stu_grade(stu_grade number) return number
deterministic as
begin
       declare standard_grade number;
       begin
               standard_grade:=stu_grade - 60;
               if standard_grade <=0 then
                  return 0;
               end if;
               return 1;
       end;
end get_stu_grade;
阅读原文请点击: http://click.aliyun.com/m/23107/

猜你喜欢

转载自1369049491.iteye.com/blog/2379163