使用PL / SQL块创建触发器时出错

问题描述 投票:1回答:1

问题: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 CX­7520 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
oracle plsql database-trigger
1个回答
2
投票

为了能够在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 CX­7520 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 CX­7520 can be scheduled only on Tuesday, Friday and Sunday.'); END IF; END; /
    使用NLS_DATE_LANGUAGE选项的使用日缩写。否则,您会遇到意外情况。
  • 显示的错误源于在weekday函数中使用EXTRACT()的情况,其中对于日期类型变量使用daymonthyear,也允许使用hourminutesecond。 。等等。如果是日期时间类型变量,则允许,但
  • not
weekday
© www.soinside.com 2019 - 2024. All rights reserved.