How to create a trigger which does not allow user to update a table on a specific day (like tuesday)?

StackOverflow https://stackoverflow.com/questions/23616705

  •  21-07-2023
  •  | 
  •  

Question

I created this trigger on SQL command line of Oracle 10g and it got created successfully. It's Tuesday today and I tried to insert a row in my employee table. But instead of raising error, row got inserted.

create or replace trigger myt1 before insert or delete or update on employee
   begin
      if upper(to_char(sysdate,'DAY')) = 'TUESDAY' then
          raise_application_error(-20101,'TABLE IS SECURED');
      end if;
   end;

I logged in as hr while doing so in Oracle 10g. May be it doesn't affect who you are logged in as to raise error.

Please tell me why is trigger not raising application error and what should I do.

Was it helpful?

Solution

The DAY format mask returns a space-padded string. Assuming that your session's language is set to English, the longest day is WEDNESDAY which is 9 characters so all the other day strings would be padded out to 9 characters as well. This made sense long ago when reports were generally printed out in fixed width fonts so having functions return strings of consistent length simplified formatting issues.

You can remove the extra spaces by adding fm to your format mask

IF( to_char( sysdate, 'fmDAY' ) = 'TUESDAY' )

You can also use a format mask that produces the abbreviated day name which won't be space padded

IF( to_char( sysdate, 'DY' ) = 'TUE' )

Or, if you really wanted to, you could use the DAY format mask and compare against a space-padded string

IF( to_char( sysdate, 'DAY' ) = 'TUESDAY  ' )

If you are writing code that needs to deal with an environment where you need to support multiple languages, you'd need to account for the fact that weeks start on different days in different regions and the names and abbreviations of the days of the week are different in different languages. If you're in this situation, you'd need to pass in a third parameter to the to_char function to specify your NLS settings.

IF( to_char( sysdate, 'fmDAY', 'NLS_DATE_LANGUAGE=ENGLISH' ) = 'TUESDAY' )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top