React to Inserting and Updating actions : Inserting « 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 » Inserting 
28. 16. 2. React to Inserting and Updating actions
SQL>
SQL> create table ord
  2          (
  3           order_no               integer         primary key
  4          ,empl_no                integer
  5          ,order_date             date not null
  6          ,total_order_price      number(7,2)
  7          ,deliver_date           date
  8          ,deliver_time           varchar2(7)
  9          ,payment_method         varchar2(2)
 10          ,emp_no                 number(3,0)
 11          ,deliver_name           varchar2(35)
 12          ,gift_message           varchar2(100)
 13           );

Table created.

SQL>
SQL>
SQL>
SQL> insert into ord(order_no,empl_no,order_date,total_order_price,deliver_date,deliver_time,payment_method,emp_no,deliver_name,gift_message)
  2  values(1,1,add_months(sysdate, -1)235.00'14-Feb-1999', '12 noon', 'CA',1, null, 'Gift for wife');

row created.

SQL>
SQL> insert into ord(order_no  ,empl_no ,order_date ,total_order_price ,deliver_date ,deliver_time ,payment_method ,emp_no,deliver_name ,gift_message )
  2  values(2,1,add_months(sysdate, -2)50.98'14-feb-1999', '1 pm', 'CA',7'Rose', 'Happy Valentines Day to Mother');

row created.

SQL>
SQL> insert into ord(order_no  ,empl_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
  2  values(32,add_months(sysdate, -3)35.99'14-feb-1999', '1 pm', 'VS',2'Ruby', 'Happy Valentines Day to Mother');

row created.

SQL>
SQL> insert into ord(order_no  ,empl_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
  2  values(42,add_months(sysdate, -4)19.95'14-feb-1999', '5 pm', 'CA',2'Coy', 'Happy Valentines Day to You');

row created.

SQL>
SQL> insert into ord(order_no  ,empl_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
  2  values(79,add_months(sysdate, -7)35.95'21-jun-1999', '12 noon', 'VS', 2'Fill', 'Happy Birthday from Joe');

row created.

SQL>
SQL> insert into ord(order_no  ,empl_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
  2  values (812, add_months(sysdate, -8)35.95'1-jan-2000', '12 noon', 'DI',3'Laura', 'Happy New Year''s from Lawrence');

row created.

SQL>
SQL> insert into ord(order_no  ,empl_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
  2  values (912, add_months(sysdate, -9)75.95'2-jan-2000', '12 noon', 'CA',7'Sara', 'Happy Birthday from Lawrence' );

row created.

SQL>
SQL> insert into ord(order_no  ,empl_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
  2  values(104, add_months(sysdate, -10)19.95, sysdate, '2:30 pm', 'VG',2'OK', 'Happy Valentines Day to You');

row created.

SQL>
SQL> insert into ord(order_no  ,empl_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
  2  values(112, add_months(sysdate, -11)30.00, sysdate+2'1:30 pm', 'VG',2'Hi', 'Happy Birthday Day to You');

row created.

SQL>
SQL> insert into ord(order_no  ,empl_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message)
  2  values(127, add_months(sysdate, -12)21.95, sysdate-2'3:30 pm', 'CA',2'Jack', 'Happy Birthday Day to You');

row created.

SQL>
SQL> insert into ord(order_no  ,empl_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message)
  2  values(137, add_months(sysdate, -1)21.95, sysdate, '3:30 pm', 'CA',2'Jay', 'Thanks for giving 100%!');

row created.

SQL>
SQL>
SQL>
SQL>
SQL> create table ord_item
  2          (
  3           order_no               integer
  4          ,product_id             integer
  5          ,quantity               number(4,0)
  6          ,item_price                 number(7,2)
  7        ,total_order_item_price   number(9,2)
  8          ,primary key (order_no ,product_id)
  9          );

Table created.

SQL>
SQL>
SQL>
SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(1,   2,  1023.00 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(21123.00 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(25110.50 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(28117.48 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(38135.99 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(47119.95 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(55110.95 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(68122.95 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(71615.00 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(75110.50 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(78110.45 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(88135.95 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(98165.45 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(95110.50 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(103119.95 );

row created.

SQL> insert into ord_item (order_no, product_id, quantity, item_pricevalues(118130.00);

row created.

SQL>
SQL> create or replace trigger tr_total_order_price
  2  before
  3  update of item_price or insert or delete
  4  on ord_item
  5  for each row
  6  declare
  7      v_change_in_price  number;
  8      v_order_no number;
  9  begin
 10
 11   if inserting then
 12      :new.total_order_item_price := :new.item_price * :new.quantity ;
 13
 14      v_change_in_price := :new.item_price * :new.quantity;
 15
 16      v_order_no := :new.order_no;
 17
 18      dbms_output.put_line('inserting'||:new.total_order_item_price);
 19
 20   elsif updating then
 21
 22      :new.total_order_item_price := :new.item_price * :new.quantity ;
 23
 24      v_change_in_price := (:new.item_price * :new.quantity(:old.item_price * :old.quantity;
 25
 26      v_order_no := :old.order_no;
 27
 28      dbms_output.put_line('updating');
 29
 30   else
 31
 32      v_change_in_price := (:old.item_price * :old.quantity)  * -1;
 33
 34      v_order_no := :old.order_no;
 35
 36      dbms_output.put_line('deleting');
 37
 38   end if;
 39
 40   update ord
 41      set total_order_price = total_order_price + v_change_in_price
 42      where order_no = v_order_no;
 43
 44  end;
 45  /

Trigger created.

SQL> show errors
No errors.
SQL>
SQL> select total_order_price from ord where order_no = 7;

TOTAL_ORDER_PRICE
-----------------
            35.95

SQL> select total_order_item_price from ord_item
  2   where order_no = and product_id = 6;

no rows selected

SQL>
SQL> insert into ord_item (order_no, product_id, quantity, item_price)
  2    values (7625;

row created.

SQL> update ord_item set item_price = 25 where order_no = and product_id = ;

row updated.

SQL> select total_order_price from ord where order_no = 7;

TOTAL_ORDER_PRICE
-----------------
            60.45

SQL> select total_order_item_price from ord_item
  2   where order_no = and product_id = 6;

TOTAL_ORDER_ITEM_PRICE
----------------------
                    10

SQL>
SQL>
SQL> drop table ord;

Table dropped.

SQL>
SQL> drop table ord_item;

Table dropped.
28. 16. Inserting
28. 16. 1. Trigger that logs actions for inserting actions
28. 16. 2. React to Inserting and Updating actions
28. 16. 3. If new value is null use the value from a sequence
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.