Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

code optimization

Former Member
0 Likes
417

Hi,

I need an assistance to optimise this code as it is slowing down the server.


  select werks from t001w into table itab_werks where werks like 'A%'.
  delete itab_werks where werks cp 'A0*' .
  loop at itab_werks.
    select matnr werks lgort labst insme   from mard into
                  corresponding fields of table imard where
                          werks = itab_werks-werks.
    delete imard where labst = 0  and insme = 0 .
    sort imard by lgort matnr .
    loop at imard .
      select single * from mara where matnr = imard-matnr and
                      mtart = 'FERT'.
      if sy-subrc ne 0.
        continue.
      endif.
      refresh : clr_qty ,  h_status_tab.
      clear clr_qty.
      select * from equi into table itab_equi where matnr = imard-matnr
     loop at itab_equi.
         if itab_equi-werk  eq itab_werks-werks and
             itab_equi-lager eq imard-lgort.
             refresh : h_status_tab , h_status_text_tab.
          clear atwrt2.
          clear colour.
          move itab_equi-charge to colour.
          move colour+2(8) to atwrt2.
          call function 'STATUS_READ'
            exporting
              client           = sy-mandt
              objnr            = itab_equi-objnr
              only_active      = 'X'
            tables
              status           = h_status_tab
            exceptions
              object_not_found = 1
              others           = 2.
          loop at h_status_tab.
            call function 'STATUS_NUMBER_CONVERSION'
              exporting
                language      = sy-langu
                objnr         = itab_equi-objnr
                status_number = h_status_tab-stat
              importing
                txt04         = h_status_text_tab-txt04
              exceptions
                others        = 01.
            if sy-subrc = 0.
              append h_status_text_tab.
            endif.
          endloop.
          clear h_stat_flag.
          loop at h_status_text_tab.
            check h_status_text_tab-txt04 = 'ESTO'.
            h_stat_flag = 'X'.
            exit.
          endloop.
          delete h_status_text_tab where txt04 ne 'ESTO' .
          clear h_status_text_tab .
          read table h_status_text_tab index 1.
          describe table h_status_text_tab lines lin2.
          if lin2 > 0 .
            if h_stat_flag = 'X'.
              move : atwrt2 to clr_qty-atwrt.
                move 1 to clr_qty-qty.
              collect clr_qty.
              clear: clr_qty-qty.
            endif.
          endif.
        else.
          clear eqbs.
          select single * from eqbs where equnr   = itab_equi-equnr
                                and b_werk    = itab_werks-werks
                                and b_lager   = imard-lgort.
          if sy-subrc eq 0 .
            refresh : h_status_tab , h_status_text_tab.
            clear atwrt2.
            clear colour.
            move itab_equi-charge to colour.
            move colour+2(8) to atwrt2.
           call function 'STATUS_READ'
              exporting
                client           = sy-mandt
                objnr            = itab_equi-objnr
                only_active      = 'X'
              tables
                status           = h_status_tab
              exceptions
                object_not_found = 1
                others           = 2.
            loop at h_status_tab.
              call function 'STATUS_NUMBER_CONVERSION'
                exporting
                  language      = sy-langu
                  objnr         = itab_equi-objnr
                  status_number = h_status_tab-stat
                importing
                  txt04         = h_status_text_tab-txt04
                exceptions
                  others        = 01.
              if sy-subrc = 0.
                append h_status_text_tab.
              endif.
            endloop.
            clear h_stat_flag.
            loop at h_status_text_tab.
              check h_status_text_tab-txt04 = 'ESTO'.
              h_stat_flag = 'X'.
              exit.
            endloop.
            delete h_status_text_tab where txt04 ne 'ESTO' .
            clear h_status_text_tab .
            read table h_status_text_tab index 1.
            describe table h_status_text_tab lines lin2.
            if lin2 > 0 .
              if h_stat_flag = 'X'.
                move : atwrt2 to clr_qty-atwrt.
                  move 1 to clr_qty-qty.
                   collect clr_qty.
                clear: clr_qty-qty , clr_qty-atwrt.
              endif .
            endif.
          endif.
        endif .
 endloop.
      loop at clr_qty.
        move imard-matnr to wa-matnr .
        move imard-werks to wa-werks .
        move imard-lgort to wa-lgort.
        move clr_qty-atwrt to  wa-atwrt.
        move clr_qty-qty to wa-cvmla .
        append wa to  itab_zstockdet .
      endloop.
     clear wa.
    endloop.
  endloop.

2 REPLIES 2
Read only

amit_khare
Active Contributor
0 Likes
385

Instead of using so many nested loops ... use FOR ALL ENTERIES

Read only

matt
Active Contributor
0 Likes
385

>

> Instead of using so many nested loops ... use FOR ALL ENTERIES

Inner joins are more efficient.

Don't use tables with header line, use hashed tables, or sorted tables. Break your program down into FORMs.

Read this forum where other people have posted their code asking for tips. They don't vary much. As such, this is a frequently asked and answered question. So I'm locking it.

matt

Edited by: Matt on Mar 23, 2009 4:33 PM