admin管理员组文章数量:1794759
oracle中自定义函数如何使用,oracle自定义函数语法及使用
1、自定义函数语法
create [or replace] function 函数名 -- 定义一个名为xxx的函数
(
parameterName1, mode1 dataType1,
parameterName2, mode2 dataType2,
...
)
return返回值类型 --表示函数的返回类型为char或number等类型
is-- 表示函数体部分,关键字用is或as都可以
声明部分;
begin-- 表示函数开始
函数体;
return变量; -- 表示函数返回值
end; -- 表示函数结束
参数含义:
参数
含义
值
参数值含义
parameterName1
表示参数名称
dataType
表示参数的数据类型
char、NUMBER、nvarchar2.....
mode1、mode2
表示参数类型
in、out、in out
1、IN模式:只读。在模块里面,实参的数值只能被引用,而这个参数不能被改变。
2、OUT模式:只写。模块能够给这个参数进行赋值,但是这个参数的数值不能被引用。
3、IN OUT模式:可读写。这个模块的数值在模块内可被引用或改变。
2、自定义函数使用
select 自定义函数名(参数) from 表名;
3、示例
示例1:
create or replace function explainParameter
(
inParam in char,
outParam out char,
inAndOutParam in out char
)
return char
as
returnChar char;
num number:=0;
begin
inParam := 'Hello World'; -- 这是错误的,in类型的参数只能用来传值,不能赋值
outParam := 'Hello World'; -- 这是正确的,out类型的参数可以用来被赋值
inAndOutParam := 'Hello World'; -- 这是正确的,in out参数既可以用来传值,又可以被赋值
inAndOutParam := outParam;-- 这是错误的,out参数不能用来传值
returnreturnChar;
end;
调用:select explainParameter ('a','b','c') from dual;
示例二:根据表名查询该表最大主键值,vseq序列表存其他业务表refcode主键最大值。
create or replace
function f_getMaxRefcode(tableName in vnarchar2)
returnnumber
is
nextval number;
num number:=0;
begin
select count(*) into num from ( select refcode from vseq where tablename=tableName );
if(num>1) then
update vseq set maxrefcode=maxrefcode+1 where tablename=tableName;
commit;
end if;
if(num=0) then
insert into vseq(tablename,maxrefcode) values(tableName,1);
commit;
end if;
select maxrefcode into nextval from vseq where tablename=tableName;
returnnextval;
end;
版权声明:本文标题:oracle中自定义函数如何使用,oracle自定义函数语法及使用 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686497839a74169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论