Commit in trigger : Trigger and Transaction « Trigger « Oracle PL/SQL Tutorial

Oracle PL/SQL Tutorial
1. Introduction
2. Query Select
3. Set
4. Insert Update Delete
5. Sequences
6. Table
7. Table Joins
8. View
9. Index
10. SQL Data Types
11. Character String Functions
12. Aggregate Functions
13. Date Timestamp Functions
14. Numerical Math Functions
15. Conversion Functions
16. Analytical Functions
17. Miscellaneous Functions
18. Regular Expressions Functions
19. Statistical Functions
20. Linear Regression Functions
21. PL SQL Data Types
22. PL SQL Statements
23. PL SQL Operators
24. PL SQL Programming
25. Cursor
26. Collections
27. Function Procedure Packages
28. Trigger
29. SQL PLUS Session Environment
30. System Tables Data Dictionary
31. System Packages
32. Object Oriented
33. XML
34. Large Objects
35. Transaction
36. User Privilege
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Oracle PL/SQL Tutorial » Trigger » Trigger and Transaction 
28. 19. 3. Commit in trigger
SQL>
SQL> CREATE TABLE employee_compensation (
  2     company VARCHAR2(100),
  3     NAME VARCHAR2(100),
  4     compensation NUMBER);

Table created.

SQL>
SQL> CREATE TABLE employee_history (
  2     NAME VARCHAR2(100),
  3     description VARCHAR2(255),
  4     occurred_on DATE);

Table created.

SQL>
SQL> CREATE OR REPLACE TRIGGER bef_ins_ceo_comp
  2     BEFORE INSERT
  3     ON employee_compensation
  4     FOR EACH ROW
  5  DECLARE
  6     PRAGMA AUTONOMOUS_TRANSACTION;
  7  BEGIN
  8     INSERT INTO employee_history
  9          VALUES (:NEW.NAME, 'BEFORE INSERT', SYSDATE);
 10
 11     COMMIT;
 12  END;
 13  /

Trigger created.

SQL> CREATE OR REPLACE TRIGGER aft_ins_ceo_comp
  2     AFTER INSERT
  3     ON employee_compensation
  4     FOR EACH ROW
  5  DECLARE
  6     PRAGMA AUTONOMOUS_TRANSACTION;
  7  BEGIN
  8     IF :NEW.compensation > 1000000000
  9     THEN
 10        RAISE VALUE_ERROR;
 11     ELSE
 12        INSERT INTO employee_history VALUES (:NEW.NAME, 'AFTER INSERT', SYSDATE);
 13        COMMIT;
 14     END IF;
 15  EXCEPTION
 16     WHEN OTHERS
 17     THEN
 18        ROLLBACK;
 19        RAISE;
 20  END;
 21  /

Trigger created.

SQL> COLUMN name FORMAT a20
SQL> COLUMN description FORMAT a30
SQL>
SQL> SELECT NAME, description
  2       , TO_CHAR (occurred_on, 'MM/DD/YYYY HH:MI:SS') occurred_on
  3    FROM employee_history;

no rows selected

SQL>
SQL> BEGIN
  2     INSERT INTO employee_compensation VALUES ('B''J'9100000);
  3
  4     INSERT INTO employee_compensation VALUES ('B''A'10700000);
  5
  6     INSERT INTO employee_compensation VALUES ('B''Sally Bigdeal', 1000000001);
  7
  8  END;
  9  /
BEGIN
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "JAVA2S.AFT_INS_CEO_COMP", line 15
ORA-04088: error during execution of trigger 'JAVA2S.AFT_INS_CEO_COMP'
ORA-06512: at line 6


SQL>
SQL> SELECT   NAME, description
  2         , TO_CHAR (occurred_on, 'MM/DD/YYYY HH:MI:SS') occurred_on
  3      FROM employee_history
  4  ORDER BY occurred_on;

NAME                 DESCRIPTION                    OCCURRED_ON
-------------------- ------------------------------ -------------------
J                    BEFORE INSERT                  07/24/2008 08:03:16
J                    AFTER INSERT                   07/24/2008 08:03:16
Sally Bigdeal        BEFORE INSERT                  07/24/2008 08:03:16
A                    AFTER INSERT                   07/24/2008 08:03:16
A                    BEFORE INSERT                  07/24/2008 08:03:16

SQL>
SQL>
SQL> DROP TABLE employee_compensation;

Table dropped.

SQL>
SQL> DROP TABLE employee_history;

Table dropped.

SQL>
28. 19. Trigger and Transaction
28. 19. 1. Autonomous triggers
28. 19. 2. Mark trigger with PRAGMA AUTONOMOUS_TRANSACTION
28. 19. 3. Commit in trigger
28. 19. 4. Call PRAGMA AUTONOMOUS_TRANSACTION procedure
28. 19. 5. SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
28. 19. 6. Transaction with 'pragma autonomous_transaction'
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.