469,366 Members | 2,306 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Using PRAGMA - II

debasisdas
8,127 Expert 4TB
This thread contains some useful tips/sample codes regarding some of advance concepts in Use of PRAGMA in oracle, that the forum members may find useful.

PRAGMA EXCEPTION_INIT
====================
This is used to bind a user defined exception to a particular error number.

For example: To display USER DEFINED MESSAGE FOR ORACLE DEFINED NUMBER
---------------------
Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2. I EXCEPTION;
  3. PRAGMA EXCEPTION_INIT(I,-00001);
  4. BEGIN
  5. INSERT INTO DEPT VALUES(&DNO,'&DNAME','&LOC');
  6. DBMS_OUTPUT.PUT_LINE('ONE RECORD INSERTED');
  7. EXCEPTION
  8. WHEN I THEN
  9. DBMS_OUTPUT.PUT_LINE('DUPLICATE VALUE');
  10. END;
  11.  
Few more predefined number for predefined exception
========================================

dup_val_on_index, -0001
timeout_on_resource, -0051
invalid_cursor, -1001
not_logged_on, -1012
login_denied, -1017
too_many_rows, -1422
zero_divide, -1476
invalid_number, -1722
storage_error, -6500
program_error, -6501
value_error, -6502
rowtype_mismatch, -6504
cursor_already_open, -6511
access_into_null, -6530
collection_is_null , -6531
subscript_outside_limit, -6532
subscript_beyond_count , -6533
Jun 16 '07 #1
2 11142
debasisdas
8,127 Expert 4TB
RESTRICT_REFERENCES Pragma
===========================
This pragma was used to assert to the PL/SQL compiler the purity level of a packaged procedure or function. The RESTRICT_REFERENCES pragma had to be included in the package specification if you were to use that program inside a SQL statement (directly or indirectly).

This is not required starting with Oracle8i.

This pragma confirms to Oracle database that the function as the specified side-effects or ensures that it lacks any such side-effects.

Usage is as follows:

PRAGMA RESTRICT_REFERENCES(function_name, WNDS [, WNPS] [, RNDS], [, RNPS])

WNDS: Writes No Database State. States that the function will not perform any DMLs.

WNPS: Writes No Package State. States that the function will not modify any Package variables.

RNDS: Reads No Database State. Analogous to Write. This pragma affirms that the function will not read any database tables.

RNPS: Reads No Package State. Analogous to Write. This pragma affirms that the function will not read any package variables.

You can declare the pragma RESTRICT_REFERENCES only in a package spec or object type spec. You can specify up to four constraints (RNDS, RNPS, WNDS, WNPS) in any order. To call the function from parallel queries, you must specify all four constraints. No constraint implies another. For example, WNPS does not imply RNPS.

When you specify TRUST, the function body is not checked for violations of the constraints listed in the pragma. The function is trusted not to violate them.

If you specify DEFAULT instead of a function name, the pragma applies to all functions in the package spec or object type spec (including, in the latter case, the system-defined constructor). You can still declare the pragma for individual functions. Such pragmas override the default pragma.

A RESTRICT_REFERENCES pragma can apply to only one function declaration. So, a pragma that references the name of overloaded functions always applies to the nearest foregoing function declaration.

syntax
========
Expand|Select|Wrap|Line Numbers
  1. PRAGMA RESTRICT_REFERENCES ( 
  2.     function_name, WNDS [, WNPS] [, RNDS] [, RNPS] [, TRUST] );
  3.  
In the following example, the pragma applies to the second declaration of isok:
Expand|Select|Wrap|Line Numbers
  1. CREATE PACKAGE showrec AS
  2.    FUNCTION isok (amount NUMBER) RETURN BOOLEAN;
  3.    FUNCTION isok (net_time DATE) RETURN BOOLEAN;
  4.    PRAGMA RESTRICT_REFERENCES (isok, WNDS);
  5.    ...
  6. END showrec;
  7.  
DEFAULT
----------------
This specifies that the pragma applies to all functions in the package spec or object type spec. You can still declare the pragma for individual functions. Such pragmas override the default pragma.
Jun 16 '07 #2
debasisdas
8,127 Expert 4TB
SERIALLY_REUSABLE:
====================
This pragma lets the PL/SQL engine know that package-level data should not persist between reference to that data.

Package data (global variables in package specification etc.) by default persists for an entire session (or until a package is recompiled). Globally accessible data structures can cause some side effects. For instance, what if a cursor is left open in a package. In addition, a program can use up lots of real memory (UGA) and then not release it if the data is stored in a package-level structure.

In order to manage this, Oracle8i introduced the SERIALLY_REUSABLE pragma. This pragma is used in packages only and must be defined BOTH in specification and in the body. A bodiless package can be marked as serially reusable. If a package has a spec and body, you must mark both. You cannot mark only the body.

The global memory for serially reusable packages is pooled in the System Global Area (SGA), not allocated to individual users in the User Global Area (UGA). That way, the package work area can be reused. When the call to the server ends, the memory is returned to the pool. Each time the package is reused, its public variables are initialized to their default values or to NULL.

The advantage is that based on the pragma, a package state can be reduced to a single call of a program unit in the package as opposed to the package being available for the whole session.

Note :---Serially reusable packages cannot be accessed from database triggers.

SAMPLE CODE OF PRAGMA SERIALLY_REUSABLE;
===========================================
Expand|Select|Wrap|Line Numbers
  1. CREATE PACKAGE pkg1 IS
  2.    PRAGMA SERIALLY_REUSABLE;
  3.    num NUMBER := 0;
  4.    PROCEDURE init_pkg_state(n NUMBER);
  5.    PROCEDURE print_pkg_state;
  6. END pkg1;
  7.  
  8. CREATE PACKAGE BODY pkg1 IS
  9.    PRAGMA SERIALLY_REUSABLE;
  10.    PROCEDURE init_pkg_state (n NUMBER) IS
  11.    BEGIN
  12.       pkg1.num := n;
  13.    END;
  14.  
  15.    PROCEDURE print_pkg_state IS
  16.    BEGIN
  17.       dbms_output.put_line('Number: ' || pkg1.num);
  18.    END;
  19. END pkg1;
  20.  
Jun 16 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Shri | last post: by
1 post views Thread by Gustavo L. Fabro | last post: by
15 posts views Thread by muttaa | last post: by
10 posts views Thread by =?Utf-8?B?TmFuZCBLaXNob3JlIEd1cHRh?= | last post: by
5 posts views Thread by venkat | last post: by
26 posts views Thread by Rick | last post: by
2 posts views Thread by aleemakhtar1 | last post: by
debasisdas
reply views Thread by debasisdas | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.