Oracle带输入输出参数的存储过程

前端之家收集整理的这篇文章主要介绍了Oracle带输入输出参数的存储过程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文链接:http://blog.itpub.net/29485627/viewspace-1248721/

(一)使用输入参数
需求:在emp_copy中添加一条记录,empno为已有empno的最大值+1,ename不能为空且长度必须大于0,deptno为60。
创建存储过程:
create or replace procedure insert_emp(emp_name in varchar2,dept_no in number) as
begin
declare max_empno number;
begin
if(emp_name is null or length(emp_name) = 0) then
return;
end if;

if(dept_no != 60) then
select max(empno) into max_empno from emp_copy;
insert into emp_copy(empno,ename,deptno) values(max_empno + 1,emp_name,dept_no);
end;
end insert_emp;
/
Procedure created.
调用存储过程并验证:
(1)
sql>execute insert_emp('Li Si',60);
PL/sql procedure successfully completed.
sql>
col empno format 99999;
col ename format a15;
col deptno format 99999;
select empno,deptno from emp_copy where deptno = 60;
EMPNO ENAME DEPTNO
------ --------------- ------
7981 Li Si 60
(2)
sql> execute insert_emp('',6);
sql> select empno,deptno from emp_copy where deptno = 6;
(二)使用输出参数
需求:在上个需求的基础上,要分别统计表emp_copy插入数据前后的记录数。
select count(1) into original_count from emp_copy;
select count(1) into current_count from emp_copy;
调用存储过程:
declare count1 number;
count2 number;
insert_emp('Wang Wu',60,count1,count2);
dbms_output.put_line('Original count of table emp_copy is ' || count1);
dbms_output.put_line('Current count of table emp_copy is ' || count2);
end;
Original count of table emp_copy is 15
Current count of table emp_copy is 16
(三)使用输入输出参数
in out参数综合了上述两种参数类型,既向过程体传值,也被赋值而传到过程体外。in out参数既可以用作输入也可以用作输出
需求:实现两数交换。
create or replace procedure swap(value1 in out number,value2 in out number) as
value1 := value1 + value2;
value2 := value1 - value2;
value1 := value1 - value2;
end swap;
declare a number := 22;
b number := 33;
dbms_output.put_line('Before swap: a = ' || a || ',b = ' || b);
swap(a,b);
dbms_output.put_line('After swap: a = ' || a || ',Arial; line-height:26px">Before swap: a = 22,b = 33
After swap: a = 33,b = 22
PL/sql procedure successfully completed.

原文链接:https://www.f2er.com/oracle/213080.html

猜你在找的Oracle相关文章