ALTER FUNCTION "DBA"."get_eak_earve_kbnr"( in a_id_dk_earve integer, in a_id integer )
RETURNS INTEGER
BEGIN
  declare ll_kbnr integer;
  if a_id>0 then
    select if dk_earve_rida.eakvatcode is null or Length(dk_earve_rida.eakvatcode)=0 then (if dk_earve_rida.vatrate is null or dk_earve_rida.vatrate=0 then null else existid_kblist_protsent(dk_earve_rida.vatrate) endif) else existid_kblist_nimi(dk_earve_rida.eakvatcode) endif
        into ll_kbnr
        from dk_earve_rida
        where dk_earve_rida.id_dk_earve=a_id_dk_earve and dk_earve_rida.id=a_id;
  end if;
  return ll_kbnr;
END