P. 1
Pragma Details

Pragma Details

|Views: 3|Likes:
Published by Bala Krishna

More info:

Published by: Bala Krishna on Aug 24, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

08/24/2013

pdf

text

original

PRAGMA AUTONOMOUS TRANSACTIONS........................................................ . . . . . . . . . . . . . . . . . . . . . . . . 1 DESCRIPTION................... ................................................................................ .......

......... 2 EXAMPLE Autonomous Transaction............................... ................... . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 PRAG MA EXCEPTION.................................................................... ................. . . . . . . . . . . . 14 PRAGMA SERIALLY_REUSABLE............. ............................................................... 15 PRAGMA RESRTI CTED_REFERENCE..................................................... . . . . . . . . . . . . . . . 17 AUTONOMOUS TRANSACTIONS INTRO Before the release of PL/SQL 8.1, each Oracle session could have at most one active transaction at a given time. In other words,any and all changes made in y our session had to be either saved or erased in their entirety. This restriction has long been considered a drawback in the PL/SQL world. Developers have requested the ability to execute and save or cancel certain DML statements (INSERT, UPDATE, DELETE) without affecting the overall session s transaction. You can now accomplish this goal with the autonomous transaction feature of PL/SQL 8.1 and above. When you define a PL/SQL block (anonymous block, procedure , function, packaged procedure, packaged function, database trigger) as an autonom ous transaction, you isolate the DML in that block from the caller s transaction conte xt. That block becomes an independent transaction that is started by another transac tion, referred to as the main transaction. Within the autonomous transaction block, the main transaction is suspended. You perform your SQL operations, commit or roll back those operations, and resume the main transaction. To define a PL/SQL block as an autonomous transaction you simply include the following statement in your declaration section: Syntax -PRAGMA AUTONOMOUS_TRANSACTION; The pragma instructs the PL/SQL compiler to establish a PL/SQL block as autonomo us or independent. For the purposes of the autonomous transaction, a PL/SQL bloc k can be any of the following: Top-level (but not nested) anonymous PL/SQL blocks Functions and procedures, defined either in a package or as standalone programs Methods (functions and procedures) of a SQL object type Database triggers 1 You can put the autonomous transaction pragma anywhere in the declaration sectio

Usage Notes In this context. autonomous triggers can contain transaction control statements such as COMMIT and ROLLBACK. DESCRIPTION The AUTONOMOUS_TRANSACTION pragma instructs the PL/SQL compiler to mark a routine as autonomous (independent). COMMIT. you can log eve nts. and so on. Also. but only if its isolation level is set to READ COMM ITTED (the default). the main transaction . even if the main transaction rolls back. However. code the pragma at the top of the section. 2 . Autonomous transactions let you suspend the main transaction. unlike regular triggers. It shares no locks .n of your PL/SQL block. Y ou can code the pragma anywhere in the declarative section of a routine. This pragma is the only syntax change made to PL/SQL to support autonomous transactions. these statements have a different scope of impact and visibilit y when executed within an autonomous transaction. commit or roll back those operations. for r eadability. Once started. the DML statements all the rest is as it was before. and you will have to include a COMMIT or ROLLBACK in your program. Unlike regular triggers. So. autonomous triggers can execute DDL statements (such as CREATE and DROP) using native dynamic SQL. An autonomous transaction is an independent transaction started by another transaction. Changes made by an autonomous transaction become visible to other transactions w hen the autonomous transaction commits. standalone. or commit-dependencies with the main transaction. The best option. an autonomous transaction is fully independent. the term routine includes Top-level (not nested) anonymous PL/SQL blocks Local. do SQL operations. then resume the main transaction. and packaged functions and procedures Methods of a SQL object type Database triggers You cannot use the pragma to mark all subprograms in a package (or all methods i n an object type) as autonomous. The changes also become visible to the main transaction when it resumes. increment retry counters. ROLLBACK. would be to place it before any dat a structure declarations. however. That way. anyone reading your code will immediately iden tify the program as an autonomous transaction. resources. Only individual routines can be marked autonomous. But.

