oracle的存储过程和存储函数
发布日期:2021-05-08 13:40:49 浏览次数:22 分类:精选文章

本文共 1070 字,大约阅读时间需要 3 分钟。

存储过程

存储过程实际上是封装在服务器上的一段PLSQL代码片段(已经编译好了的代码)。用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。

在这里插入图片描述
创建一个给某个员工加特定工钱的存储过程:

-- empid是用户输入的指定的员工id-- insal是用户输入的指定的涨薪多少-- currentsal是员工涨薪钱的目前工资create or replace procedure cc1(empid in number,insal in number)is   currentsal number;begin  select sal into currentsal from emp1 where id=empid;  dbms_output.put_line('his sal before is '||currentsal||'$');  update emp1 set sal=sal+insal where id=empid;  dbms_output.put_line('his sal after is '||(insal+currentsal)||'$');  commit;end;

在这里插入图片描述

存储过程的调用

-- 第一种call  存储过程名(参数1, 参数2···);-- 第二种declarebegin	存储过程名(参数1, 参数2···);end;

在这里插入图片描述

call cc1(1005,3000);

在这里插入图片描述

存储函数

存储函数和存储过程基本都差不多,只是存储函数有返回值,存储过程没有。

在这里插入图片描述

存储函数的调用

declare  变量1   类型;begin   变量1:=存储函数名(参数1,参数2···);   dbms_output.put_line(变量1);end;

范例:

在这里插入图片描述
创建一个查询某个员工的年薪的存储函数:

-- 输入参数的in标识可以省略create or replace function emptotalsal(empid number) return numberis   fullyearsal number;begin  select sal*12+nvl(bonus,0) into fullyearsal from emp1 where id=empid;  return fullyearsal;end;
-- 调用存储函数emptotalsaldeclare  e number;begin   e:=emptotalsal(1007);   dbms_output.put_line(e);end;

在这里插入图片描述

上一篇:sqlplus的基本使用
下一篇:oracle中的游标

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年03月24日 09时10分17秒