Solved

Oracle: Trigger to capture table updates

Posted on 2006-06-22
2
389 Views
Last Modified: 2008-01-09
I'd like to write a trigger that will capture updates made to a certain column of a table, and place the old and new values into another table.

Cost_Table
Product:    Watch
Price:       200

If the price of the watch is changed to 300, a row should be inserted into Cost_Table_Updates as follows:
Product:   Watch
Old_Price: 200
New_Price: 300

Thanks!
0
Comment
Question by:TotalEnergy
2 Comments
 
LVL 25

Accepted Solution

by:
jrb1 earned 500 total points
ID: 16963748
create or replace trigger
   Cost_Table_Updates
after update on cost_table
for each row

BEGIN
   IF UPDATING ('PRICE') THEN
      INSERT INTO Cost_Table_Updates (product, old_price, new_price)
         VALUES (:new.product, :old.price, :new.price);
   END IF;
END;
/
0
 
LVL 3

Expert Comment

by:hardikbeit
ID: 16966514
Trigger:

Create or Replace Trigger Update_Cost_watch
AFTER update ON cost_table --Before or after as per your requirement
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW

BEGIN
  IF UPDATING THEN
    IF :NEW.PRICE=300 THEN
        INSERT INTO Cost_Table_Updates(product, old_price, new_price)
        VALUES (:new.product_id, :Old.Price,:NEW.Price);
    END IF;
  END IF;
END;

This helps you.

Regards, Hardik
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question