Results 1 to 4 of 4
  1. #1
    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.

  2. #2
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,006
    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).

  3. #3
    Join Date
    Nov 2012
    Posts
    2

    .

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


    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?

  4. #4
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •