ALTER FUNCTION "DBA"."get_pa_kaader_tun_tk"( in a_asutnr integer, in a_kuup date, in a_skoopkood char(16) default '' )
returns smallint
begin
  declare li_tk smallint;
  select first pa_kaader_tun.tk into li_tk
    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.tk is not null and left(skoobid.kood,length(a_skoopkood))=a_skoopkood
    order by pa_kaader_tun.kuup1 desc;
  if li_tk is null then
    set li_tk=3
  end if;
  return li_tk
end