How to read more than one record with READ TABLE?

Asked

Viewed 777 times

3

It is possible to make a READ TABLE in ABAP to read more than one line?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

3 answers

2

There is no way to do it directly, you have to use a loop. Example from documentation:

DATA itab TYPE STANDARD TABLE OF i
          WITH EMPTY KEY
          WITH NON-UNIQUE SORTED KEY sort_key COMPONENTS table_line.

itab = VALUE #( ( 2 ) ( 5 ) ( 1 ) ( 3 ) ( 4 ) ).

DATA(output) = ``.
DATA(idx) = lines( itab ).
WHILE idx > 0.
  READ TABLE itab INDEX idx USING KEY sort_key
             ASSIGNING FIELD-SYMBOL(<fs>).
  idx = idx  - 1.
  CHECK <fs> > 2.
  output = output && <fs> && ` `.
ENDWHILE.

cl_demo_output=>display( output ).

I put in the Github for future reference.

0

As friends have mentioned before, the expression READ TABLE in the ABAP language is just a SELECT SINGLE, if we are going to exchange for kids...

Where ALWAYS will bring only ONE record each time it is processed, the big difference between SELECT SINGLE and READ TABLE is that in the first, the data search is done in the database and the second is done by the internal table (only at runtime ), based on this. if you need to "join" two tables for example, it is very simple...

Let’s assume that you have the need to display in a report the data of Material Code (MARA-MATNR), Creation Date (MARA-ERSDA) and finally the material DESCRIPTION (MAKT-MAKTX) in this case, we need to join two tables, for complete the information we need for the display:

The code would basically look like this:

TYPES: BEGIN OF y_saida_relatorio,
      matnr TYPE mara-matnr,
      ersda TYPE mara-ersda,
      maktx TYPE makt-maktx,
   END OF y_saida_relatorio.
DATA: w_saida_relatorio TYPE y_saida_relatorio,
      t_saida_relatorio TYPE TABLE OF y_saida_relatorio.
* Selecionando dados do banco de dados
SELECT matnr,
       ersda
  FROM mara
INTO TABLE @DATA(t_mara).
IF sy-subrc IS INITIAL.
SELECT matnr,
spras,
maktx
FROM makt
INTO TABLE @DATA(t_makt)
FOR ALL ENTRIES IN @t_mara
WHERE matnr EQ @t_mara-matnr
AND spras EQ 'PT'.
IF NOT sy-subrc IS INITIAL.
FREE t_makt[].
ENDIF.
ENDIF.
* Selecionando dados da minha tabela interna (Existente somente em tempo de processamento)
SORT: t_mara BY matnr,
t_makt BY matnr spras.
LOOP AT t_mara
INTO DATA(w_mara).
* Buscando 1 informação por vez na tabela interna
READ TABLE t_makt INTO DATA(w_makt) WITH KEY matnr = w_mara-matnr
                                                  BINARY SEARCH.
IF sy-subrc IS INITIAL.
w_saida_relatorio-matnr = w_mara-matnr.
w_saida_relatorio-ersda = w_mara-ersda.
w_saida_relatorio-maktx = w_makt-maktx.
APPEND w_saida_relatorio TO t_saida_relatorio.
CLEAR w_saida_relatorio.
ENDIF.
ENDLOOP.

0

Read Table is made to select only 1 row from a transparent table. You need to use a loop to select the desired content from one table to another.

inserir a descrição da imagem aqui

  • Hello @Igor. Avoid placing code images. Edit your answer and put the structured code there instead of the image.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.