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在运行是对它们不能检查。
eg:
create or replace package pack_purity is
min_sal emp.sal%type;
max_sal emp.sal%type;
function get_minsal return emp.sal%type;
function get_maxsal return emp.sal%type;
pragma restrict_references(get_minsal,WNPS); –禁止向包内写入数据
pragma restrict_references(get_maxsal,WNPS);
end pack_purity;
create or replace package body pack_purity is
function get_minsal return emp.sal%type
is
begin
return min_sal;
end;
function get_maxsal return emp.sal%type
is
begin
return max_sal;
end;
–构造方法给变量赋值,但get_maxsal函数却不可以赋值,只能读取;
begin
select min(sal),max(sal) INTO min_sal,max_sal from emp;
end;
end pack_purity;
/*
调用纯度级别的包类函数
*/
declare
min_sal emp.sal%type;
max_sal emp.sal%type;
begin
min_sal:= pack_purity.get_minsal();
max_sal:= pack_purity.get_maxsal();
dbms_output.put_line(‘最小工资:’|| min_sal);
dbms_output.put_line(‘最大工资:’ || max_sal);
end;