ALTER PROCEDURE "DBA"."ea_tulud"(inout a_komb_id integer, in a_xxxnr integer)
begin
  declare li_tr_level smallint;
  declare li_tr_nlevel smallint;
  declare li_id_lkd integer;
  declare li_rows integer;
  declare ls_nimi char(100);
  declare ls_kood char(16);
  delete from gl_ea_rida;
  select ea_komb.id_lkd into li_id_lkd from ea_komb where "id" = a_komb_id;
  set li_tr_level=get_ea_tr_level(a_komb_id, 1);
  set li_tr_nlevel=get_ea_tr_level(a_komb_id, 2);
  if li_tr_level=15 or li_tr_level=16 then
    if a_xxxnr>0 then
      select "meetlist"."meet","meetlist"."nimi" into ls_kood,ls_nimi from "meetlist" where "meetlist"."meetnr"=a_xxxnr;
      set li_rows=ea_trykkel(a_komb_id,1,li_tr_nlevel,'gl_ea_kulkond','','gl_ea_kulkond.meetnr='+convert(char(10),a_xxxnr),ls_kood,null,null,null,null,null,null,null,null,null,ls_nimi)
    else
      call ea_meetjaot(a_komb_id,1,'gl_ea_kulkond','','',null,null,null,null,null,null,null,null,null,null)
    end if
  elseif li_tr_level=1 or li_tr_level=2 then
    if a_xxxnr>0 then
      select "kullist"."ptk","kullist"."nimi" into ls_kood,ls_nimi from "kullist" where "kullist"."kulnr"=a_xxxnr;
      set li_rows=ea_trykkel(a_komb_id,1,li_tr_nlevel,'gl_ea_tulkond','','gl_ea_tulkond.kulnr='+convert(char(10),a_xxxnr),null,ls_kood,null,null,null,null,null,null,null,null,ls_nimi)
    else
      call ea_kuljaot(a_komb_id,1,'gl_ea_tulkond','','',null,null,null,null,null,null,null,null,null,null)
    end if
  elseif li_tr_level=3 or li_tr_level=4 then
    if a_xxxnr>0 then
      select "tallist"."osa","tallist"."nimi" into ls_kood,ls_nimi from "tallist" where "tallist"."talnr"=a_xxxnr;
      set li_rows=ea_trykkel(a_komb_id,1,li_tr_nlevel,'gl_ea_tulkond','','gl_ea_tulkond.talnr='+convert(char(10),a_xxxnr),null,null,ls_kood,null,null,null,null,null,null,null,ls_nimi)
    else
      call ea_taljaot(a_komb_id,1,'gl_ea_tulkond','','',null,null,null,null,null,null,null,null,null,null)
    end if
  elseif li_tr_level=5 or li_tr_level=6 then
    if a_xxxnr>0 then
      select "tullist"."osa","tullist"."nimi" into ls_kood,ls_nimi from "tullist" where "tullist"."tulnr"=a_xxxnr;
      set li_rows=ea_trykkel(a_komb_id,1,li_tr_nlevel,'gl_ea_tulkond','','gl_ea_tulkond.tulnr='+convert(char(10),a_xxxnr),null,null,null,null,ls_kood,null,null,null,null,null,ls_nimi)
    else
      call ea_tuljaot(a_komb_id,1,'gl_ea_tulkond','','',null,null,null,null,null,null,null,null,null,null)
    end if
  elseif li_tr_level=7 or li_tr_level=8 then
    if a_xxxnr>0 then
      select "objlist"."object","objlist"."nimi" into ls_kood,ls_nimi from "objlist" where "objlist"."objectnr"=a_xxxnr;
      set li_rows=ea_trykkel(a_komb_id,1,li_tr_nlevel,'gl_ea_tulkond','','gl_ea_tulkond.objectnr='+convert(char(10),a_xxxnr),null,null,null,null,null,ls_kood,null,null,null,null,ls_nimi)
    else
      call ea_objectjaot(a_komb_id,1,'gl_ea_tulkond','','',null,null,null,null,null,null,null,null,null,null)
    end if
  elseif li_tr_level=9 or li_tr_level=10 then
    if a_xxxnr>0 then
      select "osaklist"."osak","osaklist"."nimi" into ls_kood,ls_nimi from "osaklist" where "osaklist"."osaknr"=a_xxxnr;
      set li_rows=ea_trykkel(a_komb_id,1,li_tr_nlevel,'gl_ea_tulkond','','gl_ea_tulkond.osaknr='+convert(char(10),a_xxxnr),null,null,null,null,null,null,ls_kood,null,null,null,ls_nimi)
    else
      call ea_osakjaot(a_komb_id,1,'gl_ea_tulkond','','',null,null,null,null,null,null,null,null,null,null)
    end if
  elseif li_tr_level=11 or li_tr_level=12 then
    if a_xxxnr>0 then
      select "sublist"."subject","sublist"."nimi" into ls_kood,ls_nimi from "sublist" where "sublist"."subjectnr"=a_xxxnr;
      set li_rows=ea_trykkel(a_komb_id,1,li_tr_nlevel,'gl_ea_tulkond','','gl_ea_tulkond.subjectnr='+convert(char(10),a_xxxnr),null,null,null,null,null,null,null,ls_kood,null,null,ls_nimi)
    else
      call ea_subjectjaot(a_komb_id,1,'gl_ea_tulkond','','',null,null,null,null,null,null,null,null,null,null)
    end if
  elseif li_tr_level=13 or li_tr_level=14 then
    if a_xxxnr>0 then
      select "suundlist"."suund","suundlist"."nimi" into ls_kood,ls_nimi from "suundlist" where "suundlist"."suundnr"=a_xxxnr;
      set li_rows=ea_trykkel(a_komb_id,1,li_tr_nlevel,'gl_ea_tulkond','','gl_ea_tulkond.suundnr='+convert(char(10),a_xxxnr),null,null,null,null,null,null,null,null,ls_kood,null,ls_nimi)
    else
      call ea_suundjaot(a_komb_id,1,'gl_ea_tulkond','','',null,null,null,null,null,null,null,null,null,null)
    end if
  elseif li_tr_level=17 or li_tr_level=18 then
    if a_xxxnr>0 then
      select "taotlist"."taot","taotlist"."nimi" into ls_kood,ls_nimi from "taotlist" where "taotlist"."taotnr"=a_xxxnr;
      set li_rows=ea_trykkel(a_komb_id,1,li_tr_nlevel,'gl_ea_kulkond','','gl_ea_kulkond.taotnr='+convert(char(10),a_xxxnr),null,null,null,null,null,null,null,null,null,ls_kood,ls_nimi)
    else
      call ea_taotjaot(a_komb_id,1,'gl_ea_kulkond','','',null,null,null,null,null,null,null,null,null,null)
    end if
  end if;
  if li_tr_level in (1,3,5,7,9,11,13,15,17,19) then
    set li_tr_nlevel=get_ea_tr_level(a_komb_id, 100);
    set li_rows=ea_trykkel(a_komb_id,7,li_tr_nlevel,'gl_ea_tulkond','','1=1',null,null,null,null,null,null,null,null,null,'','TULUD KOKKU');
  end if;
  delete from gl_ea_rida where col01=0 and col02=0 and col03=0 and col04=0 and col05=0 and col06=0 and col07=0 and col08=0 and col09=0 and col10=0 and col11=0 and col12=0 and col13=0 and col14=0 and col15=0 and col16=0 and col17=0 and col18=0 and col19=0 and col20=0;
  call ea_compute(li_id_lkd,'gl_ea_rida',5)
end