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: 

update itab2 with ref to itab1

Former Member
0 Kudos

Hi all,

Here i have the situation as below, itab2 fields should be updated with 'X' if the corresponding field has value in the itab1. I dont want to write a code to check each field one by one, i hope we need to use field symbols, but i dont have much exp on that. Can some one look in to this. Thanks in advance.

data: begin of itab1 occurs 0, 
        f1(10) type c, 
        f2(10) type c, 
        f3(10) type c, 
        f4(10) type c, 
      end of itab1. 
      
data: begin of itab2 occurs 0, 
        f1     type c, 
        f2     type c, 
        f3     type c, 
        f4     type c, 
      end of itab2.       
      
START-OF-SELECTION. 
      itab1-f1 = 'TEST1'. 
      itab1-f2 = 'TEST1'. 
      itab1-f4 = 'TEST1'. 
   append itab1. clear itab1.    
   
      itab1-f1 = 'TEST2'. 
      itab1-f3 = 'TEST2'. 
      itab1-f4 = 'TEST2'. 
   append itab1. clear itab1.  

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Jaya Sri,

you can use this approach, in order to avoid field by field comparison:


TYPES: BEGIN OF ty_tab1,
        f1(10) TYPE c,
        f2(10) TYPE c,
        f3(10) TYPE c,
        f4(10) TYPE c,
       END OF ty_tab1.

TYPES: BEGIN OF ty_tab2,
        f1 TYPE c,
        f2 TYPE c,
        f3 TYPE c,
        f4 TYPE c,
       END OF ty_tab2.

DATA: itab1 TYPE TABLE OF ty_tab1,
      itab2 TYPE TABLE OF ty_tab2.

DATA: wa1 TYPE ty_tab1,
      wa2 TYPE ty_tab2.

DATA: field_type           TYPE c,
      number_of_components TYPE i.

FIELD-SYMBOLS: <tab1> TYPE ty_tab1,
               <tab2> TYPE ty_tab2,
               <fs>   TYPE ANY,
               <fs2>  TYPE ANY.

wa1-f1 = wa1-f2 = wa1-f4 = 'TEST1'.
APPEND wa1 TO itab1.

wa1-f1 = wa1-f3 = wa1-f4 = 'TEST2'.
APPEND wa1 TO itab1.

DESCRIBE FIELD wa1 TYPE field_type COMPONENTS number_of_components.

LOOP AT itab1 ASSIGNING <tab1>.

  DO number_of_components TIMES.

    ASSIGN COMPONENT sy-index OF STRUCTURE <tab1> TO <fs>.

    IF NOT <fs> IS INITIAL.

      ASSIGN COMPONENT sy-index OF STRUCTURE wa2 TO <fs2>.
      <fs2> = 'X'.

    ENDIF.

  ENDDO.

  APPEND wa2 TO itab2.

ENDLOOP.

Regards

REA

3 REPLIES 3

Former Member
0 Kudos

HI,

You must have to write comparision statement and update itab2 accordingly.

Thx.

Former Member
0 Kudos

Hi,

if you don´t want to check each field in itab1 within a loop, then you must append information in itab2 as you fill the fields in itab1:

START-OF-SELECTION.

itab1-f1 = 'TEST1'.

itab2-f1 = 'X'.

itab1-f2 = 'TEST1'.

itab2-f2 = 'X'.

itab1-f4 = 'TEST1'.

itab2-f4 = 'X'.

append itab1. clear itab1.

append itab2. clear itab2.

Former Member
0 Kudos

Hi Jaya Sri,

you can use this approach, in order to avoid field by field comparison:


TYPES: BEGIN OF ty_tab1,
        f1(10) TYPE c,
        f2(10) TYPE c,
        f3(10) TYPE c,
        f4(10) TYPE c,
       END OF ty_tab1.

TYPES: BEGIN OF ty_tab2,
        f1 TYPE c,
        f2 TYPE c,
        f3 TYPE c,
        f4 TYPE c,
       END OF ty_tab2.

DATA: itab1 TYPE TABLE OF ty_tab1,
      itab2 TYPE TABLE OF ty_tab2.

DATA: wa1 TYPE ty_tab1,
      wa2 TYPE ty_tab2.

DATA: field_type           TYPE c,
      number_of_components TYPE i.

FIELD-SYMBOLS: <tab1> TYPE ty_tab1,
               <tab2> TYPE ty_tab2,
               <fs>   TYPE ANY,
               <fs2>  TYPE ANY.

wa1-f1 = wa1-f2 = wa1-f4 = 'TEST1'.
APPEND wa1 TO itab1.

wa1-f1 = wa1-f3 = wa1-f4 = 'TEST2'.
APPEND wa1 TO itab1.

DESCRIBE FIELD wa1 TYPE field_type COMPONENTS number_of_components.

LOOP AT itab1 ASSIGNING <tab1>.

  DO number_of_components TIMES.

    ASSIGN COMPONENT sy-index OF STRUCTURE <tab1> TO <fs>.

    IF NOT <fs> IS INITIAL.

      ASSIGN COMPONENT sy-index OF STRUCTURE wa2 TO <fs2>.
      <fs2> = 'X'.

    ENDIF.

  ENDDO.

  APPEND wa2 TO itab2.

ENDLOOP.

Regards

REA