you mark a packaged function as autonomous: CREATE PACKAGE banking AS . RESTRICT_REFERENCES Pragma. Unlike regular triggers. BEGIN . changes made by its autonomous transactions are not visible to the main transact ion when it resumes: When in the main transaction. 3 CREATE TRIGGER parts_trigger BEFORE INSERT ON parts FOR EACH ROW DECLARE PRAGMA AUTONOMOUS_TRANSACTION. COMMIT.. Oracle raises an exception in the autonomous transaction. Related Topics EXCEPTION_INIT Pragma. ****** When to Use Autonomous Transactions .. autonomous transactions are fully independent of the main transaction.. :new. Oracle raises an exception. you mark a database trigger as autonomous. which is rolled back if the exception goes unhandled.. END banking.. If you try to exit an active autonomous transaction without committing or rollin g back.pnum. FUNCTION balance (acct_id INTEGER) RETURN REAL. the transaction is rolled back. END. my_bal REAL. If an autonomous transaction attempts to access a resource held by the main tran saction (which cannot resume until the autonomous routine exits). If the exception goes unhandled. rolling back to a savepoint marked before you star ted an autonomous transaction does not roll back the autonomous transaction. autonomous triggers can contain transaction control statements. END banking. -.. SERIALLY_RESUABLE Pragma EXAMPLE Autonomous Transaction In the following example. In the example below. BEGIN INSERT INTO parts_log VALUES(:new. In that case.If you set the isolation level of the main transaction to SERIALIZABLE.pname). Remember. CREATE PACKAGE BODY banking AS . a deadlock can occur. FUNCTION balance (acct_id INTEGER) RETURN REAL ISPRAGMA AUTONOMOUS_TRANSACTION. as follo ws.allowed only in autonomous triggers END.

