Question : pl sql

hi im reading up on pl sql and i've come across this example. im wondering if in the sql query below where i put a comment, how do the trigger differenciates the value? Also, what if its a insert statement where the 'peron' is a new record? the select statement will not return any values isnt it?

Given a table
person(staff_id, income, repay_amount), write a database trigger called
RepayTrigger to enforce the constraint that repay_amount is 10% of
fortnight_income if no repay_amount is given in an update or insert.

create or replace trigger TestTrigger
Before update or insert on person
Declare
  amount person.fornight_income%type;
Begin
  -- I AM NOT SURE ABOUT THIS PART --
  select income into amount from person where old.staff_id = new.staff_id //do i nid to do a new. ?
  if(amount < 10) then
    EXCEPTION limit;
  end if;
  EXCEPTION
    when limit then
        raise_application_error(-20001, 'something is wrong');
END;
.
RUN;

Answer : pl sql

I would write the trigger like this:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
create or replace trigger TestTrigger
Before update or insert on person
Begin
  IF EXISTS( select null from person where income < 10 )
    EXCEPTION limit;
  end if;
  EXCEPTION 
    when limit then 
        raise_application_error(-20001, 'something is wrong');
END;
Open in New Window Select All
Random Solutions  
 
programming4us programming4us