Solved

Oracle: Trigger to capture table updates

Posted on 2006-06-22
2
385 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now