Oracle存储过程、存储函数

2026-04-17 07:46:46

1、存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的 SQL 语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。存储过程是数据库中的一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程。

Oracle存储过程、存储函数

2、创建存储过程语法:create [or replace] PROCEDURE 过程名[(参数名 in/out 数据类型)] AS begin PLSQL 子程序体;

End;

或者

create [or replace] PROCEDURE 过程名[(参数名 in/out 数据类型)] is

begin

PLSQL 子程序体;

End 过程名;

Oracle存储过程、存储函数

3、范例:创建一个输出 helloword 的存储过程

create or replace procedure helloworld is

begin

dbms_output.put_line('helloworld');

end helloworld;

调用存储过程

在 plsql 中调用存储过程

begin

-- Call the procedure   helloworld;

end;

Oracle存储过程、存储函数

1、存储函数语法:create or replace function 函数名(Name in type, Name in type, ...) return 数据类型 is 结果变量 数据类型;begin  return(结果变量);end 函数名;

Oracle存储过程、存储函数

2、范例:使用存储函数来查询指定员工的年薪

create or replace function empincome(eno in  emp.empno%type) return numberispsal emp.sal%type;pcomm emp.comm%type;begin  select t.sal,t.comm into psal,pcomm from emp t where t.empno=eno;  return psal*12+nvl(pcomm,0);end;

Oracle存储过程、存储函数

1、一般来讲,过程和函数的区别在于函数可以有一个返回值;而过程没有返回值。但过程和函数都可以通过 out 指定一个或多个输出参数。我们可以利用 out 参数,在过程和函数中实现返回多个值。

Oracle存储过程、存储函数

猜你喜欢