Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 23rd, 2005, 04:16 AM
prg18 prg18 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 13 prg18 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 55 sec
Reputation Power: 0
Question Problems with trigger

Hello. I'm having some trouble with a trigger. I have a load of tables which have the fields:

RecordId - NUMBER - Record Identifier
Created - TIMESTAMP DEFAULT CURRENT_TIMESTAMP - Date of creation
CreatedBy - VARCHAR2(6) - User that created record
LastUpdated - TIMESTAMP DEFAULT CURRENT_TIMESTAMP - Date of last update
LastUpdatedBy - VARCHAR2(6) - Last user to update

I've created a trigger to automatically fill in these values when an INSERT statement is executed but the values are not informed. The problem is with the UPDATE statements. I want the trigger to automatically set LastUpdatedBy to a default value and LastUpdated to the CURRENT_TIMESTAMP when these values are not set in the UPDATE statement. The code I had was:

Code:
CREATE OR REPLACE TRIGGER {TRIGGER NAME}
BEFORE INSERT OR UPDATE ON {TABLE NAME}
REFERENCING NEW AS n OLD AS o
FOR EACH ROW
BEGIN
IF INSERTING THEN
        IF (:n.RecordId IS NULL) THEN
            SELECT {SEQUENCE NAME}.nextVal INTO :n.RecordId FROM dual;
    	END IF;
	IF (:n.CreatedBy IS NULL) THEN
            :n.CreatedBy := '{NAME}';
    	END IF;
	IF (:n.LastUpdatedBy IS NULL) THEN
            :n.LastUpdatedBy := '{NAME}';
    	END IF;
    END IF;
    IF UPDATING THEN
	IF (:n.LastUpdatedBy IS NULL) THEN
            :n.LastUpdatedBy := '{NAME}';
    	END IF;
	IF (:n.LastUpdated IS NULL) THEN
            :n.LastUpdated := CURRENT_TIMESTAMP;
        END IF; 
    END IF;
END;


The problem is, of course, NEW references what the record will look like after the UPDATE statement, so LastUpdatedBy and LastUpdated are never updated because they were set on the INSERT statement. Is there any way to find out if the triggering statement is setting these fields? Or any way to read the triggering statement from within the trigger?

I'm open to suggestions. Thanks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Problems with trigger


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT