ALTER FUNCTION "dba"."pa_exists_summa"( in a_a_apaev numeric(2),in a_a_lpaev numeric(2),in a_a_akuu numeric(2),in a_a_aasta numeric(4),in a_lepnr integer,in a_tas_s integer,in a_paryldnr integer,in a_tasunr integer default 0)
returns numeric(15,2)
begin
  declare ldc_summa numeric(15,2);
  if a_tasunr is null then
    set a_tasunr = 0
  end if;
  select sum(pa_tasu.summa) into ldc_summa
    from pa_tasu
    where pa_tasu.a_apaev = a_a_apaev and pa_tasu.a_lpaev = a_a_lpaev and
          pa_tasu.a_akuu = a_a_akuu and pa_tasu.a_aasta = a_a_aasta and
          pa_tasu.lepnr = a_lepnr and pa_tasu.tas_s = a_tas_s and
         (pa_tasu.paryldnr = a_paryldnr or a_paryldnr is null) and
          pa_tasu.tasunr <> a_tasunr;
  if ldc_summa is null then
    set ldc_summa = 0
  end if;
  return(ldc_summa)
end