ALTER FUNCTION "DBA"."get_pa_kaader_tun_tmt"( in a_asutnr integer, in a_kuup date, in a_skoopkood char(16) default '' )
returns smallint
begin
  declare li_tmt smallint;
  select first pa_kaader_tun.tmt into li_tmt
    from pa_kaader_tun left outer join skoobid on pa_kaader_tun.skoop=skoobid.skoop
    where pa_kaader_tun.asutnr=a_asutnr and pa_kaader_tun.kuup1<=a_kuup and pa_kaader_tun.tmt is not null and left(skoobid.kood,length(a_skoopkood))=a_skoopkood
    order by pa_kaader_tun.kuup1 desc;
  if li_tmt is null then
    set li_tmt=2
  end if;
  return li_tmt
end