我有以下表格:PL/SQL觸發器返回缺貨錯誤:觸發與編譯錯誤創建
order_lines table
Name Null? Type
ORDER_ID NOT NULL NUMBER(5)
PRODUCT_ID NOT NULL NUMBER(4)
ACTUAL_PRICE NUMBER
DISCOUNT NUMBER
QUANTITY NUMBER
TOTAL_AMOUNT NUMBER
而且
Product_inventory table
Name Null? Type
PRODUCT_ID NOT NULL NUMBER(4)
QTY_ON_HAND NUMBER
QTY_ON_ORDER NUMBER
DATE_ORDERED DATE
DELIVERY_DATE DATE
我想有一個觸發器,它檢查訂單的數量,看它是否缺貨,並顯示一條消息,如果是。我現在的觸發與一些錯誤編譯:
CREATE OR REPLACE TRIGGER check_order_line
BEFORE INSERT OR UPDATE ON order_lines
for each row
DECLARE
l_current_stock product_inventory.qty_on_hand%type;
BEGIN
select product_inventory.qty_on_hand
into l_current_stock
from product_inventory, order_lines
where product_inventory.product_id = :new.product_id;
if(:new.quantity > l_current_stock) then
RAISE_APPLICATION_ERROR(-20103, 'Insufficient Stock');
else
update product_inventory
set qty_on_hand = qty_on_hand - :new.quantity
where product_inventory.product_id = :new.product_id;
end if;
END;
當我測試了扳機,我發現了以下錯誤:
insert into order_lines values (388,1023,100,20,2,160)
*
ERROR at line 1:
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "DBA643.CHECK_ORDER_LINE", line 4
ORA-04088: error during execution of trigger 'DBA643.CHECK_ORDER_LINE'
爲什麼會出現呢?
謝謝觸發工作正常,但是當我試圖測試它給我的錯誤。我已經更新了帖子 – WT86 2014-09-26 13:23:44
@ WT86觸發:要創建一個使用這條線交叉聯接:'從product_inventory,order_lines'從select語句中刪除'order_lines'表。這不是必需的。並確保'select'只返回單行(該錯誤消息所示) – 2014-09-26 13:26:20
@ WT86:和它通常不是一個完全新的錯誤來改變這個問題是個好主意。你還是創建該問題的一個新的問題(原來的問題已經解決) – 2014-09-26 13:28:32