Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Compare data between two internal tables

Former Member
0 Kudos

Hi experts

I have two internal table, first contains username and a code, second table I have in one record all codes that I need to check on first table.

I mean:

TABLE 1

username code

XXX NA

YYY PA

ZZZ DA

TABLE 2

code1 code2 code3 code4 code5 code6 code7 code8

LA CA NA PA DA XA

How can I check codes in table 1 match in table 2?

Thanks un advance.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

You can pass the codes to a ranges and verify.

RANGES: r_code FOR TABLE1-CODE.

r_code-SIGN = 'I'.
r_code-OPTION = 'EQ'.
r_code-LOW = TABLE2-CODE1.
APPEND r_code.

r_code-SIGN = 'I'.
r_code-OPTION = 'EQ'.
r_code-LOW = TABLE2-CODE2.
APPEND r_code.

r_code-SIGN = 'I'.
r_code-OPTION = 'EQ'.
r_code-LOW = TABLE2-CODE2.
APPEND r_code.

5 REPLIES 5

Former Member
0 Kudos

You can pass the codes to a ranges and verify.

RANGES: r_code FOR TABLE1-CODE.

r_code-SIGN = 'I'.
r_code-OPTION = 'EQ'.
r_code-LOW = TABLE2-CODE1.
APPEND r_code.

r_code-SIGN = 'I'.
r_code-OPTION = 'EQ'.
r_code-LOW = TABLE2-CODE2.
APPEND r_code.

r_code-SIGN = 'I'.
r_code-OPTION = 'EQ'.
r_code-LOW = TABLE2-CODE2.
APPEND r_code.

marcelo_ramos
Active Contributor
0 Kudos

Hi Mgg,

Try to do as following:


"// Internal Tables with User Name and Code
DATA: BEGIN OF TableUser OCCURS 10,
        user TYPE char10,
        code TYPE char10,
      END OF TableUser.

"// Internal Table with Codes
DATA: BEGIN OF TableCode OCCURS 10,
        code1 TYPE char10,
        code2 TYPE char10,
        code3 TYPE char10,
        code4 TYPE char10,
        code5 TYPE char10,
      END OF TableCode.

"// Variable Auxiliar
DATA Aux TYPE n.

"// Dinamyc Field
FIELD-SYMBOLS <fsCode> TYPE char10.

START-OF-SELECTION.

  LOOP AT TableUser.
    "// Check Component Position
    Aux = sy-tabix MOD 2.
    IF Aux IS INITIAL.
      ASSIGN COMPONENT sy-tabix OF STRUCTURE TableCode TO <fsCode>.
      IF sy-subrc IS INITIAL.
        IF TableUser-code EQ <fsCode>.
          "//Do Something ........
        ENDIF.
      ENDIF.
    ENDIF.
  ENDLOOP.

I hope it helps you !

Good Look.

Marcelo Ramos

Former Member
0 Kudos

REPORT ZEXAMPLE01.

DATA:BEGIN OF ITAB1 OCCURS 0,

UNAME(10) TYPE C,

CODE(2) TYPE C,

END OF ITAB1.

DATA:BEGIN OF ITAB2 OCCURS 0,

CODE1(2) TYPE C,

CODE2(2) TYPE C,

CODE3(2) TYPE C,

CODE4(2) TYPE C,

END OF ITAB2.

RANGES:R_CODE FOR ITAB1-CODE.

ITAB1-UNAME = 'XXX'.

ITAB1-CODE = 'NA'.

APPEND ITAB1.

CLEAR ITAB1.

ITAB1-UNAME = 'YYY'.

ITAB1-CODE = 'PA'.

APPEND ITAB1.

CLEAR ITAB1.

ITAB1-UNAME = 'ZZZ'.

ITAB1-CODE = 'DA'.

APPEND ITAB1.

CLEAR ITAB1.

ITAB2-CODE1 = 'LA'.

ITAB2-CODE2 = 'CA'.

ITAB2-CODE3 = 'NA'.

ITAB2-CODE4 = 'DA'.

APPEND ITAB2.

CLEAR ITAB2.

LOOP AT ITAB1.

WRITE:/ ITAB1-UNAME,ITAB1-CODE.

ENDLOOP.

SKIP 2.

LOOP AT ITAB2.

WRITE:/ ITAB2-CODE1,ITAB2-CODE2,ITAB2-CODE3,ITAB2-CODE4.

ENDLOOP.

SKIP 2.

LOOP AT ITAB1.

LOOP AT ITAB2.

IF ITAB2-CODE1 = ITAB1-CODE OR ITAB2-CODE2 = ITAB1-CODE OR ITAB2-CODE3 = ITAB1-CODE OR ITAB2-CODE4 = ITAB1-CODE.

WRITE:/ ITAB1-UNAME,ITAB1-CODE.

ENDIF.

ENDLOOP.

ENDLOOP.

OUTPUT

XXX NA

YYY PA

ZZZ DA

LA CA NA DA

XXX NA

ZZZ DA

Former Member
0 Kudos

Hi..

Use ranges and do compare..with ur first internal table.

Former Member
0 Kudos

Hi,

You Can Compare Like this

Loop at itab1 into wa1.

Loop at itab2 into wa2.

if wa1-code = wa2-code.

write: wa2-code.

endif.

Endloop.

endloop.

Reward points if helpful.

Sujit