CREATE PROCEDURE "DBA"."dk_arvetasarno_taot"(in as_taot varchar(20))
begin
  declare ldc_arvesumma numeric(15,2);
  declare ldc_makssumma numeric(15,2);
  
  set as_taot=left(trim(as_taot)+space(16),16);

  select sum((2*dk_kanne.sisse-1)*(dk_kanne.summa+dk_kanne.summa2))
    into ldc_arvesumma
    from dk_kanne left outer join taotlist on dk_kanne.taotnr=taotlist.taotnr
    where taotlist.taot=as_taot;
  if ldc_arvesumma is null then
    set ldc_arvesumma=0
  end if;

  select sum((2*kp_kanne.sisse-1)*kp_kanne.summa)
    into ldc_makssumma
    from kp_kanne left outer join taotlist on kp_kanne.taotnr=taotlist.taotnr
    where taotlist.taot=as_taot;
  if ldc_makssumma is null then
    set ldc_makssumma=0
  end if;

  select ldc_makssumma-ldc_arvesumma as saldo;

end