If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > Help with Triggers

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Nov 2012
Posts: 2
Help with Triggers

I'm trying to create a trigger to prevent inserts/updates on a table where the booking date is the same as the current date.

CREATE OR REPLACE TRIGGER driver_bookings BEFORE INSERT OR UPDATE ON Bookings
FOR EACH ROW WHEN (new.DriverID = old.DriverID)
BEGIN
IF TO_DATE(:new.StartDate, 'DD.MM.YYYY') = TO_DATE(sysdate,'DD.MM.YYYY');

raise_application_error(-20014, 'Booking cannot be made at this time.');
END IF;

END;
/



The table has (BookingID, BookingDate, StartDate).

To check that the trigger fires i am inserting this value into the table:

INSERT INTO Bookings VALUES (6,TO_DATE('9.11.2012','DD.MM.YYYY'), TO_DATE('09.11.2012/20:12','DD.MM.YYYY/HH24:MI');

For some reason the trigger allows the insert when it shouldnt and i have no idea whats wrong.

any help would be appreciated.
Reply With Quote
  #2 (permalink)  
Old
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 4,003
If STARTDATE is a DATE, then you don't TO_DATE it.

SYSDATE is a function that returns DATE, so you don't TO_DATE it either. However, you might want to TRUNC it (to remove time portion).
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Nov 2012
Posts: 2
.

I changed the TO_DATE to Trunc and no luck. It's still accepting the insert.


Quote:
CREATE OR REPLACE TRIGGER driver_bookings BEFORE INSERT OR UPDATE ON Bookings
FOR EACH ROW WHEN (new.DriverID = old.DriverID)
BEGIN
IF TRUNC(:new.starttime) = TRUNC(sysdate) THEN

raise_application_error(-20014, 'Booking cannot be made at this time');
END IF;

END;
/
Just to confirm, my tigger is only checking rows where the ID in the insert statement matches an ID in the table, right?
Reply With Quote
  #4 (permalink)  
Old
Registered User
 
Join Date: Oct 2002
Location: Cape Town, South Africa
Posts: 251
The trigger would surely not fire for inserts because there is no old value at that point. The logic should work just fine for updates though. Just to clarify the trigger is only executing when the DRIVERID in the table is the same as the proposed DRIVERID.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On