0
CREATE OR REPLACE TRIGGER Guest_Change_In_WorkingHour
       BEFORE INSERT OR UPDATE OR DELETE ON guest
    BEGIN
       IF  TO_NUMBER(TO_CHAR(SYSDATE,'hh24')) < 8 -- cant do anything before 8:00am
           OR TO_NUMBER(TO_CHAR(SYSDATE,'hh24')) >= 5
              -- changes must be made BEFORE 5:00pm
            OR TO_CHAR(SYSDATE,'dy') in ('sun','sat') THEN -- nothing on weekends
    RAISE_APPLICATION_ERROR (-20000, 'Satff changes only allowed during business hours.');
    END IF;
   END;
   /

This trigger for during working hours (8AM To 5PM) can perform insert, delete and update data. For my problem is when the time is 4.00AM still can insert, delete and update data. I found that this trigger ignore AM and PM. How to solve?

1 Answer 1

2

You're using time format HH24, which is 24-hour ('military' in the US, I believe) time. You therefore need to do this:

TO_NUMBER(TO_CHAR(SYSDATE,'hh24')) >= 17

... rather than use 5.

But as it is, it should be raising the error at any time, not just outside the 08:00-17:00 range (since very few numbers are both >= 8 and < 5). Does the trigger actually compile?

Also, you need to check the case you're using to compare the day of the week too, as I don't think that will match at the moment (correction - yes it will, with dy); but relying on NLS-sensitive values for comparison is not ideal.

Sign up to request clarification or add additional context in comments.

3 Comments

@LONGKIT - I've tested it, and with 17, this trigger does what you want; did you change anything else? If you try it again as it is now with just that change to 17, what happens?
i change to 17 and then i also change my pc time to 4.00Am, but still can update record.
Did you verify that the database also thinks SYSDATE is 04:00, in the same session that you did the update? Is the database on your PC, or on a server somewhere?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.