CREATE FUNCTION "dba"."get_pa_kaader_pens_rate" ( in a_asutnr integer, in a_kuup date)
returns numeric(10,3)
begin
  declare ldc_rate numeric(10,3);
  select first pa_kaader_pens.rate into ldc_rate
    from pa_kaader_pens
    where pa_kaader_pens.asutnr=a_asutnr and pa_kaader_pens.kuup1<=a_kuup and pa_kaader_pens.rate is not null
    order by pa_kaader_pens.kuup1 desc;
  if ldc_rate is null then
    set ldc_rate=0
  end if;
  return ldc_rate
end