Oracle存储过程和自定义函数

存储 存储软件
存储过程和存储函数是指存储在数据库中供所有用户程序调用的子程序叫存储过程、存储函数。异同点:存储过程和存储函数的相同点:完成特定功能的程序。存储过程和存储函数的区别:是否用return语句返回值。

概述

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

异同点: 

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

存储过程的创建和调用

***个存储过程: 打印hello world

  1. createorreplace procedure sayhelloword 
  2. as 
  3.  --说明部分,as一定要写 
  4. begin 
  5.    dbms_output.put_line('Hello World'); 
  6. end

[[222712]]

调用存储过程

1.execsayhelloworld()

2.2

  1. -- 调用两次 
  2. begin 
  3.    sayhelloworld(); 
  4.    sayhelloworld(); 
  5. end

oracle 带参数的存储过程

  1. --创建一个带参数的存储过程 
  2. --给指定的员工涨100块钱的工资,并且打印涨前后涨后的薪水 
  3. createorreplace procedure raisesalary(enoinnumber)--in这是一个输入参数 
  4. as 
  5.  --定义一个变量保存涨前的薪水 
  6.    psal emp.sal%type; 
  7. begin 
  8.  --得到员工涨前的薪水 
  9.    selectsalintopsalfromempwhereempno=eno; 
  10.  --给该员工涨100块钱 
  11.    update emp setsal=sal+100whereempno=eno; 
  12.  --一般,这里不需要commit!也不需要rollback 
  13.  --注意:一般不在存储过程或者存储函数中,commit和rollback 
  14.  --打印 
  15.    dbms_output.put_line('涨前:'||psal||',涨后:'||(psal+100)); 
  16. end
  17. --调用: 
  18. begin 
  19.   raisesalary(7839); 
  20.   raisesalary(7566); 
  21. end

如何调试存储过程

  1. 调试存储过程***放到Oracle数据库所在的系统或虚拟机上,解压SQL developer ,双击运行。
  2. 为了确保存储过程或函数是可调试的,右键“以编译并进行调试”,点击红色按钮“调试”
  3. 利用已写好的调用函数进行调试。
  4. 给调试账户授权
  1. grant DEBUG CONNECT SESSION ,DEBUG ANY PROCEDURE to scott; 

存储函数

函数的定义

  是一个命名的存储程序,可带参数,并返回一个计算值。必须有return 子句,用于返回函数值。

创建存储函数语法

  1.  create or replace function 函数名(参数列表) 
  2.   return 函数值类型 
  3.   as 
  4.   begin 
  5.     PLSQL子程序体; 
  6.   end
  7. ​ 

注意 表达式中某个字段为空时,表达式返回值为空。为防止含有表达式的返回值错误,在可能为空的字段上加上NVL(字段名,0)。

--查询某个员工的年收入

  1. --查询某个员工的年收入 
  2. create or replace function queryemp_income(eno in number) 
  3. return number 
  4. as  
  5.     --定义变量接收薪水和奖金 
  6.     p_sal emp.sal%type; 
  7.     p_comm emp.comm%type; 
  8. begin 
  9.   select sal,comm into p_sal,p_comm from emp where empno=eno; 
  10.   --nvl为遇空函数,如果p_comm为空则返回0 
  11.   return nvl(p_comm,0)+p_sal*12; 
  12. end
  13. ​ 

out 参数

存储过程和存储函数都可以有多个输入(in)和输出参数(out),都可以通过out参数实现返回多个值。

  1. -- out参数:查询某个员工姓名、月薪和职位 
  2. -- 原则: 如果只有一个返回值,用存储函数;否则,就用存储过程。 
  3. create or replace procedure queryempinfor(eno in number,pename out varchar2, 
  4.   psal out number,pjob out varchar2) 
  5. as  
  6. begin 
  7.    -- 得到该员工的姓名、月薪和职位 
  8.     select ename,sal,empjob into pename,psal,pjob from emp where empno=eno; 
  9. end
  10. ​ 

在 out 参数中访问光标

申明包结构

  • 包头(申明)
  • 包体(实现)

案例

  1. -- 查询某个部门中所有员工的所有信息  //ref(reference引用) cursor(光标) 
  2. #包头 
  3. create or replace package mypackage as 
  4. type empcursor is ref cursor
  5. procedure queryEmpList(dno in number,empList out empcursor); 
  6. end mypackage; 
  7. #包体 
  8. create or replace package body mypackage as 
  9. procedure queryEmpList(dno in number,empList out empcursor) as 
  10.  begin 
  11.    open empList for select * from emp where deptno=dno; 
  12.  end queryEmpList; 
  13. end mypackage; 
  14. ***********包体需要实现包头中声明的所有方法********************* 
  15. ​ 

在应用程序中访问

在java应用程序中访问存储过程和存储函数以及访问包下的存储过程,可以查看java API文档。

 

责任编辑:武晓燕 来源: 序猿不姓程
相关推荐

2010-09-06 15:41:34

SQL函数存储过程

2010-10-25 16:05:07

oracle自定义函数

2011-05-17 13:32:04

oracle

2010-04-28 12:33:36

Oracle自定义函数

2010-09-14 16:47:23

SQL自定义函数

2009-07-06 16:20:50

JSP自定义标签

2010-09-14 16:59:39

SQL自定义函数

2010-05-11 13:16:21

Unix awk

2017-03-16 14:37:05

LinuxShell函数

2015-02-12 15:33:43

微信SDK

2010-10-25 13:48:26

Oracle过程

2011-07-04 14:08:02

C++

2021-07-15 16:41:21

Swift查询函数

2009-08-12 14:53:50

C#类型转换函数

2023-10-31 09:10:39

2023-02-28 11:29:09

存储函数MySQL

2015-02-12 15:38:26

微信SDK

2023-06-28 08:05:46

场景vue3自定义

2015-03-26 11:51:22

2009-11-25 14:36:39

PHP函数usort(
点赞
收藏

51CTO技术栈公众号