简体   繁体   中英

Oracle SQL (trigger)

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM