Oracle数据库——存储过程和自定义函数

一、存储过程和存储函数

指存储在数据库中供所有用户程序调用的子程序叫存储过程/存储函数。

存储过程和存储函数的相同点:完成特定功能的程序
存储过程和存储函数的区别:是否用return语句返回值

二、创建和使用存储过程

CREATE PROCEDURE命令建立存储过程和存储函数。
语法:
   create [or replace] PROCEDURE 过程名(参数列表) 
   AS
   PLSQL子程序体;

1、第一个存储过程

打印:Hello World

--第一个存储过程:打印Hello World
/*
调用存储过程:
1.exec sayhelloworld() ;
2.begin
	sayhelloworld() ;
	sayhelloworld() ;
  end;
*/
create or replace procedure sayhelloworld
as
--说明部分
begin
	dbms_output.put_line('Hello World');
end;
/

在这里插入图片描述
在这里插入图片描述

2、调用存储过程

使用exec语句

使用PL/SQL程序

3、带参数存储过程

举例:为指定的员工,涨100块钱的工资;并且打印涨之前和涨之后的薪水。

--创建一个带参数的存储过程
--给指定的员工涨100块钱的工资,并且打印涨前和涨后的薪水
create or replace procedure raisesalary (eno in number)
as
psal emp.sal%type;
begin
	--得到员工涨前的薪水
	select sal into psal from emp where empno = eno;
	--给该员工涨100
	update emp set sal = sal + 100 where empno = eno;
	--打印
	dbms_ output.put_ line('涨前: '||psal||' 涨后: '||(psal + 100));
end;
/

在这里插入图片描述

--调用存储过程
begin
	raisesalary( 7839) ;
	raisesalary( 7566) ;
	commit;
end;
/

在这里插入图片描述

4、如何调试存储过程

被阻断:
在这里插入图片描述
授权:
在这里插入图片描述

BEGIN
DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE
(
host => '192.168.128.1', 
lower_port => null,
upper_port => null,
ace => xs$ace_type(privilege_list => xs$name_list('HYHCJ'),
principal_name => 'c##scott',
principal_type => xs_acl.ptype_db)
);
END;

三、存储函数

  • 函数(Function)为一命名的存储程序,可带参数,并返回一计算机值。

  • 函数和过程的结构类似,但必须有一return子句,用于返回函数值。

1、创建存储函数的语法

create [or replace] FUNCTION 函数名(参数列表)
return 函数值类型
AS
PLSQL子程序体;

2、存储函数举例

查询某个员工的年收入

--存储函数:查询某个员工的年收入
create or replace function query empincome (eno in number)
return number
as
	--定义变量保存员工的薪水和奖金
	psal emp.sal%type;
	pcomm emp.comm%type;
begin
	--得到该员工的月薪和奖金
	select sal, comm into psal, pcomm from emp where empno = eno;
	--直接返回年收入
	return psal*12+nvl(pcomm,0);
end;
/

在这里插入图片描述

3、in 和 out参数

一般来讲,存储过程和存储函数的区别在于存储函数可以有一个返回值;而存储过程没有返回值。

过程和函数都可以通过out指定一个或多个输出参数。我们可以利用out参数,在过程和函数中实现返回多个值。

  • 存储过程和存储函数都可以有out参数
  • 存储过程和存储函数都可以有多个out参数
  • 存储过程可以通过out参数来实现返回值

四、什么时候用存储过程/存储函数?

  • 原则:
    如果只有一个返回值,用存储函数;否则,就用存储过程。
--out函数: 查询某个员工姓名月薪和职位
create or replace procedure queryempinform(eno in number,
											pename out varchar2,
											psal out number ,
											empjob out varchar2)
as
begin
	--得到该员工的姓名月薪和职位
	select ename, sal, empjob into pename, psal, emppjob from emp where empno = eno;
end;
/

在这里插入图片描述

1、在应用程序中访问存储过程和存储函数

  • 访问存储过程

  • 访问存储函数

参考:https://blog.csdn.net/hyh17808770899/article/details/106872076

2、在out参数中使用光标

  • 申明包结构
  • 包头
  • 包体

案例:查询某个部门中所有员工的所有信息

包头:

create or replace package mypackage as
	type empcursor is ref cursor;
	procedure queryEmpList (dno in number, empList out empcursor);
end mypackage;

在这里插入图片描述
包体:
包体需要实现包头中声明的所有方法

create or replace package body mypackage as
	procedure queryEmpList(dno in number, empList out empcursor) as
	begin
		open empList for select * from emp where deptno = dno;
	end queryEmpList;
end mypackage;

在这里插入图片描述

3、在应用中访问包中的存储过程

注意:需要带上包名

猜你喜欢

转载自blog.csdn.net/hyh17808770899/article/details/106867758