ALTER TRIGGER "tib_kp_kanne" before insert order 1 on
DBA.kp_kanne
referencing new as new_name
for each row
begin
  declare li_id integer;
  declare ls_doknr char(20);
  declare li_years integer;
  set li_years=years(new_name.kuup);
  if(new_name.id is null) or(new_name.id = 0) then
    select getid_kp_kanne(li_years) into li_id from DUMMY;
    set new_name.id=li_id
  end if;
  if(new_name.doknr is null) or(new_name.doknr = '') then
    select get_doknr_kp(li_years,new_name.objnr,new_name.sisse) into ls_doknr from DUMMY;
    set new_name.doknr=ls_doknr
  end if;
  if(new_name.dkdoknimi is null) then
    set new_name.dkdoknimi='Arve nr. '
  end if;
  call pr_kanne_add(month(new_name.kuup),li_years,new_name.objnr,1);
  set new_name.aasta=li_years;
  set new_name.kell=Now(*)
end