Oracle————存储过程与函数
存储过程
存储过程参数模式包括IN、OUT、 IN OUT。
IN(默认参数模式):表示当存储过程别调用时,实参值被传递给形参;形参起变量作用,只能读该参数,而不能修改该参数。IN模式参数可以是变量或表达式。
OUT:表示当存储过程被调用时,实参值被忽略;形参起未初始化的PL/SQL变量的作用,形参的初始值为NULL,可以进行读/写操作,在存储过程调用结束后,形参值被给实参。OUT模式参数只能是变量,不能是常量或表达式。
IN OUT表示当存储过程被调用时,形参值被传递给形参。形参起已初始化的PL/SQL变量的作用,可读可写。IN OUT 模式参数只能是变量,不能是常量或表达式。
使用OUT、IN OUT模式参数时只有当程序正常结束时形参值才会传递给实参。
举例:
create or replace procedure proc_divide
(num1 in out number,num2 in out number) is
r1 number;
r2 number;
begin
r1:=trunc(num1/num2);
r2:=mod(num1,num2);
num1 := r1;
num2 := r2;
exception
when zero_divide then
dbms_output.put_line('除法中分母不能为零');
when others then
dbms_output.put_line('程序运行错误!请使用游标');
end proc_divide;
set serveroutput on
declare
n1 number:=&n1;
n2 number:=&n2;
begin
proc_divide(n1,n2);
dbms_output.put_line('两个数字相除的结果是:'||n1);
dbms_output.put_line('取余的结果是:'||n2);
end;
函数
(1)函数的创建与存储过程的创建相似,不同之处在于,函数有一个显示的返回值。
(2)在函数的创建过程中没有declare关键字,而是使用is或者as关键字来代替。
(3)函数必须有返回值:return datatype。
(4)一般不在函数中执行 DML(数据操纵语言-插入、删除、更新)操作。
举例:存储过程调用函数
--函数 increaseSalary()
create or replace function increaseSalary(theIncome in number) return varchar2
as
theMessage varchar2(50);
begin
if theIncome <=4000 then
theMessage := '收入太低,工资增加10%';
elsif theIncome <=8000 then
theMessage := '收入偏低,工资增加5%';
elsif theIncome <=20000 then
theMessage := '收入一般,工资增加2%';
else
theMessage := '收入很高,工资增加1%';
end if;
return theMessage;
end;
--存储过程
create or replace procedure getEmpInfo
(theId in out employees.employee_id%type,
theName out employees.first_name%type,
theSalary out employees.salary%type,
theMessage out varchar2)
is
begin
select employee_id,first_name,salary,increaseSalary(salary)
into theId,theName,theSalary,theMessage
from employees
where employee_id = theId;
exception
when NO_DATA_FOUND then
dbms_output.put_line('没有该员工信息');
end;
set serveroutput on ;
declare
theId employees.employee_id%type:=&theId;
theName employees.first_name%type;
theSalary employees.salary%type;
theMessage varchar2(50);
begin
getEmpInfo(&theId,theName,theSalary,theMessage);--输入员工id
dbms_output.put_line('ID为:'||theId||'的员工,名字为'||theName
||', 收入为'||theSalary||','||theMessage);
end;
---------------------
相关文章
- Oracle表里 同个人有五条数据 ,取出id值最大的那条数据:
- jdbctemplate 调用oracle 有返回(会话型临时表数据的)结果的存储过程
- 数组做为参数传入Oracle存储过程操作数据库
- oracle怎么查存在的dblink
- oracle大数据量更新引发的死锁问题解决方法及oracle分区和存储过程的思考
- PL/SQL如何调试Oracle存储过程
- .Net Core 调用Oracle 存储过程
- Oracle 数据库分页查询与排序分页查询
- 使用smem 计算Oracle用户下内存占用情况.
- [转载]以及部分总结--Linux下创建单机ASM存储的Oracle实例的过程---感谢方总
- Oracle存储过程的异常处理
- oracle安装
- Oracle函数/存储过程返回结果集(游标)
- Oracle中执行存储过程call和exec区别
- ORACLE存储过程,循环语法和游标
- Oracle中如何导出存储过程、函数、包和触发器的定义语句?如何导出表的结构?如何导出索引的创建语句?
- Oracle存储过程update受外键约束的主键值时完整性冲突解决方式
- ORACLE忘记用户名和密码、解锁、查询
- Oracle 表空间的日常维护与管理
- Linux下Oracle client(sqlplus)安装和配置
- 关于oracle pfile和spfile文件说明
- Oracle的存储过程、函数与触发器(代码版)
- oracle 物化视图详解
- oracle 的自定义的存储函数遇到的 package or function is in an invalid state
- 一些应该使用mongodb或者其他文档存储而不是redis或mysql、oracle json的情形(最近更新场景)
- oracle存储过程中is和as区别
- Oracle:Authid Current_User的使用
- oracle的sqlldr并行导入表不要加索引
- oracle记录错误存储过程
- ORACLE 存储过程解及表解锁和停止执行
- Oracle备份的几种方式
- oracle启动
- 基于Oracle OCI的数据访问C语言接口ORADBI .