"
Supported by

MOVE-CORRESPONDING between two tables with automatic look up

Abapinho hasn’t been talking a lot about 7.40 because most of the new possibilities have already been widely discussed in other sites and we don’t want to reinvent the wheel.

But, here and there, I find small useful jewels which seem to still be under the radar. This is one of them.

I need to populate the internal table T_DATA with FI document items:


BSEG-BUKRS
BSEG-BELNR
BSEG-GJAHR
BSEG-BUZEI
BSEG-LIFNR
LFA1-NAME1

Let’s assume T_DATA is already filled with data from table BSEG except for column NAME1 which comes from table LFA1. And that we already have table T_LFA1 filled with the required data from table LFA1:


LFA1-LIFNR
LFA1-NAME1

Before 7.40 I would need to do this in order to populate column NAME1 of T_DATA with the contents of table T_LFA1:


LOOP AT t_data ASSIGNING <s_data>.
  READ TABLE t_lfa1 ASSIGNING <s_lfa1> WITH KEY lifnr = <s_data>-lifnr.
  IF sy-subrc = 0.
    <s_data>-name1 = <s_lfa1>-name1.
  ENDIF.
ENDLOOP.

Now take a look at how this can be done in 7.40:


t_data = CORRESPONDING #( t_data FROM t_lfa1 USING lifnr = lifnr ).

How lovely! In a single command, column T_DATA-NAME1 is filled with data from T_LFA1 respecting its key LIFNR. And the command can be enhanced with MAPPING in case some of field names don’t match.

What before required a LOOP, a READ TABLE, auxiliary variables and explicit attributions, is now reduced to a single one-line operation which, as a bonus, is also much simpler to read and understand.

And the new CORRESPONDING command has several extra options. You can define which fields should be copied and which should not. For example, here we just copy NAME1 without overwriting any other field:


t_data = CORRESPONDING #( t_data FROM t_lfa1 USING lifnr = lifnr
                          MAPPING name1 EXCEPT * ).

Greetings from Abapinho.

2 comentários a “MOVE-CORRESPONDING between two tables with automatic look up”

  1. cintia Diz:

    Boa ! E quanto a performance?

  2. Nuno Godinho Diz:

    Olá, não fiz testes de performance. Até agora só usei em tabelas relativamente pequenas. Mas não vejo razão para ser lento visto que a tabela de LOOKUP tem obrigatoriamente de ser SORTED ou HASHED.

Deixe um comentário


About Abapinho
Abapinho runs on WordPress
Articles (RSS) e Comments (RSS).