我創建了一個觸發器,以便在插入或更新預訂記錄時,如果評估爲0,則將預訂的詳細信息插入到審計表中。下面的觸發器完美的作品,如果我更新記錄,但是在插入它給了我下面的錯誤:ORACLE SQL觸發器 - 未找到數據
INSERT INTO "SCOTT"."BOOKING" (PASSENGER_PASSENGER_ID, VOYAGE_VOYAGE_ID, CABIN_NUM, CLASS, EVALUATION) VALUES ('2', '1', '202', 'SECOND', '0')
ORA-01403: no data found
ORA-01403: no data found
ORA-06512: at "SCOTT.EVALUATION_TRIG", line 8
ORA-04088: error during execution of trigger 'SCOTT.EVALUATION_TRIG'
ORA-06512: at line 1
One error saving changes to table "SCOTT"."BOOKING":
Row 2: ORA-01403: no data found
ORA-01403: no data found
ORA-06512: at "SCOTT.EVALUATION_TRIG", line 8
ORA-04088: error during execution of trigger 'SCOTT.EVALUATION_TRIG'
ORA-06512: at line 1
Oracle的SQL觸發代碼:
create or replace trigger EVALUATION_TRIG
AFTER INSERT OR UPDATE
ON BOOKING
FOR EACH ROW
WHEN (NEW.EVALUATION =0)
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
PASSENGER_NAME VARCHAR2(30);
CRUISE_NAME VARCHAR2(30);
VOYAGE_DATE DATE;
SHIP_NAME1 VARCHAR2(30);
BEGIN
SELECT NAME INTO PASSENGER_NAME FROM PASSENGER JOIN BOOKING
ON PASSENGER.PASSENGER_ID = BOOKING.PASSENGER_PASSENGER_ID
WHERE BOOKING.PASSENGER_PASSENGER_ID = :NEW.PASSENGER_PASSENGER_ID
AND ROWNUM = 1;
SELECT NAME INTO CRUISE_NAME FROM CRUISE JOIN VOYAGE
ON CRUISE.CRUISE_ID = VOYAGE.CRUISE_CRUISE_ID
JOIN BOOKING ON
VOYAGE.VOYAGE_ID = BOOKING.VOYAGE_VOYAGE_ID
WHERE BOOKING.VOYAGE_VOYAGE_ID = :NEW.VOYAGE_VOYAGE_ID
AND ROWNUM= 1;
SELECT START_DATE INTO VOYAGE_DATE FROM VOYAGE JOIN BOOKING
ON VOYAGE.VOYAGE_ID= BOOKING.VOYAGE_VOYAGE_ID
WHERE BOOKING.VOYAGE_VOYAGE_ID = :NEW.VOYAGE_VOYAGE_ID
AND ROWNUM = 1;
SELECT SHIP_NAME INTO SHIP_NAME1 FROM SHIP JOIN VOYAGE
ON SHIP.SHIP_ID = VOYAGE.SHIP_SHIP_ID JOIN BOOKING
ON VOYAGE.VOYAGE_ID= BOOKING.VOYAGE_VOYAGE_ID
WHERE BOOKING.VOYAGE_VOYAGE_ID = :NEW.VOYAGE_VOYAGE_ID
AND ROWNUM = 1;
INSERT INTO EVALUATION_AUDIT VALUES (PASSENGER_NAME, CRUISE_NAME,VOYAGE_DATE, SHIP_NAME1,:NEW.EVALUATION);
COMMIT;
END;
你怎麼讀這樣一個龐大的代碼塊? – GolezTrol 2015-04-04 16:54:43
我知道發佈大型代碼並不理想,但由於顯而易見的原因,它是一個相當大的觸發器。 – user2838928 2015-04-04 16:58:07
這不是因爲它很大,而是因爲它全部大寫,沒有空白,沒有評論,也沒有任何縮進。當然,你的選擇,但我個人覺得很難閱讀和調試。 – GolezTrol 2015-04-04 17:00:42