Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trigger comparing columns on UPDATE

Posted on 2007-11-15
14
Medium Priority
?
1,535 Views
Last Modified: 2013-12-19
Hi Experts -
I'm trying to write an UPDATE trigger for an Oracle database.  The goal is to check an existing date column to make sure the newly updated column is later.
Here's what I have:

CREATE or REPLACE TRIGGER trigger_name
BEFORE INSERT OR UPDATE date1 ON sometable1
FOR EACH ROW
BEGIN
  IF (_______date2 < _______date1) THEN
        RAISE_APPLICATION_ERROR(-20001, Date2 must be greater than Date1);
    END IF;
END;

When I do an insert with a date2 that is before date1, the trigger is not firing.  I don't know what to put in the underlined fields above (:new., :old, etc.).

Thanks for any insight you can provide.

chezbrgrs
0
Comment
Question by:chezbrgrs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
14 Comments
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20289618

CREATE or REPLACE TRIGGER trigger_name
BEFORE INSERT OR UPDATE date1 ON sometable1
FOR EACH ROW
BEGIN
IF (:new.date1 < :old.date1) THEN
   RAISE_APPLICATION_ERROR(-20001, New Date must be greater than Old Date); 
END IF; 
END;

Open in new window

0
 
LVL 12

Expert Comment

by:jwahl
ID: 20289683
do you have a NOT NULL constraint on date1?
if not, you have to compare with NVL() or do an additional check.


CREATE or REPLACE TRIGGER trigger_name
BEFORE INSERT OR UPDATE date1 ON sometable1
FOR EACH ROW
BEGIN
  IF :NEW.date1 < :OLD.date1 THEN
     RAISE_APPLICATION_ERROR(-20001, 'New Date must be greater than old Date!');
  END IF;
END;

Open in new window

0
 

Author Comment

by:chezbrgrs
ID: 20289705
Sorry, I wasn't clear.  The dates are in two different columns (startdate and enddate).  Wouldn't this solution just be looking at one column?
0
Industry Leaders: 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!

 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20289830
Then, you dont need a trigger at all. Just create a CHECK constraint on the table as below : -

ALTER TABLE sometable1 ADD CONSTRAINT someconstraint1 CHECK (start_date <= end_date) ENABLE VALIDATE;

Above would throw error if the existing data in the table does not follow the constraint rule. In that case, u may use the NOVALIDATE option to ignore validation of existing data.
0
 
LVL 12

Expert Comment

by:jwahl
ID: 20289837
in this case you also can define a check constraint:





ALTER TABLE my_table ADD 
CONSTRAINT date_test
 CHECK (startdate < enddate) ENABLE
 VALIDATE;

Open in new window

0
 
LVL 12

Expert Comment

by:jwahl
ID: 20289843
jinesh_kamdar is always faster ... ;-)
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20289895
@jwahl - Guess my TOAD works a bit faster ;-)
0
 
LVL 12

Expert Comment

by:jwahl
ID: 20289925
... i don't think it's only the TOAD  ... ;-)
0
 

Author Comment

by:chezbrgrs
ID: 20289993
The CHECK constraint would be much easier but I've been asked to use a TRIGGER and not alter the table.  Can it be done with a TRIGGER?
0
 
LVL 12

Accepted Solution

by:
jwahl earned 2000 total points
ID: 20290114
sure can it be done with trigger. is it possible to update startdate and enddate at the same time? can you update to NULL? if not, you can use


CREATE or REPLACE TRIGGER trigger_name
BEFORE INSERT OR UPDATE date1 ON sometable1
FOR EACH ROW
BEGIN
  IF :NEW.enddate < :OLD.startdate THEN
     RAISE_APPLICATION_ERROR(-20001, 'Enddate must be greater than Stardate!');
  END IF;
END;

Open in new window

0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20290157
I think the IF condition should be IF :NEW.enddate < :NEW.startdate THEN
0
 

Author Comment

by:chezbrgrs
ID: 20290166
jwahl -

Thanks I'll try this this evening and will let you know the results.  Thanks.

chezbrgrs
0
 
LVL 12

Expert Comment

by:jwahl
ID: 20290326
@jinesh_kamdar: you are right when the trigger is inserting then :OLD.startdate is null.

i think he should do different checks depending on insert/update:

...
   IF UPDATING THEN
       ....
   ELSIF INSERTING THEN
       ...
   END IF;
...
0
 

Author Comment

by:chezbrgrs
ID: 20294673
The :old value worked because startdate was not NULL.  Thanks for contributing.

chezbrgrs
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

721 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