You will want to define your program module as an autonomous transaction wheneve r you want to isolate the changes made in that module from the caller s transaction context. Retry counter Autonomous transactions can help you keep track of how many times a user tries t o connect to a database or get access to a resource (you'll reject access after a certain number of attempts). Software usage meter You want to keep track of how often a program is called during an application se ssion. Autonomous transactions will play a crucial role in this area. Reusable application components It is becoming ever more important to be able to offer standalone units of work (also known as cartridges) that get their job done without any side effects on the cal ling environment. COMMIT. 4 ****** RULES AND EXCEPTIONS While it is certainly very easy to add the autonomous transaction pragma to your code. The following construction will work: DECLARE PRAGMA AUTONOMOUS_TRANSACTION. then you can commit and/or roll back in that code. END.). You need to log error information in a database table. Here are some specific ideas where autonomous transactions are useful: Logging mechanism This is the classic example of the need for an autonomous transaction. This information is not dependent on. Commits and rollbacks in your database triggers If you define a trigger as an autonomous transaction. BEGIN INSERT INTO emp VALUES (myempno. You can only m ake a toplevel anonymous block an autonomous transaction. the transaction being p rocessed in the application. myempno NUMBER. there are some rules and restrictions on the use of this feature. /whereas this construction: DECLARE . and cannot affect. but don't want that log entry to be a par t of the logical transaction... .

you can cause a d eadlock to occur in your program. BEGIN INSERT INTO emp VALUES (myempno. These actions will not affect the transaction that caused the database trigger to fire . A procedure to perform an update is created. and then called after already having all rows updated: 5 CREATE OR REPLACE PROCEDURE update_salary (dept_in IN NUMBER) ISPRAGMA AUTONOMOUS_TRANSACTION. /results in this error: PLS-00710: PRAGMA AUTONOMOUS_TRANSACTION cannot be declared here You can now use COMMIT and ROLLBACK inside your database triggers. You must indicate autonomous transactions explicitly in each program. END LOOP. .empno. COMMIT. COMMIT. END. END.. update_salary (10). For example. If an autonomous transaction attempts to access a resource held by the main tran saction (which has been suspended until the autonomous routine exits). END. BEGIN DECLARE PRAGMA AUTONOMOUS_TRANSACTION. the following package spec ification is invalid: CREATE PACKAGE warcrimes_pkg . END.myempno NUMBER.).. /BEGIN UPDATE emp SET sal = sal * 2. CURSOR myemps IS SELECT empno FROM emp WHERE deptno = dept_in FOR UPDATE NOWAIT. /This results in the following error: ERROR at line 1: ORA-00054: resource busy and acquire with NOWAIT specified You cannot mark all subprograms in a package (or all methods in an object type) as autonomous with a single PRAGMA declaration. Here is a simple example to demonstrate the problem. BEGIN FOR rec IN myemps LOOP UPDATE emp SET sal = sal * 2 WHERE empno = rec.

You can have multiple COMMIT and/or ROLLBACK statements inside your autonomous block. you must perform an explicit commit or rollback. You would then want to use this procedure: CREATE OR REPLACE PROCEDURE fire_em_all ISPRAGMA AUTONOMOUS_TRANSACTION. for example. Suppose. END warcrimes_pkg. / 6 One consequence of this rule is that you cannot tell by looking at the package specification which. If the program (or any program called by it) has tr ansactions pending. the following error occurs: SQL> exec fire_em_all ERROR at line 1 ORA-06519: active autonomous transaction detected and rolled back The COMMIT and ROLLBACK statements end the active autonomous transaction. or more autonomous transactions can be executed within an autonomous block. the runtime engine will raise this excep tion: ORA-01086: savepoint 'your savepoint' never established The TRANSACTIONS parameter in the Oracle initialization file (INIT. force the termination of the autonomous routi ne. however.ORA) specifies the maximum number of transactions allowed concurrently in a session. END. you 7 might exceed this number and raise an exception unless you raise the TRANSACTIONS value. If you do not explicitly co mmit. BEGIN DELETE FROM emp. when you run this procedure. Zero. If you try to do so. they do not. one. An autonomous block is one in which autonomous transactions are expected.ASPRAGMA AUTONOMOUS_TRANSACTION. PROCEDURE register ( culprit IN VARCHAR2. To exit without errors from an autonomous transaction program. If you use autonomous transactions (which run concurrently with the main transaction). This is the error you will get if you encounter this problem : . event IN VARCHAR2). if any. When you are in an autonomous transaction you cannot roll back to a savepoint set in the main transaction. You can roll back only to savepoints marked in the current transaction. that your job is to take over failing companies and make t hem profitable by firing employees. programs will run as autonomous transactions. / You want to make the program an autonomous transaction because you don t want anyo ne to back out the changes when you are not looking. the runtime engine will raise an exception and then it will roll back tho se uncommitted transactions.

rowid_info ROWID. such as the WHERE clause. END. As long as you define a program to be an autonomous transaction. the results are very . yet a n autonomous transaction s DML actions never affect the main transaction.ORA-01574: maximum number of concurrent transactions exceeded The default value for TRANSACTIONS in Oracle8i is 75. An autono mous transaction program never violates the two database-related purity levels. This is because those purity levels or constraints apply to the SQL statement (which. in this case. This function is then created to perform the audit: CREATE OR REPLACE FUNCTION traceit ( tab IN VARCHAR2. * ERROR at line 1: ORA-14551: cannot perform a DML operation inside a query However. The main one is this: you are n ot allowed to update the database. With the autonomous transaction feature. if traceit is now transformed into an autonomous transaction by adding the pragma (and committing the results before the RETURN statement). If your program cannot assert a nother purity level.3. suppose that you want to keep a trace of all the rows that have b een touched by a query. RNDS (reads no database state) and WNDS (writes no database state). And you can t save or cancel changes from withi n the function. rowid) from emp. you may be restrict ed from calling that program in certain parts of the SQL statement. This table is created: CREATE TABLE query_trace ( table_name VARCHAR2(30). rowid_in. such as WNPS (writes no package state). USER. Using Autonomous Transactions from Within SQL Ever since Oracle 7. queried_by VARCHAR2(30). 8 /When you try to use this function inside a query. you get the following error: SQL> select ename. queried_at DATE ). SYSDATE). rowid_in IN ROWID) RETURN INTEGER IS BEGIN INSERT INTO query_trace VALUES (tab. you have been able to call your own functions from within SQL provided that you follow a variety of rules. As an example. however. traceit ('emp'. RETURN 0. the picture changes. even if that program act ually does read from or write to the database. you can also ca ll it directly or indirectly in a SQL statement. is the main transaction).

. 'HH:MI:SS') queried_at 3 FROM query_trace. The query works.-----------------. which means that as soon as chang es are committed. Now that autonomous transactions are available. in addition. not just the current program. an autonomous transaction procedure is created: CREATE OR REPLACE PROCEDURE fire_em_all ISPRAGMA AUTONOMOUS_TRANSACTION. issue any SQL statements). if you do want to send information to a database table (or delete rows or update data.---------emp AAADEPAACAAAAg0AAA SCOTT 05:32:54 . they are visible to the main transaction. 2 TO_CHAR (queried_at. First. rowid_info. and the query_trace table is filled: SQL> SELECT ename. BEGIN DELETE FROM emp2. The following script demonstrates the SERIALIZABLE isolation level at work. TABLE_NAME ROWID_INFO QUERIED_BY QUERIED_AT ---------..).ROWID) ------------------------KING 0 . emp AAADEPAACAAAAg0AAN SCOTT 05:36:50 You have other options when it comes to tracing queries: you can write to the sc reen with the DBMS_OUTPUT built-in package or send information to a pipe with DB MS_PIPE. ENAME TRACEIT ('EMP'. ROWID) 2 FROM emp.. you can t ake that route instead... SQL> SQL> SELECT table_name.. queried_by. Oracle offers a SET TRANSACTION statement option to hide those changes from the main transaction: SET TRANSACTION ISOLATION LEVEL SERIALIZABLE.---------. you must call it befo re you initiate your transactions (i. As is usually the case with the SET TRANSACTION statement. traceit ('emp'. etc. but be sure to analyze carefully the overhead of this ap proach.different. The default isolation level is READ COMMITTED. th e setting affects your entire session. MILLER 0 14 rows selected.e. Transaction Visibility 9 The default behavior of autonomous transactions is that once a COMMIT or ROLLBACK occurs in the autonomous transaction. COMMIT. those changes are visible immediately in the main transaction.

here is the script that is run: DECLARE PROCEDURE showcount (str VARCHAR2) IS num INTEGER.END. showcount ('After MT commit'). statements that are otherwise not allowed in database triggers. END. showcount ('Before isolated AT delete'). fire_em_all. You may want to take an action in the database trigger that is not affected by t he ultimate disposition of the transaction that caused the trigger to fire. /A script is run that sets the isolation level to SERIALIZABLE. BEGIN SELECT COUNT(*) INTO num FROM emp2. For example.PUT_LINE (str || ' ' || num). END. /Here is the output from running the script: Before isolated AT delete 14 After isolated AT delete 14 After MT commit 0 ****************** EXAMPLE FOR DATA BASE TRIGGER DEFINED AS AN AUTONOMOUS TRANSACTIONS The benefit of autonomous transactions for database triggers is that inside those triggers you can now issue COMMITs and ROLLBACKs. They w ill only apply to DML activity taking place inside the trigger itself (or throug h stored program units called within the trigger). You might even want to be able to detect which actions failed. You ca n use autonomous transactions to do this. then the number of rows that appear in the emp2 table at the following times are displayed: Before I call fire_em_all After I call fire_em_all but before the main transaction is committed or rolled back After I commit in the main transaction. construct a simple autonomous transaction trigger on the ceo_compensation . BEGIN SET TRANSACTION ISOLATION LEVEL SERIALIZABLE. whether or not the action is completed. The changes you commit and roll back will not affect the main transaction that caused the database trigger to fire. First. showcount ('After isolated AT delete'). sup pose that you want to keep track of each action against a table. DBMS_OUTPUT. 10 COMMIT.

BEGIN INSERT INTOceo_ com p_history VALUES ( :new.name. INSERT INTOceo_ com pensation VALUES ( 'Eastman Kodak'. 'BEFORE INSERT'. SYSDATE). /SELECT name. 'Harvey Golub'. every insert attempt can be tracked. END. 33200000.table that writes a simple message to the following ceo_comp_history table. occurred_on DATE). compensation NUMBER. COMMIT. name VARCHAR2(100). description VARCHAR2(255). TO_CHAR (occurred_on. Here is the before-insert trigger to run all the elements in the script: CREATE OR REPLACE TRIGGER bef_ins_ceo_comp 11 BEFORE INSERT ONceo_ com pensation FOR EACH ROW DECLARE PRAGMA AUTONOMOUS_TRANSACTION.---------------------------------Jill Barad BEFORE INSERT 03/17/1999 04:00:56 Harvey Golub BEFORE INSERT 03/17/1999 04:00:56 George Fisher BEFORE INSERT 03/17/1999 04:00:56 Fine-tuning the database trigger . NAME DESCRIPTION OCCURRED_ON -------------------. layoffs NUMBER). as shown in th e steps below: BEGIN INSERT INTOceo_ com pensation VALUES ( 'Mattel'. 'George Fisher'. Her e are the two table definitions: CREATE TABLE ceo_compensation ( company VARCHAR2(100). 20100). 2700). 3300). 'Jill Barad'. CREATE TABLE ceo_comp_history ( name VARCHAR2(100). /With this trigger in place. 9100000. INSERT INTOceo_ com pensation VALUES ( 'American Express Company'. 'MM/DD/YYYY HH:MI:SS') occurred_on FROMceo_ com p_history. --I wish! END. 10700000. description. ROLLBACK.

END IF. END.name. CREATE OR REPLACE TRIGGER aft_ins_ceo_comp AFTER INSERT ON ceo_compensation FOR EACH ROW DECLARE ok BOOLEAN := is_valid_comp_info (:NEW. description IN VARCHAR2. /Note the following differences: The trigger is now an after-insert trigger.occurred_on ).description. do not even perform an . it is recommended that you not make a database trigger itself the aut onomous transaction. 'AFTER INSERT'. then perform the audit. /Then change the trigger to the following. Make that procedure the autonomo us transaction. push all of the independent DML activity (such as writing to the audit or history table) into its own procedure. END. ELSE RAISE VALUE_ERROR. Wait until after the INSERT to the compensation table takes place. audit_ceo_comp. BEGIN IF ok THEN audit_ceo_comp ( :new. First.name). SYSDATE). audit_ceo_comp.name.But there is something of a problem with the trigger just defined. occurred_on IN DATE ) ISPRAGMA AUTONOMOUS_TRANSACTION. rather than a before-insert trigger. It won t be rolled back with the rest of the tra nsaction (if a rollback occurs). The trigger w as defined as an autonomous transaction because the alert was performed in the b ody of the trigger. Suppose you want to perform some additional DML for the main transaction here in the trigger. COMMIT. create the audit procedure: CREATE OR REPLACE PROCEDURE audit_ceo_comp ( name IN VARCHAR2. BEGIN INSERT INTO ceo_comp_history VALUES ( audit_ceo_comp. Instead. 12 Generally. 13 When the is_valid_comp_info function returns FALSE. Have the trigger call the procedure.

as is almost always the case). we declare and associate an exception for this er ror: ORA-2292 violated integrity constraining (OWNER. This error occurs if we try to delete a parent record while there are child reco rds still in that table. BEGIN /* Try to delete the company. You should only associ ate a single error code to a specific exception. DECLARE exception_name EXCEPTION. */ PRAGMA EXCEPTION_INIT (still_have_employees. if the error code is negat ive. you will always want to perform an audit. this exception is raised! */ . The EXCEPTION_INIT clause must appear in the same declarative section as its associated exception handler. BEGIN wheree xception_name is the name of an exception anderro r_code_l iteral is the number of the Oracle error (including the minus sign.audit. Under other circumstances. */ still_have_employees EXCEPTION. /* Associate the exception name with an error number. PRAGMA EXCEPTION_INIT (exception_name. You will almost always be better off hid ing the details of your new.CONSTRAINT) child record found. This allows specification of a handler to take care of any exceptions that resul t from an Oracle error. however. In the following program code. you must a llow for processing of non-specified exceptions in the OTHERS exception area. -2292). you may want to stop the main transaction by raising an exception. A child record is a record with a foreign key reference to the paren t table: 14 PROCEDURE delete_company (company_id_in IN NUMBER) IS /* Declare the exception. error_code_literal). plan out how you will be using these new code elements. EXCEPTION /* If child records were found. As you take advantage of the new autonomous transaction pragma. stop the transaction by raising an error. independent transactions behind a procedural interf ace. Both of those events can't happen if the exception is raised in the same block and transaction as the audit DML. This demonstrates the other reason you don t want the trigger itself to be autonomous. In some situations. */ DELETE FROM company WHERE company_id = company_id_in. If you don't specify the EXCEPTION_INIT clause. PRAGMA EXCEPTION The EXCEPTION_INIT pragma is used to tell the compiler to associate a particular error number with an exception declared in your program. Instead.

" You can so mark a package if its state is needed only for the duration of a call to the server (for example. which lets you mark some packages as "serially reusable. The global memory for such packages is not kept in the memory area per user. but instead in a small SGA pool for reuse. The keyword pragma signifies that the statement is a pragma (compiler directive) . the pur pose of the exception handler is self-evident. Before reuse. In this scheme. Note: Use this feature with care.PUT_LINE (' Please delete employees for company first. By explicitly naming this system exception.WHEN still_have_employees THEN DBMS_OUTPUT. The user of "serially reusable" packages does increase the shared-pool requirements slightly. it limits scalability since such memory grows linearly with the number of users. an OCI call to the server. END. the package global variables are in itialized to NULL or to the default values provided. but this is more than offset by the decrease in the per-user memory requirements . 15 The pool is kept in SGA memory so that the work area of a package can be reused across users who have requests for the same package. They do not affect the m . Further. Oracle ages out work areas not in use when it needs to reclaim shared pool memor y. To help applications better manage memory usage. Many of your existing packages may absolutely rely on the persistent data feature. global data). any data declared in a package simply stayed around until the end of the session. You can even assign more than one excepti on name to the same error number. When you use EXCEPTION_INIT. whether or not it was needed any more by the application.'). which is typically much fewer than the total number of logged on users. *********** PRAGMA SERIALLY_REUSABLE Prior to PL/SQL 8. The EXCEPTION_INIT pragma improves the readability of your programs by assigning names to otherwise obscure error numbers. a PL/SQL client-to-server. not at run time. or se rver-toserver RPC). You can employ the EXCEPTION_INIT pragma more than once in your program. PL/SQL8 provides the pragma SERIALLY_REUSABLE. Pragmas are processed at compile time. you must supply a literal number for the second argument of the pragma call. the maximum number of work areas needed for a package is only as many as there are concurrent users of the package. W hile this is an important feature of PL/SQL packages (persistent.

.print_pkg. END sr_pkg.Global variable PROCEDURE print_pkg.init_pkg(4). we enable output from SQL*Plus: SQLPLUS> set serveroutput on.Initialize package state PROCEDURE init_pkg (n NUMBER) IS BEGIN sr_pkg. -. -. we initialize the package with a value of 4 and then display the package c ontents -all within a single PL/SQL block: SQLPLUS> begin -. END.PUT_LINE ('num: ' || sr_pkg.We should see that the package state is reset to the -. as is shown in the following steps: SQLPLUS> begin -. -. -.initial (default) values. SR_PKG. If we had placed the call to SR_PKG.eaning of a program. Next.the body is required to have the pragma since the -.print_pkg. SR_PKG.Print package state PROCEDURE print_pkg IS BEGIN DBMS_OUTPUT.We should see the new values. --initialise global variable END. end. / Statement processed.print_pk g inside the same PL/SQL block. that package variable would lose its setting.Print it in the same call to the server.initialize and print the package SR_PKG. -. however. num: 4 And we see that initial value of 4. num NUMBER := 0. they simply convey information to the compiler. PROCEDURE init_pkg (n NUMBER).num := n.num). END sr_pkg.specification of this package has the pragma PRAGMA SERIALLY_REUSABLE. 16 First. Example The following example shows how global variables in a "serially reusable" packag e behave across call boundaries: CREATE OR REPLACE PACKAGE sr_pkg ISPRAGMA SERIALLY_REUSABLE. /CREATE OR REPLACE PACKAGE BODY sr_pkg IS-.

This asserts that the function reads no database state (does not query da tabase tables). a of . Usage Notes You can declare the pragma RESTRICT_REFERENCES only in a package spec or object type spec. UPDATE. num: 0 PRAGMA RESRTICTED_REFERENCE When a stand alone user defined function is used in a SQL statement. A RESTRICT_REFERENCES pragma can apply to only one function declaration. No constraint implies another. RESTRICT_REFERENCES Pragma Syntax RESTRICT_REFERENCESÄÄ function_name <WNDS> RNDS . RNPS . The purity of the package is denoted with the PL/SQL 17 pragma: RESTRICT_REFERENCES. RNPS. the function body is not checked for violations of the constraints listed in the pragma. You can still declare the pragma for individual functions. If you specify DEFAULT instead of a function name. So. WNPS) To ca ll the function from parallel queries. that is. The optional arguments define the level of the purity the function.end. This pragma has one mandatory argument and three optional arguments. You do not hav e to execute the specific path for the statement to fail. you must specify all four constraints. the execution will fail. You can specify up to four constraints (RNDS. WNDS.This asserts that the function reads no package state (does not reference the values of packaged variables) WNPS . If the code contains ANY ta ble modification SQL. the functions in the packag e do not modify the database. TRUST . in the latter case. Such pragmas override the default pragma. If the S QL parse identifies an INSERT.This asserts that the function writes no package state (does not change t he values of packaged variables).This asserts that the function can be trusted not to violate one or more rules. The function is trusted not to violate them. there is no guarantee that the function does not update the database. you will know it. If user defined functions are used to any extent it is advisable to encapsulate them within a package and designate this package as pure . This guarantee can be made by: Inspecting the code. For example. or DELETE. / Statement processed.This asserts that the function writes no database state (does not modify database tables). WNDS . t he system-defined constructor). WNPS does not imply RNPS. the pragma applies to all fun ctions in the package spec or object type spec (including. or by Executing a SQL statement with the function. When you specify TRUST.

WNDS). end emp_pkg. -.returns the ROUNDED salary of the employee pragma restrict_references(round_salary. -. The following code segment illustrates a package that provides procedures and fu nctions in employee data and uses the pragma RESTRICT_REFERENCES.pragma that references the name of overloaded functions always applies to the ne arest foregoing function declaration. ==================================THE END==================== .emp%name. create or replace package emp_pkg is function highest_paid_employee return emp.returns the highest paid employee name function round_salary(n varchar2) return number.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->