ALTER TRIGGER "tib_dk_kanne" before insert order 1 on
DBA.dk_kanne
referencing new as new_name
for each row
begin
  declare li_id integer;
  declare ls_doknr char(20);
  declare li_years integer;
  declare li_spank smallint;
  set li_years=years(new_name.kuup);
  set new_name.aasta=li_years;
  if(new_name.id is null) or(new_name.id = 0) then
    select getid_dk_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
    if new_name.arvekuup is not null then
      set li_years=years(new_name.arvekuup)
    end if;
    select dk_obj.spank into li_spank from dk_obj where dk_obj.objnr=new_name.objnr;
    select get_doknr_dk(li_years,new_name.objnr) into ls_doknr from DUMMY;
    set new_name.doknr=ls_doknr;
    if li_spank = 0 and (new_name.arvenr is null) or(new_name.arvenr = '') then
      set new_name.arvenr=ls_doknr
    end if
  end if;
  call pr_kanne_add(month(new_name.kuup),li_years,new_name.objnr,2);
  set new_name.kell=Now(*)
end