Skip to Main Content
  • Questions
  • Trigger to allow insertion only on Sunday

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Gyanaranjan.

Asked: August 07, 2016 - 9:58 am UTC

Last updated: August 09, 2016 - 2:46 am UTC

Version: 11g

Viewed 1000+ times

You Asked

I have employee table.
I want to create a trigger that will not allow insertion in the table on sunday.
Tell me the program please..

Thank you.

and Connor said...

Change the code to allow for the day you want...

SQL> create table t ( x int );

Table created.

SQL>
SQL> create or replace
  2  trigger trg
  3  before insert on t
  4  begin
  5    if to_char(sysdate,'DY') = 'MON' then
  6       raise_application_error(-20000,'Insertion only allowed on Sunday');
  7    end if;
  8  end;
  9  /

Trigger created.

SQL>
SQL> insert into t values (1);
insert into t values (1)
            *
ERROR at line 1:
ORA-20000: Insertion only allowed on Sunday
ORA-06512: at "MCDONAC.TRG", line 3
ORA-04088: error during execution of trigger 'MCDONAC.TRG'


Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

NICE AND USE FULL

Gyanaranjan Nayak, August 08, 2016 - 3:53 am UTC

Thanks a lot sir. You are superb!!

Specifying an nls_date_language would be better

William Robertson, August 08, 2016 - 4:18 pm UTC

Just to be safe, I'd also specify a language:
if to_char(sysdate,'DY', 'nls_date_language = English') = 'SUN'
then ...

Chris Saxon
August 09, 2016 - 2:46 am UTC

Good point.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library