ABAP log

January 2, 2007

Cache your data

Filed under: ABAP, SAP — abaplog @ 8:22 pm

Very often I have to extract most of data in some block of our program and later, while processing the data, add some more stuff on demand. This is often done to simplify the initial data extraction and avoid redundancies of data transfer. An example of that can be extracting transactional data like sales or production orders and then, as I prepare data for display, extract things like material descriptions or customer addresses.

Doing SELECTS every time is not a good idea, even if we have a full key. One better way could be collecting all keys in one internal table, getting the needed information in one heavy select and then reading internal table on demand. Another solution is doing SELECTs for single values, but only when that was not done before for the same key, caching the data in some global table or a static one, inside a form.

Below is an example of my preferred way. The form extracts material description, caching the data in a local static table.

form get_material_text
  using p_matnr type matnr
  changing p_maktx type maktx.

  types: begin of lty_text,
           matnr type matnr,
           maktx type maktx,
         end of lty_text.

  types:
    lty_text_tab type hashed table of lty_text
          with unique key matnr.

  data:
    lf_wa type lty_text.

  statics:
    lt_texts type lty_text_tab.

  read table lt_texts with table key matnr = p_matnr into lf_wa.
  if sy-subrc = 0.
    p_maktx = lf_wa-maktx.
  else.
    select single maktx from makt
      into lf_wa-maktx
      where matnr = p_matnr and
            spras = sy-langu.
    if sy-subrc  0.
      lf_wa-maktx = ’Unknown’.
    endif.
    lf_wa-matnr = p_matnr.
    insert lf_wa into table lt_texts.
    p_maktx = lf_wa-maktx.
  endif.

endform.
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: