2014-03-31 83 views
0
create or replace trigger enroll_bef_ins_row 
before insert on enrollments 
for each row 
declare 
original   number; 
seatsremain_already_full exception; 
begin 

Select seatsremain into original from offering where offerno= :new.offerno; 
if original > 0 then 
    update offering set seatsremain= seatsremain - 1; 
    dmbs_output.put_line ('Seats available in offering' |offerno| 'have decreased from' |:old.seatsremain| to |:new.seatsremain|); 
else if original = 0 then 
    dbms_output.put_line ('Offering' |offerno| 'is already full!'); 
    raise seatsremain_already_full 
end if; 
exception 
when seatsremain_already_full 
    raise_application_error (-20001, 'Cannot allow insertion'); 
end 
/

我不斷收到消息,每次我試圖在插入值時間「與編譯錯誤創建觸發器」我得到一個ORA 04098 - SYSTEM.ENROLL_BEFORE_INS_ROW無效和失敗的重新生效消息。PL/SQL觸發器編譯錯誤

我的任務是寫之前行被插入到入學表執行以下任務觸發:

如果座椅是可用於特定產品,觸發應自動減少座位數爲提供並顯示消息:提供的座位(提供號碼)從(插入前可用座位的數量)減少到(插入後可用座位的數量)。如果插入後可用座位的數量等於0,則顯示消息:'沒有更多座位(此處應輸入產品編號)

如果某個特定產品的座位在插入之前等於0在入學的行,觸發器應顯示如下:

插入不允許

,並使用RAISE_APPLICATION_ERROR過程,以防止INSERT語句

+0

使用'show error'查看「編譯出錯後創建」後的確切錯誤消息 –

回答

0

連接操作符是||的執行,所有報表必須結束無線這是一個分號。這裏和那裏還有一些奇怪的撇號。

試試這個:

create or replace trigger enroll_bef_ins_row 
    before insert on enrollments 
    for each row 
declare 
    original     number; 
    seatsremain_already_full exception; 
begin 
    Select seatsremain 
    into original 
    from offering 
    where offerno = :new.offerno; 

    if original > 0 then 
    update offering 
     set seatsremain = seatsremain - 1 
     WHERE OFFERNO = :new.OFFERNO; 

    dmbs_output.put_line ('Seats available in offering' || :new.offerno || 
          'have decreased from' || original || 
          ' to ' || original-1); 
    elsif original = 0 then 
    dbms_output.put_line ('Offering' || offerno || 
          ' is already full!'); 
    raise seatsremain_already_full; 
    end if; 
exception 
    when seatsremain_already_full 
    raise_application_error (-20001, 'Cannot allow insertion'); 
end; 

我建議你可能想不會遞減SEATSREMAIN而是計算的採取OFFERING.SEATSAVAILABLE並從其減去所採取的席位和剩餘座位數量 - 否則有一場比賽在那裏的OFFERING.SEATSREMAIN條件,你可以最終超量預訂 - 但這可能適用於簡單的情況。另外,如果SEATSREMAIN結果爲負值,您可能會引發錯誤。

分享和享受。