2017-05-07 19 views
0

我的數據庫是酒店預訂系統的數據庫。根據當前日期是否落在房間的check_in和check_out值之間,我的觸發器會在進行任何新預留之前更改房間的可用狀態。觸發錯誤:ORA-01403插入時找不到數據

我試圖用insert語句觸發觸發器時發現錯誤沒有數據。我希望對此問題提供任何幫助。

錯誤:

SQL Error: ORA-01403: no data found ORA-06512: at "USER.ROOM_STATUS", line 5 ORA-04088 error during execution of trigger 'USER.ROOM_STATUS' - no data found *Cause: No data was found from the objects

觸發:

create or replace trigger room_status 
    before insert on reservation 
    for each row 
declare 
    status_in room.status%type; 
    error1 exception; 
begin 
    select status into status_in 
    from room 
    where room.room_id = :old.room_id; 
    if sysdate between :old.check_in and :old.check_out then 
    update room 
    set status = 'F' 
    where room_id = :old.room_id; 
    else 
    update room 
    set status = 'T' 
    where room_id = :old.room_id; 
    end if; 
exception 
    when error1 then 
    raise_application_error(-20100,'Insert Cancelled'); 
end; 

插入語句:

insert into reservation 
values(reservation_sequence.nextval,to_date('05-03-2017','mm/dd/yyyy'), 
to_date('05-07-2017','mm/dd/yyyy'), 116, 170); 

表:

create table roomType 
(room_type varchar2(20) constraint roomType_pk primary key, 
room_rate number(4)); 

create table room 
(room_id number(3) constraint room_pk primary key, 
room_type varchar2(15) constraint room_fk references roomType, 
status char(1)); 

create table guest 
(guest_id varchar2(5) constraint guest_pk primary key, 
first_name varchar2(20), 
last_name varchar2(20), 
email varchar2(30), 
phone varchar2(10)); 

create table reservation 
(reservation_id number(6) constraint reservation_pk primary key, 
check_in date, 
check_out date, 
room_id number(3), 
guest_id varchar2(5), 
foreign key (room_id) references room(room_id), 
foreign key (guest_id) references guest(guest_id)); 

回答

2

當SELECT語句找不到數據時,Oracle會拋出NO_DATA_FOUND錯誤。所以,看看你的觸發器,明顯的解釋可能是ROOM表中沒有數據。

然而,在你的觸發代碼個微妙的問題:應用DML之前

select status into status_in 
from room 
where room.room_id = :old.room_id; 

:old命名空間引用列的價值。但是您的DML語句是insert,所以:old.room_id的值爲空,因爲沒有先前的記錄。任何= null總是錯誤的,所以選擇無法找到任何東西。

您需要做的是參考:new值。

select status into status_in 
from room 
where room.room_id = :new.room_id; 

:new適用於INSERT和UPDATE語句,:old適用於UPDATE和DELETE語句。


順便說一句,你已經定義了一個例外error1,但你不養它的任何地方,這樣的異常處理程序將不會執行。