ALTER TRIGGER "tub_asulist" before update order 1 on
DBA.asulist
referencing new as new_name
for each row
begin
  if new_name.reg_nr is null then
    set new_name.reg_nr=''
  end if;
  if new_name.nimi is null then
    set new_name.nimi=''
  end if;
  if(new_name.viitnr_mulle is null) or(Length(trim(new_name.viitnr_mulle)) = 0) then
    if exists(select 1 from tm_rmpt where k_uus_viitnr=1 and id=1) then
      set new_name.viitnr_mulle=get_viitnr_last_pos(2000000+new_name.asutnr)
    else
      set new_name.viitnr_mulle=get_viitnr_last_pos(1000000+new_name.asutnr)
    end if
  end if;
  if new_name.kmdinf is null then
    if Length(new_name.reg_nr)=11 then
      set new_name.kmdinf=0
    else
      if Length(new_name.reg_nr)=8 then
        set new_name.kmdinf=1
      end if;
    end if;
  end if
end