zoukankan      html  css  js  c++  java
  • PL/SQL Procedure

    Every PL/SQL construct is composed of one or more blocks . These blocks can be entirely separate or nested within one another .

    Subprogram :

    • Is a named PL/SQL block that can accept parameters and be invoked from a calling environment
    • Is of two types :
      • A procedure that performs an action
      • A function taht computes a value
    • Is based on standard PL/SQL block structure ( 多个 PL/SQL 块组成 )
    • Contains a declarative section, an executable section, and an optional exception-handling section
    • Can be compiled and stored in the database.

    image

    image

    image

    mode : 解释,IN( default ) OUT, IN OUT

    in : --> ( 只是将参数传给 procedure ) 默认类型

    out : <-- ( 只是返回对应参数 , 并不将参数传给 procedure )

    in out : <--> ( 既传进参数 , 又传出参数 )

    You cannot reference host or bind variables in the PL/SQL block of a stored procedure. ( 不可以引用环境和绑定变量)

    You can not restrict the size of the data type in the parameters.( 不用指定参数尺寸,例如 NUMBER )

    形式参数,即 procedure 的参数,最好使用 p_ , 前缀

    execute procedure ( argument ) ;  // 执行 ( 如果是在别的 procedure 中 调用 另一个procedure, 直接 名字+ 参数就可以了 , raise_salary( 176 );

    image

    image

    如果参数是,OUT 模式,就必须要使用变量传参进去,因为需要接收从 procedure 中的返回值。

    参数传递

    image

    1. 普通,参数的顺序和个数完全一致。

    2. => , 指定参数对应关系。

    3. 结合 1,2

    个人感觉 : 要么1 , 要么2. ( 还是 2 更好一点,因为如果要是有 default 等,那么顺序可能就有变化 )

    DEFAULT

    image

    You can initialize IN parameters to default values. ( 只能是在参数模式是 IN 的时候使用 default )

    image

    注意 : 参数传递, p_loc => 2400 ( 例如 : 2 类型 => 进行参数传递 )

    add_dept(p_loc = 1200);   // 因为是在别的 PL/SQL Block 中,所以,这就相当于调用了, 不需要在 execute 了.

    内部 Subprograms

    image

    You can declare subprograms in any PL/SQL block.  如上例子 : procedure log_exec

    - log_exec : 是一个独立的 procedure, 但是,它在 “父”procedure 内。

    - 外边没有办法调用 log_exec 这个 procedure.

    - log_exec scope : 就是“父”procedure 内。

    Declaring local subprograms enhances the clarity of the code by assigning appropriate business-rule identifiers to blocks of code. ( 定义这个内层的 subprogram, 主要是为了更清晰的表明商业上的逻辑 )

    注意 : You must declare  the subprogram in the declaration section of the block, and it must be the last item.

    调用别的 procedure , 并且可以捕获到异常

    当调用一个 procedure时,如果在 procedure内部出现了异常,并且在内部的 exception 捕获了该异常,那么 procedure结束,继续执行外边的代码,并且,执行 exception 之前的所有的 transaction 还继续存在。

    image

    When you develop procedures taht are called from other procedures. When an exception is raised in a called procedure. control immediately goes to the exception section of that block. if the exception is handled, the block terminates, and control goes to the calling program. Any data manipulation language( DML ) statements issued before the exception was raised remain as part of the transaction.

    调用别的 procedure , 并且可以捕获到异常

    image

    与上边的不同之处是,control 返回时,由于没有捕获成功,所以直接返回到 exception 中( 调用函数内的 ), 继续尝试捕获。

    此时, PL/SQL does not roll back database work that is done by the subprogram, if the exception is handled in the calling procedure, all DML statements in the calling procedure and in the called procedure remain as part of the transaction. if not , the calling procedure terminates and the exception propagates to the calling environment. ALL the DML statements in the calling procedure and the called procedure are rolled back along with any changes to any host variables. The host environment determines the outcome for the unhandled exception.

    删除 Procedure

    DROP PROCEDURE procedure_name

    例如 : DROP PROCEDURE raise_salary ;

    Sub program 好处 ( procedure , function )

    - 容易维护 : 例行公事等等情况,写一个存储过程,每次只要执行一下,就OK了( 传递 日期啊之类的参数进去 ).

    - 安全        : 可以提供给没有权限的用户执行 sub program 权限,这样,他只有执行权限,有利于保护 database.

    - 效率        : 节省了很多不必要的网络运行,比如,我们现在用的算工资,只要提供参数给database。

                         因为一次编译,就可以向后执行,也省了后边的编译时间。

    - 清晰        : sub program 将很多内容整合成了一个命令,这对于商业逻辑来说,看起来结构就清晰多了。

  • 相关阅读:
    Javascript中String()和new String()的区别——JS的包装对象
    文言色彩的客套话之感想
    面试时候可以问的问题集锦
    ES6的原始类型数据——Symbol
    python
    python
    python
    python
    python
    python
  • 原文地址:https://www.cnblogs.com/moveofgod/p/2809955.html
Copyright © 2011-2022 走看看