11

Is there a shortcut or should I just loop at the table and check?

I mean I am using an internal table and I want to check if a value is contained in one field of the internal table and I don't want to loop the table to find the value. Is it possible?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Mtok
  • 1,600
  • 11
  • 36
  • 62

2 Answers2

26

To check for a specific value without doing a loop or transferring values to a work area, you can use the READ statement with the addition TRANSPORTING NO FIELDS like so:

READ TABLE itab WITH KEY FIELD = 'X' TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
  "Read was successful.
ENDIF.

UPDATE: From release 740 ABAP contains the predicate function LINE_EXISTS which is described in this blog post.

It's a built-in function to which you pass a table expression. Using the above example:

IF line_exists( itab[ field = 'X' ] ).
  "Do stuff
ENDIF.

Full syntax of table expression in that predicate see here: https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abentable_expressions.htm

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
mydoghasworms
  • 18,233
  • 11
  • 61
  • 95
2

Selam,

If you are going to use loop in your algorithm, then you can use something like this:

LOOP ITAB WHERE FIELD = 'X'.

"code sample

ENDLOOP.

If you are not going to use a loop in your code, then i don't think there is a specific way to check for a specific value in itab.

Hope its helpful.

Talha

Mtu
  • 643
  • 10
  • 18