今天爱分享给大家带来PLSQL编程-函数【详细讲解】,希望能够帮助到各位。
一、含义
函数实际上是封装在服务器上一段PLSQL代码片断,它已经编译好了,如果客户端调用存储过程,执行效率就会非常高效,它跟存储过程没有什么本质区别,存储过程能做的函数也能做,只不过函数有返回值
二、语法
1、创建函数
create function 函数名称(参数名 in|out 参数类型,参数名 in|out 参数类型,...) return 返回的参数类型 is|as --声明部分 begin --业务逻辑 end;
2、修改函数
create [or replace] function 函数名称(参数名 in|out 参数类型,参数名 in|out 参数类型,...) return 返回的参数类型 is|as --声明部分 begin --业务逻辑 end;
3、删除函数
drop function 函数名称;
4、调用函数
--方式一: select 函数名称(...) from dual; --方式二: declare 变量名 变量类型; begin 变量名 = 函数名称(...); end;
三、演示
1、创建函数
--查询指定员工的年薪
/*
参数 : 员工的编号
返回 : 员工的年薪
*/
create function func_getsal(vempno number) return number
is
vtotalsal number;
begin
select sal * 12 + nvl(comm, 0) into vtotalsal from emp where empno = vempno;
return vtotalsal;
end;
--查询员工编号为7788的年薪
declare
vsal number;
begin
vsal := func_getsal(7788);
dbms_output.put_line(vsal);
end;
2、修改函数
--查询指定员工的年薪
/*
参数 : 员工的编号
返回 : 员工的年薪
*/
create or replace function func_getsal(vempno number) return number
is
vtotalsal number;
begin
select sal * 12 + nvl(comm, 0) into vtotalsal from emp where empno = vempno;
return vtotalsal;
end;
--查询员工编号为7788的年薪
declare
vsal number;
begin
vsal := func_getsal(7788);
dbms_output.put_line(vsal);
end;
3、删除函数
drop function func_getsal;
原文链接:https://blog.itblood.com/223.html,转载请注明出处。

![我的新生活:重置0.96 [神作RPG/AI汉化/动态]](/wp-content/uploads/replace/2025/07/18/791b368191d45396167bbcebb5412ae0.webp)