1、====为什么使用存储过程====:
存储过程在数据库开发的过程中使用比较频繁,它也有着普通sql语句不可替代的作用。存储过程是预编译过的,经优化后存储在sql内存中,使用时无需再次编译,提高了使用效率,存储过程的代码直接存放在数据库中,一般直接通过存储过程的名称调用,减少了网络流量,加快了系统执行效率。同时也提高系统安全性 – 防止SQL注入 (执行存储过程的用户要具有一定的权限才能使用存储过程)。
2、====存储过程的定义====:
create [or replace] procedure 存储过程名 [(输入==>in/out==> 输出参数)]
is/as
begin
sentences;
[exception
sentences;]
end 存储过程名;
3、====无参的存储过程案例====
--创建一个无参的存储过程
create or replace procedure sayHelloworld
as
--说明部分
begin
dbms_output.put_line('helloword');
end;
--使用execute执行存储过程
execute sayHelloworld;
4、====有参的存储过程案例====
create or replace procedure reiseSalry(t_id in teachers.teacher_id%type)
as
t_wage teachers.wage%type;
begin
--得到员工涨前的薪水
select wage into t_wage from teachers where teacher_id=t_id;
--给员工涨工资
update teachers set wage=wage+1000 where teacher_id=t_id;
dbms_output.put_line('涨前薪水'||t_wage||'涨后薪水'||(t_wage+1000));
end;
--执行
begin
reiseSalry(10101);
commit;
dbms_output.put_line('操作已经完成');
end;