Better Ways To Read Master Data Text in ABAP

There is a popular (but inefficient) method to read texts of master data in ABAP, which will look like this:

select matnr into gt_itab from mseg where matnr in s_matnr.

loop at gt_itab.
  select single maktx into gt_itab-maktx from makt
  where matnr eq gt_itab-matnr and spras eq sy-langu.

  modify gt_itab.
endloop.

Plesae pardon the bad MSEG query, it is only a simple example 😉

This approach is understandable. If you would bind MSEG & MAKTX with an INNER JOIN query, you would surely miss MSEG entries of materials which don’t have defined text in the logon language. Therefore, you think that you should collect material texts with many SELECT SINGLE queries.

However, there is a better approach. You can also reach MAKT with a LEFT JOIN query, which will look like this:

select mseg~matnr makt~maktx into gt_itab
from
  mseg
  left join makt on makt~matnr eq mseg~matnr
  and makt~spras eq sy-langu
where mseg~matnr in s_matnr.

This approach will make sure that you will get all entries in MSEG, no matter if MATNR has a corresponding text in the logon language or not. Don’t worry, existing texts of materials will surely return from MAKT.

Cool, eh? For more advanced SQL, I recommend reading my book Veritabanı Mantığı.

Here is a second case. Sometimes, you get a list of master data in an internal table (from a function module, for instance) and there is no way to collect texts through JOIN statements. In that case, you would think that the only way out is the following:

loop at gt_itab.
  select single maktx into gt_itab-maktx from makt
  where matnr eq gt_itab-matnr and spras eq sy-langu.

  modify gt_itab.
endloop.

However, this is not true either. If you have 100 lines in GT_ITAB having the same material number, you will read MAKT 100 times and this will decrease performance greatly. An alternative approach to this problem is to use hashed static tables. Check the following code:

loop at gt_itab.
  perform read_matnr_single
  using gt_itab-matnr changing gt_itab-maktx.

  modify gt_itab.
endloop.

(……)

*&———————————————————————*
*& Form read_MATNR_single
*&———————————————————————*
* Reads a single record from Material Master Data
*———————————————————————-*
* –>I_MATNR Material
* <–E_MAKTX Material text
*———————————————————————-*
FORM read_MATNR_single USING I_MATNR CHANGING E_MAKTX.

  TYPES:
    BEGIN OF t_MATNR,
      MATNR TYPE MARA-MATNR,
      MAKTX TYPE MAKT-MAKTX,
    END OF t_MATNR.

  STATICS:
    lt_MATNR type hashed table of t_MATNR
    with unique key MATNR.
  data: lf_MATNR like line of lt_MATNR.

  clear E_MAKTX.

  READ TABLE lt_MATNR into lf_MATNR WITH TABLE KEY MATNR = I_MATNR
  .
  IF sy-subrc NE 0.
    CLEAR lf_MATNR.
    lf_MATNR-MATNR = I_MATNR.
    SELECT SINGLE MAKTX INTO lf_MATNR-MAKTX FROM MAKT
    WHERE MATNR EQ I_MATNR
      AND SPRAS EQ SY-LANGU
    .
    insert lf_MATNR into table lt_MATNR.
  ENDIF.

  E_MAKTX = lf_MATNR-MAKTX.

ENDFORM.

Using this technique, you can simply cache material texts and you will never read MAKT twice for the same material. Besides; since performance of hashed tables are independent from data size, you will always find the text of your material very quickly.

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