oracle 存储过程 的创建以及使用

版权声明:借鉴时注明出处就行 https://blog.csdn.net/weixin_42144379/article/details/84951661

1.概念

    存储过程:

实际上是封装在服务器上一段PLSQL代码片断,已经编译好了的代码
            客户端取调用存储过程,执行效率就会非常高效
    语法:
      create [or replace] procedure 存储过程的名称(参数名 in|out|in out 参数类型,参数名 in|out|in out 参数类型)
      is | as
       --声明部分
      begin
       --业务逻辑 
      end; 

  in、out、in out 参数使用方法请参考:

in、out、in out 参数的使用方法

2.调用方式

方式1

    call 存储过程的名称 (参数);

方式2 (用的最多的方式)

    declare

    begin
      存储过程的名称 (参数);
    end;

3.案例 :

使用oracle自带学习用户 SCOTT 里的 EMP 表 进行操作.

要求 :  创建一个储存过程,给指定的员工涨工资

-- 查看7788涨工资前的工资
select empno,sal from emp where empno = 7788;
-- 创建储存过程, 员工编号参数 p_empno,涨薪数 参数 p_sal
create or replace procedure pro_updatesal(p_empno number,p_sal number)
as
    -- 声明变量,用来 储存当前工资
    cu_sal number;
begin
  -- 把当前工资 赋值给变量
  select sal into cu_sal from emp where empno = p_empno;
  -- 更新
  update emp set sal = cu_sal+p_sal where empno = p_empno;
  -- 一般储存过程最后一句就是 提交
  commit;
end;
-- 调用储存过程,给 7788号员工 加 100 块
call pro_updatesal(7788,100);

-- 查询结果,验证是否成功
select empno,sal from emp where empno = 7788;

猜你喜欢

转载自blog.csdn.net/weixin_42144379/article/details/84951661