oracle中的函数使用

阅读原文请点击:[url]http://click.aliyun.com/m/23107/[/url]
摘要: 一函数的基本应用 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;
阅读原文请点击:[url]http://click.aliyun.com/m/23107/[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值