问题:fl_schedule(flno,出发,dtime,到达,atime,价格)。
仅在星期二,星期五和星期日安排航班号CX Tuesday7520时,创建触发器以允许插入或更新。
CREATE OR REPLACE TRIGGER flightsch_day
BEFORE INSERT OR UPDATE ON fl_schedule
FOR EACH ROW
WHEN (NEW.flno LIKE 'CX7520')
DECLARE
day NUMBER;
BEGIN
day:=EXTRACT(weekday FROM :NEW.departs);
IF day NOT IN(0,2,5) THEN
RAISE_APPLICATION_ERROR(-20000,'Flight number CX7520 can be scheduled only on Tuesday, Friday and Sunday.');
END IF;
END;
/
错误:
SQL> SHOW ERRORS;
Errors for TRIGGER FLIGHTSCH_DAY:
LINE/COL ERROR
-------- -----------------------------------------------------------------
4/10 PLS-00122: FROM as separator is allowed only with specific
built-in functions
为了能够在WHEN (NEW.flno = 'CX7520')
之后使用FOR EACH ROW
,单个列UPDATE OF flno
需要flno
CREATE OR REPLACE TRIGGER flightsch_day
BEFORE INSERT OR UPDATE OF flno ON fl_schedule
FOR EACH ROW
WHEN (NEW.flno = 'CX7520')
DECLARE
BEGIN
IF TO_CHAR(:NEW.departs,'Dy','NLS_DATE_LANGUAGE=English') NOT IN ('Tue','Fri','Sun') THEN
RAISE_APPLICATION_ERROR(-20000,'Flight number CX7520 can be scheduled only on Tuesday, Friday and Sunday.');
END IF;
END;
/
<不带WHEN..
子句,将:NEW.flno = 'CX7520'
放入IF
语句:
CREATE OR REPLACE TRIGGER flightsch_day
BEFORE INSERT OR UPDATE ON fl_schedule
FOR EACH ROW
DECLARE
BEGIN
IF NOT ( TO_CHAR(:NEW.departs,'Dy','NLS_DATE_LANGUAGE=English') IN ('Tue','Fri','Sun')
AND :NEW.flno = 'CX7520' ) THEN
RAISE_APPLICATION_ERROR(-20000,'Flight number CX7520 can be scheduled only on Tuesday, Friday and Sunday.');
END IF;
END;
/
NLS_DATE_LANGUAGE
选项的使用日缩写。否则,您会遇到意外情况。weekday
函数中使用EXTRACT()
的情况,其中对于日期类型变量使用day
,month
,year
,也允许使用hour
,minute
,second
。 。等等。如果是日期时间类型变量,则允许,但weekday