加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

Oracle————存储过程与函数

发布时间:2021-02-26 15:46:32 所属栏目:站长百科 来源:网络整理
导读:存储进程 存储进程参数模式包罗IN、OUT、 IN OUT。 IN(默认参数模式):暗示当存储进程别挪用时,实参值被转达给形参;形参起变量浸染,只能读该参数,而不能修改该参数。IN模式参数可所以变量或表达式。 OUT:暗示当存储进程被挪用时,实参值被忽略;形参起

存储进程
存储进程参数模式包罗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) isbegin 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,theMessage);--输入员工id dbms_output.put_line(‘ID为:‘||theId||‘的员工,名字为‘||theName ||‘,收入为‘||theSalary||‘,‘||theMessage); end; ---------------------?

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读