CREATE PROCEDURE "DBA"."ea_tul_group"( in a_komb_id integer )
BEGIN
  declare ls_sql char(3000);
  declare ls_insert char(650);
  declare ls_select char(850);
  declare ls_from char(850);
  declare ls_group char(650);
  declare li_level1 smallint;
  declare li_level2 smallint;
  declare li_level3 smallint;
  declare li_level4 smallint;
  declare li_level5 smallint;
  declare li_level6 smallint;
  declare li_level7 smallint;
  declare ls_list1 char(10);
  declare ls_list2 char(10);
  declare ls_list3 char(10);
  declare ls_list4 char(10);
  declare ls_list5 char(10);
  declare ls_list6 char(10);
  declare ls_list7 char(10);
  declare ls_list8 char(10);
  declare ls_list9 char(10);
  declare ls_kood1 char(10);
  declare ls_kood2 char(10);
  declare ls_kood3 char(10);
  declare ls_kood4 char(10);
  declare ls_kood5 char(10);
  declare ls_kood6 char(10);
  declare ls_kood7 char(10);
  declare ls_kood8 char(10);
  declare ls_kood9 char(10);
  declare ls_id1 char(10);
  declare ls_id2 char(10);
  declare ls_id3 char(10);
  declare ls_id4 char(10);
  declare ls_id5 char(10);
  declare ls_id6 char(10);
  declare ls_id7 char(10);
  declare ls_id8 char(10);
  declare ls_id9 char(10);
  declare ls_veerg1 char(10);
  declare ls_veerg2 char(10);
  declare ls_veerg3 char(10);
  declare ls_veerg4 char(10);
  declare ls_veerg5 char(10);
  declare ls_veerg6 char(10);
  declare ls_veerg7 char(10);
  declare ls_veerg8 char(10);
  declare ls_veerg9 char(10);
  declare ls_mask1 char(23);
  declare ls_mask2 char(23);
  declare ls_mask3 char(23);
  declare ls_mask4 char(23);
  declare ls_mask5 char(23);
  declare ls_mask6 char(23);
  declare ls_mask7 char(23);
  declare ls_mask8 char(23);
  declare ls_mask9 char(23);
  declare li_i1 integer;
  declare li_i2 integer;
  declare li_i3 integer;
  declare li_i4 integer;
  declare li_i5 integer;
  declare li_i6 integer;
  declare li_i7 integer;
  declare li_i8 integer;
  declare li_i9 integer;
  declare li_pos1 smallint;
  declare li_pos2 smallint;
  declare li_pos3 smallint;
  declare li_pos4 smallint;
  declare li_pos5 smallint;
  declare li_pos6 smallint;
  declare li_pos7 smallint;
  declare li_tryk_kul integer;
  declare li_tryk_meet integer;
  declare li_tryk_tal integer;
  declare li_tryk_tul integer;
  declare li_tryk_object integer;
  declare li_tryk_osak integer;
  declare li_tryk_subject integer;
  declare li_tryk_suund integer;
  declare li_tryk_taot integer;


  delete from gl_ea_rida;
  delete from gl_ea_rida2;

  select tr_level1,tr_level2,tr_level3,tr_level4,tr_level5,tr_level6,tr_level7,tr_tryk_meet,tr_tryk_kul,tr_tryk_tal,tr_tryk_tul,tr_tryk_object,tr_tryk_osak,tr_tryk_subject,tr_tryk_suund,tr_tryk_taot
    into li_level1,li_level2,li_level3,li_level4,li_level5,li_level6,li_level7,li_tryk_meet,li_tryk_kul,li_tryk_tal,li_tryk_tul,li_tryk_object,li_tryk_osak,li_tryk_subject,li_tryk_suund,li_tryk_taot
    from ea_komb where "id" = a_komb_id;
  if li_level1>0 then
    if li_level1=15 or li_level1=16 then
      set ls_list1='meetlist';
      set ls_kood1='meet';
      set ls_id1='meetnr';
      set ls_veerg1='meet';
      select if tm_rmpt.mask_meetlist is null or Length(trim(tm_rmpt.mask_meetlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_meetlist) endif into ls_mask1 from tm_rmpt where tm_rmpt."id"=1;
    elseif li_level1=1 or li_level1=2 then
      set ls_list1='kullist';
      set ls_kood1='ptk';
      set ls_id1='kulnr';
      set ls_veerg1='kulu_obj';
      select if tm_rmpt.mask_kullist is null or Length(trim(tm_rmpt.mask_kullist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_kullist) endif into ls_mask1 from tm_rmpt where tm_rmpt."id"=1;
    elseif li_level1=3 or li_level1=4 then
      set ls_list1='tallist';
      set ls_kood1='osa';
      set ls_id1='talnr';
      set ls_veerg1='teg_ala';
      select if tm_rmpt.mask_tallist is null or Length(trim(tm_rmpt.mask_tallist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_tallist) endif into ls_mask1 from tm_rmpt where tm_rmpt."id"=1;
    elseif li_level1=5 or li_level1=6 then
      set ls_list1='tullist';
      set ls_kood1='osa';
      set ls_id1='tulnr';
      set ls_veerg1='tulu_liik';
      select if tm_rmpt.mask_tullist is null or Length(trim(tm_rmpt.mask_tullist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_tullist) endif into ls_mask1 from tm_rmpt where tm_rmpt."id"=1;
    elseif li_level1=7 or li_level1=8 then
      set ls_list1='objlist';
      set ls_kood1='object';
      set ls_id1='objectnr';
      set ls_veerg1='projekt';
      select if tm_rmpt.mask_objlist is null or Length(trim(tm_rmpt.mask_objlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_objlist) endif into ls_mask1 from tm_rmpt where tm_rmpt."id"=1;
    elseif li_level1=9 or li_level1=10 then
      set ls_list1='osaklist';
      set ls_kood1='osak';
      set ls_id1='osaknr';
      set ls_veerg1='osakond';
      select if tm_rmpt.mask_osaklist is null or Length(trim(tm_rmpt.mask_osaklist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_osaklist) endif into ls_mask1 from tm_rmpt where tm_rmpt."id"=1;
    elseif li_level1=11 or li_level1=12 then
      set ls_list1='sublist';
      set ls_kood1='subject';
      set ls_id1='subjectnr';
      set ls_veerg1='subjekt';
      select if tm_rmpt.mask_subjectlist is null or Length(trim(tm_rmpt.mask_subjectlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_subjectlist) endif into ls_mask1 from tm_rmpt where tm_rmpt."id"=1;
    elseif li_level1=13 or li_level1=14 then
      set ls_list1='suundlist';
      set ls_kood1='suund';
      set ls_id1='suundnr';
      set ls_veerg1='teg_suund';
      select if tm_rmpt.mask_suundlist is null or Length(trim(tm_rmpt.mask_suundlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_suundlist) endif into ls_mask1 from tm_rmpt where tm_rmpt."id"=1;
    elseif li_level1=17 or li_level1=18 then
      set ls_list1='taotlist';
      set ls_kood1='taot';
      set ls_id1='taotnr';
      set ls_veerg1='taot';
      select if tm_rmpt.mask_taotlist is null or Length(trim(tm_rmpt.mask_taotlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_taotlist) endif into ls_mask1 from tm_rmpt where tm_rmpt."id"=1;
    end if;
    if li_level2>0 then
      if li_level2=15 or li_level2=16 then
        set ls_list2='meetlist';
        set ls_kood2='meet';
        set ls_id2='meetnr';
        set ls_veerg2='meet';
        select if tm_rmpt.mask_meetlist is null or Length(trim(tm_rmpt.mask_meetlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_meetlist) endif into ls_mask2 from tm_rmpt where tm_rmpt."id"=1;
      elseif li_level2=1 or li_level2=2 then
        set ls_list2='kullist';
        set ls_kood2='ptk';
        set ls_id2='kulnr';
        set ls_veerg2='kulu_obj';
        select if tm_rmpt.mask_kullist is null or Length(trim(tm_rmpt.mask_kullist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_kullist) endif into ls_mask2 from tm_rmpt where tm_rmpt."id"=1;
      elseif li_level2=3 or li_level2=4 then
        set ls_list2='tallist';
        set ls_kood2='osa';
        set ls_id2='talnr';
        set ls_veerg2='teg_ala';
        select if tm_rmpt.mask_tallist is null or Length(trim(tm_rmpt.mask_tallist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_tallist) endif into ls_mask2 from tm_rmpt where tm_rmpt."id"=1;
      elseif li_level2=5 or li_level2=6 then
        set ls_list2='tullist';
        set ls_kood2='osa';
        set ls_id2='tulnr';
        set ls_veerg2='tulu_liik';
        select if tm_rmpt.mask_tullist is null or Length(trim(tm_rmpt.mask_tullist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_tullist) endif into ls_mask2 from tm_rmpt where tm_rmpt."id"=1;
      elseif li_level2=7 or li_level2=8 then
        set ls_list2='objlist';
        set ls_kood2='object';
        set ls_id2='objectnr';
        set ls_veerg2='projekt';
        select if tm_rmpt.mask_objlist is null or Length(trim(tm_rmpt.mask_objlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_objlist) endif into ls_mask2 from tm_rmpt where tm_rmpt."id"=1;
      elseif li_level2=9 or li_level2=10 then
        set ls_list2='osaklist';
        set ls_kood2='osak';
        set ls_id2='osaknr';
        set ls_veerg2='osakond';
        select if tm_rmpt.mask_osaklist is null or Length(trim(tm_rmpt.mask_osaklist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_osaklist) endif into ls_mask2 from tm_rmpt where tm_rmpt."id"=1;
      elseif li_level2=11 or li_level2=12 then
        set ls_list2='sublist';
        set ls_kood2='subject';
        set ls_id2='subjectnr';
        set ls_veerg2='subjekt';
        select if tm_rmpt.mask_subjectlist is null or Length(trim(tm_rmpt.mask_subjectlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_subjectlist) endif into ls_mask2 from tm_rmpt where tm_rmpt."id"=1;
      elseif li_level2=13 or li_level2=14 then
        set ls_list2='suundlist';
        set ls_kood2='suund';
        set ls_id2='suundnr';
        set ls_veerg2='teg_suund';
        select if tm_rmpt.mask_suundlist is null or Length(trim(tm_rmpt.mask_suundlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_suundlist) endif into ls_mask2 from tm_rmpt where tm_rmpt."id"=1;
      elseif li_level2=17 or li_level2=18 then
        set ls_list2='taotlist';
        set ls_kood2='taot';
        set ls_id2='taotnr';
        set ls_veerg2='taot';
        select if tm_rmpt.mask_taotlist is null or Length(trim(tm_rmpt.mask_taotlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_taotlist) endif into ls_mask2 from tm_rmpt where tm_rmpt."id"=1;
      end if;
      if li_level3>0 then
        if li_level3=15 or li_level3=16 then
          set ls_list3='meetlist';
          set ls_kood3='meet';
          set ls_id3='meetnr';
          set ls_veerg3='meet';
          select if tm_rmpt.mask_meetlist is null or Length(trim(tm_rmpt.mask_meetlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_meetlist) endif into ls_mask3 from tm_rmpt where tm_rmpt."id"=1;
        elseif li_level3=1 or li_level3=2 then
          set ls_list3='kullist';
          set ls_kood3='ptk';
          set ls_id3='kulnr';
          set ls_veerg3='kulu_obj';
          select if tm_rmpt.mask_kullist is null or Length(trim(tm_rmpt.mask_kullist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_kullist) endif into ls_mask3 from tm_rmpt where tm_rmpt."id"=1;
        elseif li_level3=3 or li_level3=4 then
          set ls_list3='tallist';
          set ls_kood3='osa';
          set ls_id3='talnr';
          set ls_veerg3='teg_ala';
          select if tm_rmpt.mask_tallist is null or Length(trim(tm_rmpt.mask_tallist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_tallist) endif into ls_mask3 from tm_rmpt where tm_rmpt."id"=1;
        elseif li_level3=5 or li_level3=6 then
          set ls_list3='tullist';
          set ls_kood3='osa';
          set ls_id3='tulnr';
          set ls_veerg3='tulu_liik';
          select if tm_rmpt.mask_tullist is null or Length(trim(tm_rmpt.mask_tullist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_tullist) endif into ls_mask3 from tm_rmpt where tm_rmpt."id"=1;
        elseif li_level3=7 or li_level3=8 then
          set ls_list3='objlist';
          set ls_kood3='object';
          set ls_id3='objectnr';
          set ls_veerg3='projekt';
          select if tm_rmpt.mask_objlist is null or Length(trim(tm_rmpt.mask_objlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_objlist) endif into ls_mask3 from tm_rmpt where tm_rmpt."id"=1;
        elseif li_level3=9 or li_level3=10 then
          set ls_list3='osaklist';
          set ls_kood3='osak';
          set ls_id3='osaknr';
          set ls_veerg3='osakond';
          select if tm_rmpt.mask_osaklist is null or Length(trim(tm_rmpt.mask_osaklist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_osaklist) endif into ls_mask3 from tm_rmpt where tm_rmpt."id"=1;
        elseif li_level3=11 or li_level3=12 then
          set ls_list3='sublist';
          set ls_kood3='subject';
          set ls_id3='subjectnr';
          set ls_veerg3='subjekt';
          select if tm_rmpt.mask_subjectlist is null or Length(trim(tm_rmpt.mask_subjectlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_subjectlist) endif into ls_mask3 from tm_rmpt where tm_rmpt."id"=1;
        elseif li_level3=13 or li_level3=14 then
          set ls_list3='suundlist';
          set ls_kood3='suund';
          set ls_id3='suundnr';
          set ls_veerg3='teg_suund';
          select if tm_rmpt.mask_suundlist is null or Length(trim(tm_rmpt.mask_suundlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_suundlist) endif into ls_mask3 from tm_rmpt where tm_rmpt."id"=1;
        elseif li_level3=17 or li_level3=18 then
          set ls_list3='taotlist';
          set ls_kood3='taot';
          set ls_id3='taotnr';
          set ls_veerg3='taot';
          select if tm_rmpt.mask_taotlist is null or Length(trim(tm_rmpt.mask_taotlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_taotlist) endif into ls_mask3 from tm_rmpt where tm_rmpt."id"=1;
        end if;
        if li_level4>0 then
          if li_level4=15 or li_level4=16 then
            set ls_list4='meetlist';
            set ls_kood4='meet';
            set ls_id4='meetnr';
            set ls_veerg4='meet';
            select if tm_rmpt.mask_meetlist is null or Length(trim(tm_rmpt.mask_meetlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_meetlist) endif into ls_mask4 from tm_rmpt where tm_rmpt."id"=1;
          elseif li_level4=1 or li_level4=2 then
            set ls_list4='kullist';
            set ls_kood4='ptk';
            set ls_id4='kulnr';
            set ls_veerg4='kulu_obj';
            select if tm_rmpt.mask_kullist is null or Length(trim(tm_rmpt.mask_kullist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_kullist) endif into ls_mask4 from tm_rmpt where tm_rmpt."id"=1;
          elseif li_level4=3 or li_level4=4 then
            set ls_list4='tallist';
            set ls_kood4='osa';
            set ls_id4='talnr';
            set ls_veerg4='teg_ala';
            select if tm_rmpt.mask_tallist is null or Length(trim(tm_rmpt.mask_tallist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_tallist) endif into ls_mask4 from tm_rmpt where tm_rmpt."id"=1;
          elseif li_level4=5 or li_level4=6 then
            set ls_list4='tullist';
            set ls_kood4='osa';
            set ls_id4='tulnr';
            set ls_veerg4='tulu_liik';
            select if tm_rmpt.mask_tullist is null or Length(trim(tm_rmpt.mask_tullist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_tullist) endif into ls_mask4 from tm_rmpt where tm_rmpt."id"=1;
          elseif li_level4=7 or li_level4=8 then
            set ls_list4='objlist';
            set ls_kood4='object';
            set ls_id4='objectnr';
            set ls_veerg4='projekt';
            select if tm_rmpt.mask_objlist is null or Length(trim(tm_rmpt.mask_objlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_objlist) endif into ls_mask4 from tm_rmpt where tm_rmpt."id"=1;
          elseif li_level4=9 or li_level4=10 then
            set ls_list4='osaklist';
            set ls_kood4='osak';
            set ls_id4='osaknr';
            set ls_veerg4='osakond';
            select if tm_rmpt.mask_osaklist is null or Length(trim(tm_rmpt.mask_osaklist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_osaklist) endif into ls_mask4 from tm_rmpt where tm_rmpt."id"=1;
          elseif li_level4=11 or li_level4=12 then
            set ls_list4='sublist';
            set ls_kood4='subject';
            set ls_id4='subjectnr';
            set ls_veerg4='subjekt';
            select if tm_rmpt.mask_subjectlist is null or Length(trim(tm_rmpt.mask_subjectlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_subjectlist) endif into ls_mask4 from tm_rmpt where tm_rmpt."id"=1;
          elseif li_level4=13 or li_level4=14 then
            set ls_list4='suundlist';
            set ls_kood4='suund';
            set ls_id4='suundnr';
            set ls_veerg4='teg_suund';
            select if tm_rmpt.mask_suundlist is null or Length(trim(tm_rmpt.mask_suundlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_suundlist) endif into ls_mask4 from tm_rmpt where tm_rmpt."id"=1;
          elseif li_level4=17 or li_level4=18 then
            set ls_list4='taotlist';
            set ls_kood4='taot';
            set ls_id4='taotnr';
            set ls_veerg4='taot';
            select if tm_rmpt.mask_taotlist is null or Length(trim(tm_rmpt.mask_taotlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_taotlist) endif into ls_mask4 from tm_rmpt where tm_rmpt."id"=1;
          end if;
          if li_level5>0 then
            if li_level5=15 or li_level5=16 then
              set ls_list5='meetlist';
              set ls_kood5='meet';
              set ls_id5='meetnr';
              set ls_veerg5='meet';
              select if tm_rmpt.mask_meetlist is null or Length(trim(tm_rmpt.mask_meetlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_meetlist) endif into ls_mask5 from tm_rmpt where tm_rmpt."id"=1;
            elseif li_level5=1 or li_level5=2 then
              set ls_list5='kullist';
              set ls_kood5='ptk';
              set ls_id5='kulnr';
              set ls_veerg5='kulu_obj';
              select if tm_rmpt.mask_kullist is null or Length(trim(tm_rmpt.mask_kullist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_kullist) endif into ls_mask5 from tm_rmpt where tm_rmpt."id"=1;
            elseif li_level5=3 or li_level5=4 then
              set ls_list5='tallist';
              set ls_kood5='osa';
              set ls_id5='talnr';
              set ls_veerg5='teg_ala';
              select if tm_rmpt.mask_tallist is null or Length(trim(tm_rmpt.mask_tallist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_tallist) endif into ls_mask5 from tm_rmpt where tm_rmpt."id"=1;
            elseif li_level5=5 or li_level5=6 then
              set ls_list5='tullist';
              set ls_kood5='osa';
              set ls_id5='tulnr';
              set ls_veerg5='tulu_liik';
              select if tm_rmpt.mask_tullist is null or Length(trim(tm_rmpt.mask_tullist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_tullist) endif into ls_mask5 from tm_rmpt where tm_rmpt."id"=1;
            elseif li_level5=7 or li_level5=8 then
              set ls_list5='objlist';
              set ls_kood5='object';
              set ls_id5='objectnr';
              set ls_veerg5='projekt';
              select if tm_rmpt.mask_objlist is null or Length(trim(tm_rmpt.mask_objlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_objlist) endif into ls_mask5 from tm_rmpt where tm_rmpt."id"=1;
            elseif li_level5=9 or li_level5=10 then
              set ls_list5='osaklist';
              set ls_kood5='osak';
              set ls_id5='osaknr';
              set ls_veerg5='osakond';
              select if tm_rmpt.mask_osaklist is null or Length(trim(tm_rmpt.mask_osaklist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_osaklist) endif into ls_mask5 from tm_rmpt where tm_rmpt."id"=1;
            elseif li_level5=11 or li_level5=12 then
              set ls_list5='sublist';
              set ls_kood5='subject';
              set ls_id5='subjectnr';
              set ls_veerg5='subjekt';
              select if tm_rmpt.mask_subjectlist is null or Length(trim(tm_rmpt.mask_subjectlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_subjectlist) endif into ls_mask5 from tm_rmpt where tm_rmpt."id"=1;
            elseif li_level5=13 or li_level5=14 then
              set ls_list5='suundlist';
              set ls_kood5='suund';
              set ls_id5='suundnr';
              set ls_veerg5='teg_suund';
              select if tm_rmpt.mask_suundlist is null or Length(trim(tm_rmpt.mask_suundlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_suundlist) endif into ls_mask5 from tm_rmpt where tm_rmpt."id"=1;
            elseif li_level5=17 or li_level5=18 then
              set ls_list5='taotlist';
              set ls_kood5='taot';
              set ls_id5='taotnr';
              set ls_veerg5='taot';
              select if tm_rmpt.mask_taotlist is null or Length(trim(tm_rmpt.mask_taotlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_taotlist) endif into ls_mask5 from tm_rmpt where tm_rmpt."id"=1;
            end if;
            if li_level6>0 then
              if li_level6=15 or li_level6=16 then
                set ls_list6='meetlist';
                set ls_kood6='meet';
                set ls_id6='meetnr';
                set ls_veerg6='meet';
                select if tm_rmpt.mask_meetlist is null or Length(trim(tm_rmpt.mask_meetlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_meetlist) endif into ls_mask6 from tm_rmpt where tm_rmpt."id"=1;
              elseif li_level6=1 or li_level6=2 then
                set ls_list6='kullist';
                set ls_kood6='ptk';
                set ls_id6='kulnr';
                set ls_veerg6='kulu_obj';
                select if tm_rmpt.mask_kullist is null or Length(trim(tm_rmpt.mask_kullist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_kullist) endif into ls_mask6 from tm_rmpt where tm_rmpt."id"=1;
              elseif li_level6=3 or li_level6=4 then
                set ls_list6='tallist';
                set ls_kood6='osa';
                set ls_id6='talnr';
                set ls_veerg6='teg_ala';
                select if tm_rmpt.mask_tallist is null or Length(trim(tm_rmpt.mask_tallist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_tallist) endif into ls_mask6 from tm_rmpt where tm_rmpt."id"=1;
              elseif li_level6=5 or li_level6=6 then
                set ls_list6='tullist';
                set ls_kood6='osa';
                set ls_id6='tulnr';
                set ls_veerg6='tulu_liik';
                select if tm_rmpt.mask_tullist is null or Length(trim(tm_rmpt.mask_tullist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_tullist) endif into ls_mask6 from tm_rmpt where tm_rmpt."id"=1;
              elseif li_level6=7 or li_level6=8 then
                set ls_list6='objlist';
                set ls_kood6='object';
                set ls_id6='objectnr';
                set ls_veerg6='projekt';
                select if tm_rmpt.mask_objlist is null or Length(trim(tm_rmpt.mask_objlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_objlist) endif into ls_mask6 from tm_rmpt where tm_rmpt."id"=1;
              elseif li_level6=9 or li_level6=10 then
                set ls_list6='osaklist';
                set ls_kood6='osak';
                set ls_id6='osaknr';
                set ls_veerg6='osakond';
                select if tm_rmpt.mask_osaklist is null or Length(trim(tm_rmpt.mask_osaklist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_osaklist) endif into ls_mask6 from tm_rmpt where tm_rmpt."id"=1;
              elseif li_level6=11 or li_level6=12 then
                set ls_list6='sublist';
                set ls_kood6='subject';
                set ls_id6='subjectnr';
                set ls_veerg6='subjekt';
                select if tm_rmpt.mask_subjectlist is null or Length(trim(tm_rmpt.mask_subjectlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_subjectlist) endif into ls_mask6 from tm_rmpt where tm_rmpt."id"=1;
              elseif li_level6=13 or li_level6=14 then
                set ls_list6='suundlist';
                set ls_kood6='suund';
                set ls_id6='suundnr';
                set ls_veerg6='teg_suund';
                select if tm_rmpt.mask_suundlist is null or Length(trim(tm_rmpt.mask_suundlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_suundlist) endif into ls_mask6 from tm_rmpt where tm_rmpt."id"=1;
              elseif li_level6=17 or li_level6=18 then
                set ls_list6='taotlist';
                set ls_kood6='taot';
                set ls_id6='taotnr';
                set ls_veerg6='taot';
                select if tm_rmpt.mask_taotlist is null or Length(trim(tm_rmpt.mask_taotlist))=0 then '@@@@@@@@@@@@@@@@' else trim(tm_rmpt.mask_taotlist) endif into ls_mask6 from tm_rmpt where tm_rmpt."id"=1;
              end if;
              if li_level7>0 then
                set ls_list7=''
              else
                set ls_list7=''
              end if;
            else
              set ls_list6=''
            end if;
          else
            set ls_list5=''
          end if;
        else
          set ls_list4=''
        end if;
      else
        set ls_list3=''
      end if;
    else
      set ls_list2=''
    end if;
  else
    set ls_list1=''
  end if;
  -- tase 1
  if length(ls_list1)>0 then
    set li_pos1=0;
    set li_i1=locate(ls_mask1,'@');
    if li_i1>0 then
      pos1_loop: while li_i1 < length(ls_mask1+'  ') loop
        if substring(ls_mask1,li_i1,1)='@' then
          set li_pos1=li_pos1+1
        else
          set ls_insert='insert into gl_ea_rida2(';
          set ls_select=' select ';
          set ls_from=' from gl_ea_kulkond';
          set ls_group=' group by ';
          set ls_insert=ls_insert+ls_veerg1;
          set ls_select=ls_select+'Left(Left('+ls_list1+'.'+ls_kood1+','+convert(char(2),li_pos1)+')+''                '',16) as klassif1';
          set ls_from=ls_from+' left outer join '+ls_list1+' on gl_ea_kulkond.'+ls_id1+'='+ls_list1+'.'+ls_id1;
          set ls_group=ls_group+'klassif1';
          set ls_insert=ls_insert+',nimi,col01,col02,col03,col04,col05,col06,col07,col08,col09,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20)';
          set ls_select=ls_select+','''',sum(col01),sum(col02),sum(col03),sum(col04),sum(col05),sum(col06),sum(col07),sum(col08),sum(col09),sum(col10),sum(col11),sum(col12),sum(col13),sum(col14),sum(col15),sum(col16),sum(col17),sum(col18),sum(col19),sum(col20)';
          set ls_sql=ls_insert+ls_select+ls_from+ls_group;
          if length(ls_sql)>0 then
            execute immediate ls_sql;
          end if;
          -- tase 2
          if length(ls_list2)>0 then
            set li_pos2=0;
            set li_i2=locate(ls_mask2,'@');
            if li_i2>0 then
              pos2_loop: while li_i2 < length(ls_mask2+'  ') loop
                if substring(ls_mask2,li_i2,1)='@' then
                  set li_pos2=li_pos2+1
                else
                  set ls_insert='insert into gl_ea_rida2(';
                  set ls_select=' select ';
                  set ls_from=' from gl_ea_kulkond';
                  set ls_group=' group by ';
                  set ls_insert=ls_insert+ls_veerg1+','+ls_veerg2;
                  set ls_select=ls_select+'Left(Left('+ls_list1+'.'+ls_kood1+','+convert(char(2),li_pos1)+')+''                '',16) as klassif1,Left(Left('+ls_list2+'.'+ls_kood2+','+convert(char(2),li_pos2)+')+''                '',16) as klassif2';
                  set ls_from=ls_from+' left outer join '+ls_list1+' on gl_ea_kulkond.'+ls_id1+'='+ls_list1+'.'+ls_id1+' left outer join '+ls_list2+' on gl_ea_kulkond.'+ls_id2+'='+ls_list2+'.'+ls_id2;
                  set ls_group=ls_group+'klassif1,klassif2';
                  set ls_insert=ls_insert+',nimi,col01,col02,col03,col04,col05,col06,col07,col08,col09,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20)';
                  set ls_select=ls_select+','''',sum(col01),sum(col02),sum(col03),sum(col04),sum(col05),sum(col06),sum(col07),sum(col08),sum(col09),sum(col10),sum(col11),sum(col12),sum(col13),sum(col14),sum(col15),sum(col16),sum(col17),sum(col18),sum(col19),sum(col20)';
                  set ls_sql=ls_insert+ls_select+ls_from+ls_group;
                  if length(ls_sql)>0 then
                    execute immediate ls_sql;
                  end if;
                  -- tase 3
                  if length(ls_list3)>0 then
                    set li_pos3=0;
                    set li_i3=locate(ls_mask3,'@');
                    if li_i3>0 then
                      pos3_loop: while li_i3 < length(ls_mask3+'  ') loop
                        if substring(ls_mask3,li_i3,1)='@' then
                          set li_pos3=li_pos3+1
                        else
                          set ls_insert='insert into gl_ea_rida2(';
                          set ls_select=' select ';
                          set ls_from=' from gl_ea_kulkond';
                          set ls_group=' group by ';
                          set ls_insert=ls_insert+ls_veerg1+','+ls_veerg2+','+ls_veerg3;
                          set ls_select=ls_select+'Left(Left('+ls_list1+'.'+ls_kood1+','+convert(char(2),li_pos1)+')+''                '',16) as klassif1 ,Left(Left('+ls_list2+'.'+ls_kood2+','+convert(char(2),li_pos2)+')+''                '',16) as klassif2,Left(Left('+ls_list3+'.'+ls_kood3+','+convert(char(2),li_pos3)+')+''                '',16) as klassif3';
                          set ls_from=ls_from+' left outer join '+ls_list1+' on gl_ea_kulkond.'+ls_id1+'='+ls_list1+'.'+ls_id1+' left outer join '+ls_list2+' on gl_ea_kulkond.'+ls_id2+'='+ls_list2+'.'+ls_id2+' left outer join '+ls_list3+' on gl_ea_kulkond.'+ls_id3+'='+ls_list3+'.'+ls_id3;
                          set ls_group=ls_group+'klassif1,klassif2,klassif3';
                          set ls_insert=ls_insert+',nimi,col01,col02,col03,col04,col05,col06,col07,col08,col09,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20)';
                          set ls_select=ls_select+','''',sum(col01),sum(col02),sum(col03),sum(col04),sum(col05),sum(col06),sum(col07),sum(col08),sum(col09),sum(col10),sum(col11),sum(col12),sum(col13),sum(col14),sum(col15),sum(col16),sum(col17),sum(col18),sum(col19),sum(col20)';
                          set ls_sql=ls_insert+ls_select+ls_from+ls_group;
                          if length(ls_sql)>0 then
                            execute immediate ls_sql;
                          end if;
                          -- tase 4
                          if length(ls_list4)>0 then
                            set li_pos4=0;
                            set li_i4=locate(ls_mask4,'@');
                            if li_i4>0 then
                              pos4_loop: while li_i4 < length(ls_mask4+'  ') loop
                                if substring(ls_mask4,li_i4,1)='@' then
                                  set li_pos4=li_pos4+1
                                else
                                  set ls_insert='insert into gl_ea_rida2(';
                                  set ls_select=' select ';
                                  set ls_from=' from gl_ea_kulkond';
                                  set ls_group=' group by ';
                                  set ls_insert=ls_insert+ls_veerg1+','+ls_veerg2+','+ls_veerg3+','+ls_veerg4;
                                  set ls_select=ls_select+'Left(Left('+ls_list1+'.'+ls_kood1+','+convert(char(2),li_pos1)+')+''                '',16) as klassif1 ,Left(Left('+ls_list2+'.'+ls_kood2+','+convert(char(2),li_pos2)+')+''                '',16) as klassif2,Left(Left('+ls_list3+'.'+ls_kood3+','+convert(char(2),li_pos3)+')+''                '',16) as klassif3,Left(Left('+ls_list4+'.'+ls_kood4+','+convert(char(2),li_pos4)+')+''                '',16) as klassif4';
                                  set ls_from=ls_from+' left outer join '+ls_list1+' on gl_ea_kulkond.'+ls_id1+'='+ls_list1+'.'+ls_id1+' left outer join '+ls_list2+' on gl_ea_kulkond.'+ls_id2+'='+ls_list2+'.'+ls_id2+' left outer join '+ls_list3+' on gl_ea_kulkond.'+ls_id3+'='+ls_list3+'.'+ls_id3+' left outer join '+ls_list4+' on gl_ea_kulkond.'+ls_id4+'='+ls_list4+'.'+ls_id4;
                                  set ls_group=ls_group+'klassif1,klassif2,klassif3,klassif4';
                                  set ls_insert=ls_insert+',nimi,col01,col02,col03,col04,col05,col06,col07,col08,col09,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20)';
                                  set ls_select=ls_select+','''',sum(col01),sum(col02),sum(col03),sum(col04),sum(col05),sum(col06),sum(col07),sum(col08),sum(col09),sum(col10),sum(col11),sum(col12),sum(col13),sum(col14),sum(col15),sum(col16),sum(col17),sum(col18),sum(col19),sum(col20)';
                                  set ls_sql=ls_insert+ls_select+ls_from+ls_group;
                                  if length(ls_sql)>0 then
                                    execute immediate ls_sql;
                                  end if;
                                  -- tase 5
                                  if length(ls_list5)>0 then
                                    set li_pos5=0;
                                    set li_i5=locate(ls_mask5,'@');
                                    if li_i5>0 then
                                      pos5_loop: while li_i5 < length(ls_mask5+'  ') loop
                                        if substring(ls_mask5,li_i5,1)='@' then
                                          set li_pos5=li_pos5+1
                                        else
                                          set ls_insert='insert into gl_ea_rida2(';
                                          set ls_select=' select ';
                                          set ls_from=' from gl_ea_kulkond';
                                          set ls_group=' group by ';
                                          set ls_insert=ls_insert+ls_veerg1+','+ls_veerg2+','+ls_veerg3+','+ls_veerg4+','+ls_veerg5;
                                          set ls_select=ls_select+'Left(Left('+ls_list1+'.'+ls_kood1+','+convert(char(2),li_pos1)+')+''                '',16) as klassif1 ,Left(Left('+ls_list2+'.'+ls_kood2+','+convert(char(2),li_pos2)+')+''                '',16) as klassif2,Left(Left('+ls_list3+'.'+ls_kood3+','+convert(char(2),li_pos3)+')+''                '',16) as klassif3,Left(Left('+ls_list4+'.'+ls_kood4+','+convert(char(2),li_pos4)+')+''                '',16) as klassif4,Left(Left('+ls_list5+'.'+ls_kood5+','+convert(char(2),li_pos5)+')+''                '',16) as klassif5';
                                          set ls_from=ls_from+' left outer join '+ls_list1+' on gl_ea_kulkond.'+ls_id1+'='+ls_list1+'.'+ls_id1+' left outer join '+ls_list2+' on gl_ea_kulkond.'+ls_id2+'='+ls_list2+'.'+ls_id2+' left outer join '+ls_list3+' on gl_ea_kulkond.'+ls_id3+'='+ls_list3+'.'+ls_id3+' left outer join '+ls_list4+' on gl_ea_kulkond.'+ls_id4+'='+ls_list4+'.'+ls_id4+' left outer join '+ls_list5+' on gl_ea_kulkond.'+ls_id5+'='+ls_list5+'.'+ls_id5;
                                          set ls_group=ls_group+'klassif1,klassif2,klassif3,klassif4,klassif5';
                                          set ls_insert=ls_insert+',nimi,col01,col02,col03,col04,col05,col06,col07,col08,col09,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20)';
                                          set ls_select=ls_select+','''',sum(col01),sum(col02),sum(col03),sum(col04),sum(col05),sum(col06),sum(col07),sum(col08),sum(col09),sum(col10),sum(col11),sum(col12),sum(col13),sum(col14),sum(col15),sum(col16),sum(col17),sum(col18),sum(col19),sum(col20)';
                                          set ls_sql=ls_insert+ls_select+ls_from+ls_group;
                                          if length(ls_sql)>0 then
                                            execute immediate ls_sql;
                                          end if;
                                          -- tase 6
                                          if length(ls_list6)>0 then
                                            set li_pos6=0;
                                            set li_i6=locate(ls_mask6,'@');
                                            if li_i6>0 then
                                              pos6_loop: while li_i6 < length(ls_mask6+'  ') loop
                                                if substring(ls_mask6,li_i6,1)='@' then
                                                  set li_pos6=li_pos6+1
                                                else
                                                  set ls_insert='insert into gl_ea_rida2(';
                                                  set ls_select=' select ';
                                                  set ls_from=' from gl_ea_kulkond';
                                                  set ls_group=' group by ';
                                                  set ls_insert=ls_insert+ls_veerg1+','+ls_veerg2+','+ls_veerg3+','+ls_veerg4+','+ls_veerg5+','+ls_veerg6;
                                                  set ls_select=ls_select+'Left(Left('+ls_list1+'.'+ls_kood1+','+convert(char(2),li_pos1)+')+''                '',16) as klassif1 ,Left(Left('+ls_list2+'.'+ls_kood2+','+convert(char(2),li_pos2)+')+''                '',16) as klassif2,Left(Left('+ls_list3+'.'+ls_kood3+','+convert(char(2),li_pos3)+')+''                '',16) as klassif3,Left(Left('+ls_list4+'.'+ls_kood4+','+convert(char(2),li_pos4)+')+''                '',16) as klassif4,Left(Left('+ls_list5+'.'+ls_kood5+','+convert(char(2),li_pos5)+')+''                '',16) as klassif5,Left(Left('+ls_list6+'.'+ls_kood6+','+convert(char(2),li_pos6)+')+''                '',16) as klassif6';
                                                  set ls_from=ls_from+' left outer join '+ls_list1+' on gl_ea_kulkond.'+ls_id1+'='+ls_list1+'.'+ls_id1+' left outer join '+ls_list2+' on gl_ea_kulkond.'+ls_id2+'='+ls_list2+'.'+ls_id2+' left outer join '+ls_list3+' on gl_ea_kulkond.'+ls_id3+'='+ls_list3+'.'+ls_id3+' left outer join '+ls_list4+' on gl_ea_kulkond.'+ls_id4+'='+ls_list4+'.'+ls_id4+' left outer join '+ls_list5+' on gl_ea_kulkond.'+ls_id5+'='+ls_list5+'.'+ls_id5+' left outer join '+ls_list6+' on gl_ea_kulkond.'+ls_id6+'='+ls_list6+'.'+ls_id6;
                                                  set ls_group=ls_group+'klassif1,klassif2,klassif3,klassif4,klassif5,klassif6';
                                                  set ls_insert=ls_insert+',nimi,col01,col02,col03,col04,col05,col06,col07,col08,col09,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20)';
                                                  set ls_select=ls_select+','''',sum(col01),sum(col02),sum(col03),sum(col04),sum(col05),sum(col06),sum(col07),sum(col08),sum(col09),sum(col10),sum(col11),sum(col12),sum(col13),sum(col14),sum(col15),sum(col16),sum(col17),sum(col18),sum(col19),sum(col20)';
                                                  set ls_sql=ls_insert+ls_select+ls_from+ls_group;
                                                  if length(ls_sql)>0 then
                                                    execute immediate ls_sql;
                                                  end if;
                                                end if;
                                                set li_i6=li_i6+1
                                              end loop pos6_loop;
                                            -- lopp kui mask6 sisaldab @
                                            end if;
                                          -- lopp ls_list6
                                          end if;
                                        end if;
                                        set li_i5=li_i5+1
                                      end loop pos5_loop;
                                    -- lopp kui mask5 sisaldab @
                                    end if;
                                  -- lopp ls_list5
                                  end if;
                                end if;
                                set li_i4=li_i4+1
                              end loop pos4_loop;
                            -- lopp kui mask4 sisaldab @
                            end if;
                          -- lopp ls_list4
                          end if;
                        end if;
                        set li_i3=li_i3+1
                      end loop pos3_loop;
                    -- lopp kui mask3 sisaldab @
                    end if;
                  -- lopp ls_list3
                  end if;
                end if;
                set li_i2=li_i2+1
              end loop pos2_loop;
            -- lopp kui mask2 sisaldab @
            end if;
          -- lopp ls_list2
          end if;
        end if;
        set li_i1=li_i1+1
      end loop pos1_loop;

      -- kustuta korduvad read
      update gl_ea_rida2 set meet=null where length(trim(meet))=0;
      update gl_ea_rida2 set kulu_obj=null where length(trim(kulu_obj))=0;
      update gl_ea_rida2 set teg_ala=null where length(trim(teg_ala))=0;
      update gl_ea_rida2 set tulu_liik=null where length(trim(tulu_liik))=0;
      update gl_ea_rida2 set projekt=null where length(trim(projekt))=0;
      update gl_ea_rida2 set osakond=null where length(trim(osakond))=0;
      update gl_ea_rida2 set subjekt=null where length(trim(subjekt))=0;
      update gl_ea_rida2 set teg_suund=null where length(trim(teg_suund))=0;
      update gl_ea_rida2 set taot=null where length(trim(taot))=0;
      set ls_sql='insert into gl_ea_rida(meet,kulu_obj,teg_ala,tulu_liik,projekt,osakond,subjekt,teg_suund,taot,nimi,col01,col02,col03,col04,col05,col06,col07,col08,col09,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20)';
      set ls_sql=ls_sql+' select distinct meet,kulu_obj,teg_ala,tulu_liik,projekt,osakond,subjekt,teg_suund,taot,nimi,col01,col02,col03,col04,col05,col06,col07,col08,col09,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20';
      set ls_sql=ls_sql+' from gl_ea_rida2 order by ';
      if length(ls_veerg1)>0 then
        set ls_sql=ls_sql+ls_veerg1 + ' asc'
      end if;
      if length(ls_veerg2)>0 then
        set ls_sql=ls_sql+','+ls_veerg2 + ' asc'
      end if;
      if length(ls_veerg3)>0 then
        set ls_sql=ls_sql+','+ls_veerg3 + ' asc'
      end if;
      if length(ls_veerg4)>0 then
        set ls_sql=ls_sql+','+ls_veerg4 + ' asc'
      end if;
      if length(ls_veerg5)>0 then
        set ls_sql=ls_sql+','+ls_veerg5 + ' asc'
      end if;
      if length(ls_veerg6)>0 then
        set ls_sql=ls_sql+','+ls_veerg6 + ' asc'
      end if;
      if length(ls_veerg7)>0 then
        set ls_sql=ls_sql+','+ls_veerg7 + ' asc'
      end if;
      if length(ls_veerg8)>0 then
        set ls_sql=ls_sql+','+ls_veerg8 + ' asc'
      end if;
      if length(ls_veerg9)>0 then
        set ls_sql=ls_sql+','+ls_veerg9 + ' asc'
      end if;
      if length(ls_sql)>0 then
        execute immediate ls_sql;
      end if;

    -- kustuta mitte trykitakse read
    if li_tryk_meet > 0 then
      delete gl_ea_rida from gl_ea_rida left outer join meetlist on gl_ea_rida.meet=meetlist.meet left outer join kp_sktr_mark on meetlist.meetnr=kp_sktr_mark.xxxnr and kp_sktr_mark.sktrnr=li_tryk_meet where kp_sktr_mark.xxxnr is null and meetlist.meetnr is not null
    end if;
    if li_tryk_kul > 0 then
      delete gl_ea_rida from gl_ea_rida left outer join kullist on gl_ea_rida.kulu_obj=kullist.ptk left outer join kp_sktr_mark on kullist.kulnr=kp_sktr_mark.xxxnr and kp_sktr_mark.sktrnr=li_tryk_kul where kp_sktr_mark.xxxnr is null and kullist.kulnr is not null
    end if;
    if li_tryk_tal > 0 then
      delete gl_ea_rida from gl_ea_rida left outer join tallist on gl_ea_rida.teg_ala=tallist.osa left outer join kp_sktr_mark on tallist.talnr=kp_sktr_mark.xxxnr and kp_sktr_mark.sktrnr=li_tryk_tal where kp_sktr_mark.xxxnr is null and tallist.talnr is not null
    end if;
    if li_tryk_tul > 0 then
      delete gl_ea_rida from gl_ea_rida left outer join tullist on gl_ea_rida.tulu_liik=tullist.osa left outer join kp_sktr_mark on tullist.tulnr=kp_sktr_mark.xxxnr and kp_sktr_mark.sktrnr=li_tryk_tul where kp_sktr_mark.xxxnr is null and tullist.tulnr is not null
    end if;
    if li_tryk_object > 0 then
      delete gl_ea_rida from gl_ea_rida left outer join objlist on gl_ea_rida.projekt=objlist.object left outer join kp_sktr_mark on objlist.objectnr=kp_sktr_mark.xxxnr and kp_sktr_mark.sktrnr=li_tryk_object where kp_sktr_mark.xxxnr is null and objlist.objectnr is not null
    end if;
    if li_tryk_osak > 0 then
      delete gl_ea_rida from gl_ea_rida left outer join osaklist on gl_ea_rida.osakond=osaklist.osak left outer join kp_sktr_mark on osaklist.osaknr=kp_sktr_mark.xxxnr and kp_sktr_mark.sktrnr=li_tryk_osak where kp_sktr_mark.xxxnr is null and osaklist.osaknr is not null
    end if;
    if li_tryk_subject > 0 then
      delete gl_ea_rida from gl_ea_rida left outer join sublist on gl_ea_rida.subjekt=sublist.subject left outer join kp_sktr_mark on sublist.subjectnr=kp_sktr_mark.xxxnr and kp_sktr_mark.sktrnr=li_tryk_subject where kp_sktr_mark.xxxnr is null and sublist.subjectnr is not null
    end if;
    if li_tryk_suund > 0 then
      delete gl_ea_rida from gl_ea_rida left outer join suundlist on gl_ea_rida.teg_suund=suundlist.suund left outer join kp_sktr_mark on suundlist.suundnr=kp_sktr_mark.xxxnr and kp_sktr_mark.sktrnr=li_tryk_suund where kp_sktr_mark.xxxnr is null and suundlist.suundnr is not null
    end if;
    if li_tryk_taot > 0 then
      delete gl_ea_rida from gl_ea_rida left outer join taotlist on gl_ea_rida.taot=taotlist.taot left outer join kp_sktr_mark on taotlist.taotnr=kp_sktr_mark.xxxnr and kp_sktr_mark.sktrnr=li_tryk_taot where kp_sktr_mark.xxxnr is null and taotlist.taotnr is not null
    end if;

    -- asenda nimed
      set ls_sql='update gl_ea_rida,'+ls_list1+' set gl_ea_rida.nimi=Left('+ls_list1+'.nimi,100) where gl_ea_rida.'+ls_veerg1+'='+ls_list1+'.'+ls_kood1;
      execute immediate ls_sql;
      if length(ls_list2)>0 then
        set ls_sql='update gl_ea_rida,'+ls_list2+' set gl_ea_rida.nimi=Left(''  ''+'+ls_list2+'.nimi,100) where gl_ea_rida.'+ls_veerg2+'='+ls_list2+'.'+ls_kood2;
        execute immediate ls_sql;
      end if;
      if length(ls_list3)>0 then
        set ls_sql='update gl_ea_rida,'+ls_list3+' set gl_ea_rida.nimi=Left(''    ''+'+ls_list3+'.nimi,100) where gl_ea_rida.'+ls_veerg3+'='+ls_list3+'.'+ls_kood3;
        execute immediate ls_sql;
      end if;
      if length(ls_list4)>0 then
        set ls_sql='update gl_ea_rida,'+ls_list4+' set gl_ea_rida.nimi=Left(''      ''+'+ls_list4+'.nimi,100) where gl_ea_rida.'+ls_veerg4+'='+ls_list4+'.'+ls_kood4;
        execute immediate ls_sql;
      end if;
      if length(ls_list5)>0 then
        set ls_sql='update gl_ea_rida,'+ls_list5+' set gl_ea_rida.nimi=Left(''        ''+'+ls_list5+'.nimi,100) where gl_ea_rida.'+ls_veerg5+'='+ls_list5+'.'+ls_kood5;
        execute immediate ls_sql;
      end if;
      if length(ls_list6)>0 then
        set ls_sql='update gl_ea_rida,'+ls_list6+' set gl_ea_rida.nimi=Left(''          ''+'+ls_list6+'.nimi,100) where gl_ea_rida.'+ls_veerg6+'='+ls_list6+'.'+ls_kood6;
        execute immediate ls_sql;
      end if;
      if length(ls_list7)>0 then
        set ls_sql='update gl_ea_rida,'+ls_list7+' set gl_ea_rida.nimi=Left(''            ''+'+ls_list7+'.nimi,100) where gl_ea_rida.'+ls_veerg7+'='+ls_list7+'.'+ls_kood7;
        execute immediate ls_sql;
      end if;
    -- lopp kui mask1 sisaldab @ aruannet ei toimu
    end if;
  -- lopp ls_list1 aruannet ei toimu
  end if;

END