过程的创建
过程可分为 : 1.有参过程 2. 无参过程
有参过程可分为: 1. 输入过程(in)、2. 输出过程(out)、3. 输入输出过程(in out)
下面解释一下 in out(in out 是用来传参时使用的哦!!)
首先说一个小知识,就是过程是没有返回值的,所以想要向外传输数据的时候怎么办呢?
为了解决这个问题,就出现了in out,
(1)in(该参数一般带有初始值)是表明此个参数仅仅是本次过程使用,不需要传出去,也就是该参数在过程中参加的运算在过程结束后不需要进行存储。
(2)out (该参数一般没有初始值)是表明此个参数是要传出去的,所以在此过程中的运算在过程结束后值仍然存储着。
(3)in out 就不需要多说,这个是集合了两者的特点。
主要格式为:
create procedure 过程名 【(参数)/空白(这两种情况是在该过程是否需要传入参数来决定的哦!)】as/is
变量声明部分
begin
想要执行的代码块。
exception
要捕获的异常
end 过程名;
大家可以看出,这个过程和普通的PL/SQL编程块非常相似,对的,两者确实非常相似
下面说说两者的区别:
1. 过程其实就是一个带有名字的PL/SQL编程块
2. 过程可以被存储,而PL/SQL编程块不行
3. 过程是一次编写可多次运行,而PL/SQL编程块是一次编写一次运行(这个是运行后关闭数据库后重新开启数据库时会出现刚才所讲,也就是存储的关系)
4. 过程更加适合大量数据的运行,尤其是咱们学过函数后,(这就像同是usb 3.0的硬盘和U盘,对于少量的数据来说U盘的小型,便捷得天独厚,而对于大量数据来说,硬盘的优点就凸显出来了,毕竟硬盘更加具有性价比嘛!!!)
下面是一些sql语句演示:
--无参的`过程块
create or replace procedure proc_1
as
begin
dbms_output.put_line('欢迎您'||USER);
dbms_output.put_line('现在的时间为:'||to_char(sysdate,'YYYY-MM-DD hh:mm:ss'));
end proc_1;
execute proc_1;
--有参的in过程块(1)练习
create or replace procedure add_dept(deptno dept.deptno%type,dname dept.dname%type,loc dept.loc%type)
is
begin
insert into dept values(deptno,dname,loc);
exception
when dup_val_on_index then
dbms_output.put_line('主键冲突,请重新选取主键');
end add_dept;
execute add_dept(12,'beyounce','zaozhuang');
--有参的in过程块(2)练习
create or replace procedure show_emp_higher(v_deptno dept.deptno%type)
is
v_avgsal emp.sal%type;
begin
select avg(sal) into v_avgsal from emp where deptno=v_deptno;
dbms_output.put_line('平均工资为:'||v_avgsal);
for v_emp in (select * from emp where sal>v_avgsal and deptno=v_deptno) loop
dbms_output.put_line('部门号为:'||v_emp.empno||' 员工号为:'||v_emp.ename);
end loop;
exception
when no_data_found then
dbms_output.put_line('部门不存在!!!');
end show_emp_higher;
execute show_emp_higher(10);
begin
show_emp_higher(10);
end;
--带有参数的out过程块(1)演示
create or replace procedure get_dept(p_deptno dept.deptno%type,p_dname out dept.dname%type,p_loc out dept.loc%type)
is
begin
select dname,loc into p_dname,p_loc from dept where deptno=p_deptno;
exception
when no_data_found then
dbms_output.put_line('部门不存在!!!');
end get_dept;
declare
v_deptno dept.deptno%type:=&deptno;
v_dname dept.dname%type;
v_loc dept.loc%type;
begin
get_dept(v_deptno,v_dname,v_loc);
dbms_output.put_line('部门名为:'||v_dname);
dbms_output.put_line('部门编号为:'||v_loc);
end;
--带有参数的out过程块(2)演示
create or replace procedure query_emp(e_empno emp.empno%type,e_name out emp.ename%type,e_sal out emp.sal%type)
is
emp_sal_error exception;
begin
select ename,sal into e_name,e_sal from emp where empno=e_empno;
if e_sal>=2500 then
dbms_output.put_line('操作成功');
else
raise emp_sal_error;
end if;
exception
when no_data_found then
dbms_output.put_line('部门不存在!!!');
when emp_sal_error then
dbms_output.put_line('工资低于2500,信息保密!!!');
end query_emp;
declare
v_empno emp.empno%type:=&empno;
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
query_emp(v_empno,v_ename,v_sal);
dbms_output.put_line('员工名为:'||v_ename);
dbms_output.put_line('工资为:'||v_sal);
end;
--带有参数的in out过程块演示
create or replace procedure number_complax(num1 in out number,num2 in out number )
is
begin
num1:=num1+num2;
num2:=num1-num2;
end number_complax;
declare
n1 number:=&n1;
n2 number:=&n2;
begin
number_complax(n1,n2);
dbms_output.put_line('两数之和为:'||n1);
dbms_output.put_line('两数之差为:'||n2);
end;