Using date in a check constraint, Oracle -
i trying check add following constraint oracle returns error shown below.
alter table table1 add (constraint gt_table1_closedate check (closedate > sysdate), constraint lt_table1_closedate check (closedate <= sysdate + 365)), constraint gt_table1_startdate check (startdate > (closedate + (sysdate + 730))));
error:
error report: sql error: ora-02436: date or system variable wrongly specified in check constraint 02436. 00000 - "date or system variable wrongly specified in check constraint" *cause: attempt made use date constant or system variable, such user, in check constraint not specified in create table or alter table statement. example, date specified without century. *action: specify date constant or system variable. setting event 10149 allows constraints "a1 > '10-may-96'", bug permitted created before version 8.
a check constraint, unfortunately, cannot reference function sysdate. need create trigger checked these values when dml occurs, i.e.
create or replace trigger trg_check_dates before insert or update on table1 each row begin if( :new.closedate <= sysdate ) raise_application_error( -20001, 'invalid closedate: closedate must greater current date - value = ' || to_char( :new.closedate, 'yyyy-mm-dd hh24:mi:ss' ) ); end if; if( :new.closedate > add_months(sysdate,12) ) raise_application_error( -20002, 'invalid closedate: closedate must within next year - value = ' || to_char( :new.closedate, 'yyyy-mm-dd hh24:mi:ss' ) ); end if; if( :new.startdate <= add_months(:new.closedate,24) ) raise_application_error( -20002, 'invalid startdate: startdate must within 24 months of closedate - startdate = ' || to_char( :new.startdate, 'yyyy-mm-dd hh24:mi:ss' ) || ' closedate = ' || to_char( :new.closedate , 'yyyy-mm-dd hh24:mi:ss' ) ); end if; end;
Comments
Post a Comment