2011-11-13 30 views
1

我正在編寫一個觸發器來檢查員工是否分配到兩個重疊的航班。oracle處理沒有來自查詢的數據

這將是這樣的:

select flight_id from flight 
where arrival_time > :new.departure_time 
and departure_time < :new.arrival_time; 

if flight_id is empty 
    [do nothing] 
if flight_id exists 
    [raise application error] 

誰能幫我在我怎麼會得有代碼的條件邏輯?這是我第一次和oracle一起工作(大學課程)。

回答

2

SELECT ... INTO是你的朋友。它會引發一個異常,你會抓住它,什麼也不做,否則會引發你自己的錯誤。

select flight_id into v 
from flight 
where arrival_time > :new.departure_time 
and departure_time < :new.arrival_time; 
raise_application_error(-20000, 'not good'); 
exception 
when NO_DATA_FOUND 
    then return 1 
when others 
    then raise_application_error(-20011,'really good'); 
1
DECLARE 
    V_some_varible NUMBER; 
BEGIN 
    Seclect 1 into v_some_varible from dual where 1 = 0; 
EXCEPTION -- exception handlers begin 
    WHEN NO_DATA_FOUND THEN 
     INSERT INTO errors (message) VALUES ('no data found'); 
    WHEN OTHERS THEN -- handles all other errors 
     ROLLBACK; 
END; 

注意1 = 0強制找不到數據異常。這個例子應該可以在任何Oracle數據庫中工作,除了插入錯誤表之外。