Skip to main content

PRAGMA RESTRICT_REFERENCES

 

Pragma Introduction

In my package's functions and procedure, sometimes I have some rules to control purity rules of the database,  like reading from tables only, not query the database, ...... etc 
So I will  use 
PRAGMA RESTRICT_REFERENCES to control and preserve the database state in database packages.

Syntax of 
PRAGMA RESTRICT_REFERENCES

PRAGMA RESTRICT_REFERENCES(SUBPROGRAM_NAME, IDENTIFIER)


SUBPROGRAM_NAME can be default which will affect all the subprograms in packages or subprogram name(function or procedure) in package

IDENTIFIER can be RNDS, WNDS, RNPS, WNPS and TRUST. I will explain every one separately.

1- RNDS

select query is restricted against any of database tables

2- WNDS

DML operations are restricted against any of database tables 

3- RNPS

Selection of package variables is restricted 

4- WNPS

Modification in packages variables is restricted

5- TRUST

Asserts that the subprogram can be trusted not to violate one or more rules


Demo

Test WNPS 
let's create packages 
XXX_PKG contains GN$USER package variables and SET_USER function to set GN$USER and I will use WNPS purity check in SET_USER function


 CREATE OR REPLACE PACKAGE XXX_PKG 

 AS 

   GN$USER  VARCHAR2 (100); 

  

   PROCEDURE SET_USER; 

  

   PRAGMA RESTRICT_REFERENCES (SET_USER, WNPS); 

 END; 

  

 CREATE OR REPLACE PACKAGE BODY XXX_PKG 

 AS 

   PROCEDURE SET_USER 

   IS 

   BEGIN 

    GN$USER := 'Mahmoud A. El-Sayed'; 

   END; 

 END; 


When compile the package body compiler will raise the below error
PLS-00452: Subprogram 'SET_USER' violates its associated pragma

Test RNDS
let's create packages 
XXX_PKG contains GET_EMPLOYEE_NAME function which query from EMP database table.

 CREATE OR REPLACE PACKAGE XXX_PKG 

 AS 

   FUNCTION GET_EMPLOYEE_NAME (IN_EMPLOYEE_ID NUMBER) 

    RETURN VARCHAR2; 

  

   PRAGMA RESTRICT_REFERENCES (GET_EMPLOYEE_NAME, RNDS); 

 END; 

  

 CREATE OR REPLACE PACKAGE BODY XXX_PKG 

 AS 

   FUNCTION GET_EMPLOYEE_NAME (IN_EMPLOYEE_ID NUMBER) 

    RETURN VARCHAR2 

   IS 

    LV$EMPLOYEE_NAME  VARCHAR2 (100); 

   BEGIN 

    SELECT ENAME 

     INTO LV$EMPLOYEE_NAME 

     FROM EMP 

     WHERE EMPNO = IN_EMPLOYEE_ID; 

  

    RETURN LV$EMPLOYEE_NAME; 

   END; 

 END; 


When compile the package body compiler will raise the below error
PLS-00452: Subprogram 'GET_EMPLOYEE_NAME' violates its associated pragma

You can test the others purity checks, Just I made two examples to illustrate the idea.


one more easy example to check :


CREATE OR REPLACE PACKAGE rnds_PKG

 AS

   FUNCTION GET_EMPLOYEE_NAME (IN_EMPLOYEE_ID NUMBER)

    RETURN VARCHAR2;

  -- PRAGMA RESTRICT_REFERENCES (GET_EMPLOYEE_NAME, RNDS);

 END;



CREATE OR REPLACE PACKAGE BODY rnds_PKG

 AS

   FUNCTION GET_EMPLOYEE_NAME (IN_EMPLOYEE_ID NUMBER)

    RETURN VARCHAR2

   IS

    v_EMPLOYEE_NAME  VARCHAR2 (100);

   BEGIN

    SELECT ENAME

     INTO v_EMPLOYEE_NAME

     FROM EMP

     WHERE EMPNO = IN_EMPLOYEE_ID;

    RETURN v_EMPLOYEE_NAME;

   END;

 END;

select rnds_pkg.get_employee_name(7369) from dual;




Comments

Popular posts from this blog

Oracle Merge Statement Delta Detection

The MERGE Syntax by Ajay Nerumati Delta Detection in Oracle SQL Posted on  8. October 2016 Delta detection is a common task in every Data Warehouse. It compares new data from a source system with the last versions in the Data Warehouse to find out whether a new version has to be created. There are several ways to implement this in Oracle. Your source system delivers a full extraction every night, and you have to load only the changed rows into your Core Data Warehouse? You receive incremental loads from another source system every few minutes, but only a few columns are loaded into the Data Warehouse. In all these situations, you need a delta detection mechanism to identify the rows that have to be inserted or updated in your Data Warehouse tables. In this blog post, I want to show different methods in Oracle SQL that provide the subset of rows of a source table that were changed since the last load. All these methods are set-based, i.e. they can be executed in one SQL sta...

FOREIGN KEY

Pre-requisits : General understanding of integrity constraints. Constrains are used to prevent invalid data into the table. Integrity constrains are business rules enforced on the data in a table. The enforced rules trigger whenever a row is inserted, updated, or deleted from that table and prevent data that does not meet the set rules. FOREIGN KEY :  Establishes and enforces a relationship between two tables or to the same table itself. The foreign key or referential integrity constraint, designates a column or combination of columns as a foreign key and establishes a relationship between a primary key or a unique key in the same table or a different table.  In the example below   Emp_child  table's deptno   has been defined as the foreign key ; it references the deptno column of the Dept_parent table. Implementation: Step 1 Create parent table DEPT_PARENT as follows: Create table DEPT_PARENT ( Deptno n...

RDBMS MINI PROJECT

  Capgemini class room training.   RDBMS MINI PROJECT ( SPRINT ) LIBRARY MANAGEMENT SYSTEM   Table of Contents Serial No. Topic Name Content Page No. 1.   Introduction 1.1 Setup checklist for mini project 3     1.2 Instructions 3 2.   Problem statement   2.1 Objective 4     2.2 Abstract of the project 4     2.3 Functional components of the project 4     2.4 Technology used 5 3.   Implementation in RDBMS LOT 3.1 Guidelines on the functionality to be built 6 4.   Evaluation 4.1 Evaluation 7   ...