IF(:old.actDepartDateTime IS NULL)然后

时间:2019-05-25 09:08:01

标签: oracle plsql database-trigger

如果机票与尚未起飞的航班相对应,

备份任何已删除机票的航班ID,机票号和座位号。

创建序列generateKey     从100开始     增加1;

CREATE TABLE backUpTicket
(
cancelledKey NUMBER NOT NULL,
flightID char(9) NOT NULL,
ticketNum varchar2(5) NOT NULL,
seatNum NUMBER(3) NOT NULL,
PRIMARY KEY(cancelledKey)
);

CREATE OR REPLACE TRIGGER backUpTicketCancelled
BEFORE DELETE ON FLIGHT
FOR EACH ROW

BEGIN
    INSERT INTO backUpTicket
    VALUES
    (generateKey.nextVal, :old.flightID, :old.TicketNum, :old.seatNum);

    IF(:old.actDepartDateTime IS NULL) THEN
        DBMS_OUTPUT.PUT_LINE('The Flight has not departed yet');
    END IF;

END backUpTicket;
/       

作为错误的绑定运算符出现在IF(:old.actDepartDateTime IS NULL)中的错误

2 个答案:

答案 0 :(得分:3)

首先,您的触发器应该在TICKET表上,而不是FLIGHT表上。其次,如果航班已经起飞,则不应该创建机票的备份。您的代码始终创建备份。我建议以下方法可能会更好:

CREATE OR REPLACE TRIGGER backUpTicketCancelled
  BEFORE DELETE ON TICKET
  FOR EACH ROW
DECLARE
  rowFlight  FLIGHT%ROWTYPE;
BEGIN
  SELECT *
    INTO rowFlight
    FROM FLIGHT f
    WHERE f.FLIGHTID = :OLD.FLIGHTID;

  IF rowFlight.actDepartDateTime IS NULL THEN
    INSERT INTO backUpTicket
        (CANCELLEDKEY,        FLIGHTID,      TICKETNUM,      SEATNUM)
      VALUES
        (generateKey.nextVal, :old.flightID, :old.TicketNum, :old.seatNum);
  ELSE
    DBMS_OUTPUT.PUT_LINE(TO_CHAR(SYSDATE', 'DD-MON-YYYY HH24:MI:SS') || ': ' ||
        'Flight ' || :OLD.FLIGHTID || ' departed at ' ||
        TO_CHAR(rowFlight.actDepartDateTime, 'DD-MON-YYYY HH24:MI:SS' ||
        ' so ticket #' || :OLD.TICKETNUM || ' for seat #' || :OLD.SEATNUM ||
        ' is not eligible for backup');
  END IF;    
END backUpTicket;

此外,备份表的名称应该是BACKUP_TICKET而不是backUpTicket,因此您可能会为此而被打下标签。

此外,尽管我意识到这是一项家庭作业,但这是一个非常糟糕的设计选择。您应该创建一个执行删除票证所需的所有操作的过程,而不是在执行这种业务决策的触发器中埋入“魔术代码”。

好运。

答案 1 :(得分:1)

根据您的评论,触发器在表TICKET上,但列actDepartDateTime在表FLIGHT中。您只能使用:OLD:NEW访问触发器表中的列。要获取actDepartDateTime,您需要使用FLIGHT值从:OLD.flightID表中进行选择。