I’m new to Oracle and SQL and I’m trying to create a trigger where the req_ship_date (suborder table) cannot be before the order_date (customer_order table). I created the trigger:
create or replace trigger req_ship_date_error
before insert on suborder
for each row
declare
placed_date date;
req_date date;
begin
select order_date into placed_date
from customer_order;
select req_ship_date into req_date
from suborder
where suborder_no = :new.suborder_no;
if placed_date > req_date then
raise_application_error(-20002, ('Order ' || :new.suborder_no || ' required shipping date cannot be before order date'));
end if;
end;
But even after a try inserting a good statement I’m getting the following error
insert into suborder
values ( 8, 2, '10-jul-2012', '12-jul-2012', 'CVS', 2);
ERROR
Error starting at line 1 in command:
insert into suborder
values ( 8, 2, '10-jul-2012', '12-jul-2012', 'CVS', 2)
Error report:
SQL Error: ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "COMPANY.REQ_SHIP_DATE_ERROR", line 5
ORA-04088: error during execution of trigger 'COMPANY.REQ_SHIP_DATE_ERROR'
01422. 00000 - "exact fetch returns more than requested number of rows"
*Cause: The number specified in exact fetch is less than the rows returned.
*Action: Rewrite the query or change number of rows requested
Any idea what is causing this? Thanks
WHEREclause on the query againstCUSTOMER_ORDERSUBORDERcannot then querySUBORDER. If you do that, you’ll end up getting a mutating table error in general. Assuming thatSUBORDER_NOis the primary key ofSUBORDER, however, you shouldn’t need to query the table. Simply use the:new.req_ship_datevalue you have in your hand.Something like this should work assuming that the primary key of
customer_orderisorder_noand thatorder_noexists in thesubordertable.