zoukankan      html  css  js  c++  java
  • oracle 包纯度级别

    oracle 包纯度级别
        PRAGMA RESTRICT_REFERENCES(),这个PRAGMA比较复杂, 总的来说,它是一个程序辅助检验码,检查子程序的纯度(PURITY),帮助检验子程序是否有违反规则的地方。一般用在函数上,但当函数调用过程时,也要作相应的设置检查。这是为了避免当在DML语句上调用函数时正常执行不至于产生错误。

        语法,PRAGMA RESTRICT_REFERENCES(function_name | default , )RNDS, WNDS, RNPS, WNPS) | , TRUST);

         RNDS,WNDS,RNPS,WNPS可以同时指定。但当TRUST指定是,其它的被忽略。

         DEFAUT是指作用在该程序包上的所有子程序,函数。

         RNDS(Read No Database State),规定子程序不能读取任何的数据库状态信息。(即不会查询数据库的任何表,包括DUAL虚表)

         RNPS(Read No Package State),规定子程序不能读取任何程序包的状态信息,如变量等。

         WNDS(Write No Database State),规定子程序不能向数据库写入任何信息。(即不能修改数据库表)

         WNPS(Write No Package State),规定子程序不能向程序包写入任何信息。(即不能修改程序包变量的值)

         TRUST,指出子程序是可以相信的不会违反一个或多个规则。这个选项是需要的当用C或JAVA写的函数通过PL/SQL调用时,因为PL/SQL在运行是对它们不能检查。

    示例:
    create or replace package purity is
      minsal number(9);
      maxsal number(9);
      function max_sal return number;
      function min_sal return number;
      pragma restrict_references(max_sal, wnps);
      pragma restrict_references(min_sal, wnps);
    end;

    create or replace package body purity is
    function max_sal return number as
    begin
    select max(sal) into maxsal from emp; return maxsal;
    end;

    function min_sal return number as
    begin
    select min(sal) into minsal from emp; return minsal;
    end;

    对该包进行编译时提示“PACKAGE BODY SCOTT.PURITY 编译错误
    错误:PLS-00452: 子程序 'MAX_SAL' 违反了它的相关编译指示
    行:2
    文本:function max_sal return number
    错误:PLS-00452: 子程序 'MIN_SAL' 违反了它的相关编译指示
    行:10
    文本:function min_sal return number”

    因为规定了包中的函数不能修改包中的变量,在函数max_sal和min_sal中进行赋值操作时改变了包的变量minsal和maxsal,因此导致编译错误。
    修改如下:即函数只读但不做修改包变量:
    create or replace package purity is
      minsal number(9);
      maxsal number(9);
      function max_sal return number;
      function min_sal return number;
      pragma restrict_references(max_sal, wnps);
      pragma restrict_references(min_sal, wnps);
    end;

    create or replace package body purity is
    function max_sal return number as
    begin
    return maxsal;
    end;

    function min_sal return number as
    begin
    return minsal;
    end;

    begin
    select min(sal), max(sal) into minsal, maxsal from emp;
    end;

    --调用包的公用函数
    SQL> var minsal number;
    SQL> var maxsal number;
    SQL> exec :minsal:=purity.min_sal;
     
    PL/SQL procedure successfully completed
    minsal
    ---------
    800
    SQL> exec :maxsal:=purity.max_sal;
     
    PL/SQL procedure successfully completed
    maxsal
    ---------
    5000
  • 相关阅读:
    HDU 1069 Monkey and Banana
    HDU 1029 Ignatius and the Princess IV
    HDU 1024 Max Sum Plus Plus
    Gym100923H Por Costel and the Match
    Codeforces 682C Alyona and the Tree
    Codeforces 449B Jzzhu and Cities
    Codeforces (ccpc-wannafly camp day2) L. Por Costel and the Semipalindromes
    Codeforces 598D (ccpc-wannafly camp day1) Igor In the Museum
    Codeforces 1167c(ccpc wannafly camp day1) News Distribution 并查集模板
    快乐数问题
  • 原文地址:https://www.cnblogs.com/lanzi/p/1826947.html
Copyright © 2011-2022 走看看