存储过程

1.  存储过程的定义

存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。存储过程是数据库中的一个重要对象。

2.  存储过程的语法结构

创建存储格式

CREATE [OR REPLACE] PROCEDURE procedure_name

  (argument1 [mode1] datatype1,

   argument2 [mode2] datatype2, ...)

AS [IS]

   声明部分

BEGIN

   执行部分

EXCEPTION

   异常处理部分

END;

调用存储过程格式

call proc_update_emp();

3. 存储过程in示例

in 示例

-- 根据员工号,查询员工工资

create or replace procedure

       -- in表示入参

       pro_emp_selectArray(v_empId in employees.employee_id%type)

as     v_sal employees.salary%type;

begin

  select salary into v_sal from employees where employee_id=v_empId;

  DBMS_OUTPUT.PUT_LINE('salary:' || v_sal);

end;

-- call调用

call pro_emp_selectArray(200);

4. 存储过程out示例

in/out 示例

-- 根据员工号,查询员工工资 带out参数

create or replace procedure

       -- in表示入参,out表示出参

       pro_emp_selectArray(v_empId in employees.employee_id%type,v_sal out employees.salary%type)

as

begin

  select salary into v_sal from employees where employee_id=v_empId;

  DBMS_OUTPUT.PUT_LINE('salary:' || v_sal);

end;

-- PL/SQL调用

declare

   -- 对应的参数类型和数量保持一致

   v_empId employees.employee_id%type := '&input_empId';

   v_sal employees.salary%type;

begin

  pro_emp_selectArray(v_empId,v_sal);

exception

  when no_data_found then

    DBMS_OUTPUT.PUT_LINE('找不到对应员工');  

end;

5.  存储过程多参数传递示例

多参数传递 示例

create or replace procedure

       pro_multi_params(param1 in number,param2 in number,param3 in number)

as v_sum  number;

begin

  v_sum := param1+param2+param3;

  DBMS_OUTPUT.PUT_LINE('v_sum:' || v_sum);

end;

-- call调用

call pro_multi_params(1,2,3);

原文地址:https://www.cnblogs.com/ZXDZXD/p/12488202.html