CREATE FUNCTION "DBA"."get_pa_kaader_tun_tmt"( in a_asutnr integer, in a_kuup date )
returns smallint
begin
  declare li_tmt smallint;
  select first pa_kaader_tun.tmt into li_tmt from pa_kaader_tun where pa_kaader_tun.asutnr=a_asutnr and pa_kaader_tun.kuup1<=a_kuup and pa_kaader_tun.tmt is not null order by pa_kaader_tun.kuup1 desc;
  if li_tmt is null then
    set li_tmt=2
  end if;
  return li_tmt
end