Start of Content Area

Comparing Internal Tables  Locate the document in its SAP Library structure

Like other data objects, you can use internal tables as operands in logical expressions.

.... <itab1> <operator> <itab2> ...

For <operator>, all operators listed in the table in Comparisons Between Data Types can be used (EQ, =, NE, <>, ><, GE, >=, LE, <=, GT, >, LT, <).

If you are using internal tables with header lines, remember that the header line and the body of the table have the same name. If you want to address the body of the table in a comparison, you must place two brackets ([ ]) after the table name.

The first criterion for comparing internal tables is the number of lines they contain. The more lines an internal table contains, the larger it is. If two internal tables contain the same number of lines, they are compared line by line, component by component. If components of the table lines are themselves internal tables, they are compared recursively. If you are testing internal tables for anything other than equality, the comparison stops when it reaches the first pair of components that are unequal, and returns the corresponding result.

Example

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA: ITAB LIKE TABLE OF LINE,
JTAB LIKE TABLE OF LINE.

DO 3 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
  APPEND LINE TO ITAB.
ENDDO.

MOVE ITAB TO JTAB.

LINE-COL1 = 10. LINE-COL2 = 20.
APPEND LINE TO ITAB.

IF ITAB GT JTAB.
WRITE / 'ITAB GT JTAB'.
ENDIF.

APPEND LINE TO JTAB.

IF ITAB EQ JTAB.
WRITE / 'ITAB EQ JTAB'.
ENDIF.

LINE-COL1 = 30. LINE-COL2 = 80.
APPEND LINE TO ITAB.

IF JTAB LE ITAB.
WRITE / 'JTAB LE ITAB'.
ENDIF.

LINE-COL1 = 50. LINE-COL2 = 60.
APPEND LINE TO JTAB.

IF ITAB NE JTAB.
WRITE / 'ITAB NE JTAB'.
ENDIF.

IF ITAB LT JTAB.
WRITE / 'ITAB LT JTAB'.
ENDIF.

The output is:

ITAB GT JTAB

ITAB EQ JTAB

JTAB LE ITAB

ITAB NE JTAB

ITAB LT JTAB

This example creates two standard tables, ITAB and JTAB. ITAB is filled with 3 lines and copied to JTAB. Then, another line is appended to ITAB and the first logical expression tests whether ITAB is greater than JTAB. After appending the same line to JTAB, the second logical expression tests whether both tables are equal. Then, another line is appended to ITAB and the third logical expressions tests whether JTAB is less than or equal to ITAB. Next, another line is appended to JTAB. Its contents are unequal to the contents of the last line of ITAB. The next logical expressions test whether ITAB is not equal to JTAB. The first table field whose contents are different in ITAB and JTAB is COL1 in the last line of the table: 30 in ITAB and 50 in JTAB. Therefore, in the last logical expression, ITAB is less than JTAB.