473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

Using PRAGMA - I

debasisdas
8,127 Expert 4TB
PRAGMA:-Signifies that the statement is a pragma (compiler directive). Pragmas are processed at compile time, not at run time. They pass information to the compiler.
A pragma is an instruction to the Oracle compiler that tells it to do something. In this case you are telling Oracle to associate an error that you choose to a variable that you choose. This pragma must go in the declarative section of your block.
Types of PRAGMA
1.AUTONOMOUS_TRANSACTION
2.EXCEPTION_INIT
3.RESTRICT_REFERENCES
4.SERIALLY_REUSABLE

The AUTONOMOUS_TRANSACTION pragma changes the way a subprogram works within a transaction. A subprogram marked with this pragma can do SQL operations and commit or roll back those operations, without committing or rolling back the data in the main transaction.

Just check this sample code
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE PROCEDURE DEPTINS
  2. (
  3. DNO DEPT.DEPTNO%TYPE,
  4. DN DEPT.DNAME%TYPE,
  5. LC DEPT.LOC%TYPE
  6. )
  7. AUTHID CURRENT_USER
  8. IS
  9. PRAGMA AUTONOMOUS_TRANSACTION;
  10. BEGIN
  11. INSERT INTO DEPT VALUES(DNO,DN,LC);
  12. DBMS_OUTPUT.PUT_LINE('ONE ROW INSERTED......!');
  13. COMMIT;
  14. EXCEPTION
  15. WHEN DUP_VAL_ON_INDEX THEN
  16. DBMS_OUTPUT.PUT_LINE('DUPLICATE VALUE......!');
  17. END;
  18.  
The AUTONOMOUS_TRANSACTION pragma instructs the plsql compiler to mark a routine as autonomous (independent).An a-t is an independent transaction started by another transaction,the main transaction.It lets the user suspend the main transaction and all sql operations,commit OR rollback those operations,then resume the main transaction.

RESTRICTIONS:-
Pragma can't be used to mark all subprograms in a package as autonomous.Only individual routines can be marked autonomous.It can be coded any where in the declaration section of the sub program.
Once started, an autonomous transaction is fully independent.It shares no locks,resources or commit dependencies with the main transaction.

ADVANTAGES:-
Unlike regular triggers autonomous triggers can contain COMMIT and ROLLBACK.

Please go through this sample trigger code.
Expand|Select|Wrap|Line Numbers
  1. create or replace trigger mytrig
  2. before insert on emp
  3. declare
  4. pragma autonomous_transaction;
  5. begin
  6. if to_char(sysdate,'d') in(1,7) then
  7. insert into  trace values(user,systimestamp);
  8. commit;
  9. Raise_application_error(-20004,'Cannot do manipulation today');
  10. end if;
  11. end;
  12.  
Also these can execute DDL statments,using native dynamic SQL.
Expand|Select|Wrap|Line Numbers
  1. create or replace trigger scotttrig
  2. after logon on scott.schema
  3. declare
  4. pragma autonomous_transaction;
  5. begin
  6. if to_char(sysdate,'dy') in('sun','sat') then
  7. --this is not supported in normal triggers without using PRAGMA.
  8. execute immediate 'create table logtable(id varchar2(10),dt date)';
  9. execute immediate 'insert into logtable values(user,sysdate)';
  10. commit;
  11. Raise_application_error(-20004,'Cannot do LOGIN today');
  12. end if;
  13. end;
  14.  
LIMITATIONS:-
Changes made by a-t become visible to other transaction when the a-t commits.The changes also become visible to the main transaction when it resumes.
If a-t attempts to access a resource held by th main transaction(which can't resume until the a-t routine exits),a deallock can occur.In that case,Oracle raises an exception in the a-t,If the user tries to exit an a-t without COMMIT OR ROLLBACK ,ORACLE RAISES AN EXCEPTION,in both the cases the transaction is rolled back if the exception goes unhandled.

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE PROCEDURE 
  2.    update_salary (dept_in IN NUMBER)
  3. IS
  4.    PRAGMA AUTONOMOUS_TRANSACTION;
  5.  
  6.    CURSOR myemps IS
  7.       SELECT empno FROM emp
  8.        WHERE deptno = dept_in
  9.          FOR UPDATE NOWAIT;
  10. BEGIN
  11.    FOR rec IN myemps
  12.    LOOP
  13.       UPDATE emp SET sal = sal * 2 
  14.        WHERE empno = rec.empno;
  15.    END LOOP;
  16.    COMMIT;
  17. END;
  18. ------------
  19. BEGIN
  20.    UPDATE emp SET sal = sal * 2;
  21.    update_salary (10);
  22. END;
  23.  
Expand|Select|Wrap|Line Numbers
  1. CREATE TRIGGER parts_trig
  2. BEFORE INSERT ON parts FOR EACH ROW
  3. DECLARE
  4. PRAGMA AUTONOMOUS_TRANSACTION;
  5. BEGIN
  6. INSERT INTO parts_log VALUES(:new.pnum, :new.pname);
  7. COMMIT;
  8. END;
  9.  

Also check Using PRAGMA - II
Feb 25 '08 #1
0 10659

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

Similar topics

6
by: Shri | last post by:
Can anybody tell me where i can find a detailed document on #pragma .... --shri
1
by: Gustavo L. Fabro | last post by:
Greetings! Going directly to the point: myclass.h: //-------------------------------------- #pragma managed //Forward declaration
15
by: muttaa | last post by:
Hello all, I'm a beginner in C...May i like to know the difference between a #pragma and a #define.... Also,yet i'm unclear what a pragma is all about as i can find topics on it only in...
10
by: =?Utf-8?B?TmFuZCBLaXNob3JlIEd1cHRh?= | last post by:
I have a binary file created using C++ that contains an object of the structure: struct student { int roll_no; char name; char qualification; };
5
by: venkat | last post by:
Hi, I have come across the a pragma definition " #pragma switch direct ". I don't know any thing about pragma's. Even i when i search through google not getting exact information. what does...
26
by: Rick | last post by:
I'm told that "#pragma once" has made it into the ISO standard for either C or C++. I can't find any reference to that anywhere. If it's true, do any of you have a reference I can use? ...
2
by: aleemakhtar1 | last post by:
wat is use of pragma directive in embedded sys ??
2
by: medrifter | last post by:
I have a program like this 1.Analyze.h as follows #if !defined(AFX_ANALYZE_H__550791A7_B441_4AD3_BCEB_4F391A71D410__INCLUDED_) #define...
4
by: dissectcode | last post by:
Hello - Is there a pragma directive that will make a function be ignored? I have a function that is not currently being used and it creates a warning. I would like to fix the warning by using pragma,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.