You're inserting a new load with amount 100000
, for a customer whose account balance is 105000
. The rule is to check whether the balance is at least 25% of the amount of the loan. 25% of your loan value is 25000
, and the balance is much higher than that.
For your trigger to raise the exception the loan amount would need to be more than four times the balance:
insert into loan values (1004, 100000, 2000, 'C02');
1 rows inserted.
insert into loan values (1005, (100005 * 4), 2000, 'C02');
1 rows inserted.
insert into xloan values (1006, (105000 * 4) + 1, 2000, 'C02')
Error report -
SQL Error: ORA-20001: Balance is not sufficient
ORA-06512: at "MYSCHEMA.CLAIM", line 11
ORA-04088: error during execution of trigger 'MYSCHEMA.CLAIM'
So you just have the logic the wrong way round when you're creating the data and insert statement.
You can simplify the trigger a bit, incidentally, as you don't need to raise and catch the ex
exception, you can just raise your own error as soon as you spot the problem:
create or replace trigger claim
before insert on loan
for each row
declare
bal number;
begin
select accbal into bal from account where :new.custid=account.custid;
if bal < (0.25*:new.amount) then
raise_application_error(-20001,'Balance is not sufficient');
end if;
end;
/