zoukankan      html  css  js  c++  java
  • plsql储存过程

    第九章  存储过程

     

    初识存储过程

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

      包含一系列PL/SQL语句的集合。

     

    创建存储格式

    CREATE [OR REPLACE] PROCEDURE procedure_name

      (argument1 [mode1] datatype1,

       argument2 [mode2] datatype2, ...)

    AS [IS]

       声明部分

    BEGIN

       执行部分

    EXCEPTION

       异常处理部分

    END;

    调用存储过程格式

    call proc_update_emp();

     

    示例:

    create or replace procedure pro_stu_update

    as

    begin

      update student set stu_name='edit_name' where stu_id=5;

    end;

    -- 调用

    call pro_stu_update();

     

    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(198);

     

    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;

     

     

    inout示例

    create or replace procedure

           -- in/out 参数共用,必须保持参数类型相同

           pro_emp_selectArray(v_param  in out number)

    as

    begin

      select manager_id into v_param from employees where employee_id=v_param;

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

    end;

    -- PL/SQL调用

    declare

       v_param number := '&input_param';

    begin

      pro_emp_selectArray(v_param);

    exception

      when no_data_found then

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

    end;

     

    多参数传递 示例

    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);

     

     

  • 相关阅读:
    Tomcat 结合Atomikos 实现JTA
    [译]Kinect for Windows SDK开发入门(十一):手势识别 下:基本手势识别
    [译]Kinect for Windows SDK开发入门(十二):语音识别 上
    [译]Kinect for Windows SDK开发入门(十):手势识别 上:基本概念
    [译]Kinect for Windows SDK开发入门(十五):进阶指引 下
    [译]Kinect for Windows SDK开发入门(九):骨骼追踪进阶 下
    [译]Kinect for Windows SDK开发入门(十四):进阶指引 上
    [译]Kinect for Windows SDK开发入门(八):骨骼追踪进阶 上
    [译]Kinect for Windows SDK开发入门(十三):语音识别 下
    Codeforces Round #757 (Div. 2) A D
  • 原文地址:https://www.cnblogs.com/h-w-b/p/12488184.html
Copyright © 2011-2022 走看看