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