存储过程的参数(三种)
一、IN:
>>in类型参数:默认的参数模式,用于接受调用程序的值
>>案例1:计算指定部门的工资总和 ,输入部门编号,并统计打印出来其中的职工数量,工资总和
create procedure emp_count_sumsal (dno number)
as
vdno number(10);
vcou number(10);
vsumsal number(10);
begin
select deptno,count(*),sum(sal) into vdno,vcou,vsumsal
from emp where deptno=dno group by deptno;
dbms_output.put_line('部门编号:'||vdno||' 部门人数:'||vcou||' 部门工资总和:'||vsumsal);
end;
//调用函数
begin
emp_count_sumsal(10); //给一个参数,看下部门编号是10的职工数量和工资总和
end;
>>展示输出结果
![1006d01f19c63df43ea7640086dbd3f9.png](https://i-blog.csdnimg.cn/blog_migrate/6f55e8f7268673666e161452ee8358ff.jpeg)
>>案例2:创建一个存储过程,以员工号为参数,修改该员工的工资。若该员工属于10号部门,则工资增加150;若属于20号部门,则工资增加200;若属于30号部门,则工资增加250;若属于其他部门,则增加300。
create procedure up_sal(eno number)
as
vdno number;
begin
select deptno into vdno from emp where empno=eno;
if vdno=10 then
update emp set sal=sal+150 where empno=eno;
elsif vdno=20 then
update emp set sal=sal+200 where empno=eno;
elsif vdno=30 then
update emp set sal=sal+250 where empno=eno;
else
update emp set sal=sal+300 where empno=eno;
end if;
end;
//调用
begin
up_sal(7788);
end;
//验证
select *from emp
二、OUT:
>>out类型参数:用于向调用程序返回值
>>案例:编写一个存储过程,根据输入的工作类型,输出该工作的平均工资。
create procedure emp_avgsal(j_ob in varchar2(20),avgsal out number)
as
avgs number(10);
begin
select avg(sal) into avgs from emp where job=j_ob;
avgsal:=avgs;
end;
//和in类型不同,因为有两个参数
declare
vsal number(10);
begin
emp_avgsal('CLERK',vsal);
dbms_output.put_line(vsal);
end;
>>展示输出结果
![e5d530326bd1a50ea42e46820cf4dc38.png](https://i-blog.csdnimg.cn/blog_migrate/a9c57587312670f4b1fee96eaa003fd0.png)
三、IN OUT:
>>in out类型参数:用于接受调用程序的值,并向调用程序返回更新的值
>>案例:交换两个参数的值
create procedure exchange(num1 in out number,num2 in out number)
as
temp number(10);
begin
temp:=num1;
num1:=num2;
num2:=temp;
end;
//调用,这个类型的调用和上面的两种都不同
declare
num1 number:=100;
num2 number:=200;
begin
dbms_output.put_line('交换前'||' 第一个数是:'||num1||' 第二个数是'||num2);
exchange(num1,num2); //调用刚刚定义的存储过程
dbms_output.put_line('交换后'||' 第一个数是:'||num1||' 第二个数是'||num2);
end;
>>展示输出结果
![11979e28c7c74ba5054b75e3b8243f9e.png](https://i-blog.csdnimg.cn/blog_migrate/61a60ac5159eae34b288b1d39a643731.jpeg)