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: 

Comparing Application server file and internal table

Former Member
0 Kudos

hi all

Iam new to the world of ABAP. I have a requirement wherein i need to compare data in a internal table and that of the file. I need to extract the delta records( ie the record is found in the internal table but not in the file and vice versa ) and put it in another internal table. Which is the best way to do it?

Awaiting quick reply. Thanks

Nirmal.

5 REPLIES 5

Former Member
0 Kudos

lets say your data in internal table is ITAB1

and your file data in ITAB2.( i am assuming you know the way how to read the records of a file from application server/presentation server,if not let us know).

LOOP AT ITAB1.

READ TABLE ITAB2 WITH KEY LINE = ITAB1-LINE.

IF SY-SUBRC <> 0.

*--ITAB1 RECORD NOT available in ITAB2,so populate to another internal table.

ITAB3 = ITAB1.

APPEND ITAB3.

ENDIF.

ENDLOOP.

*--The similar way you can loop ITAB2 and do a search for ITAB1 and if not found populate that record to ITAB3.

itab1,itab2,itab3 record structure can be like,

DATA : BEGIN OF ITAB1 OCCURS 0,

LINE(1000) TYPE C,

END OF ITAB1.

Regards

Srikanth

Former Member
0 Kudos

Hi

How to check if a record is in the file and in the table depends on how you have defined the structure (of table and file) anyway this is an example:

DATA: FILE(80).

DATA: BEGIN OF ITAB OCCURS 0,

FIELD1,

FIELD2,

FIELDN,

MARK,

END OF ITAB.

DATA: WA LIKE ITAB.

OPEN DATASET FILE FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF SY-SUBRC = 0.

DO.

READ DATASET FILE INTO WA.

IF SY-SUBRC <> 0. EXIT. ENDIF.

CLEAR ITAB.

LOOP AT ITAB WHERE MARK = SPACE.

IF ITAB = WA.

ITAB-MARK = 'X'.

MODIFY ITAB.

EXIT.

ENDIF.

ENDLOOP.

IF ITAB-MARK = SPACE.

  • ----> Create delta with file record (WA)

ENDIF.

ENDDO.

  • Here all records with field MARK is space are the records of internal

  • table not in the file

LOOP AT ITAB WHERE MARK = SPACE.

  • ----> Create delta with table record (ITAB)

ENDLOOP.

ENDIF.

Max

uwe_schieferstein
Active Contributor
0 Kudos

Hello Nimal

First read the file into your itab. For comparison you can use the same logic as change documents are prepared. Assuming both of your itabs are of structure struc_a. Then define the following type:

TYPES: BEGIN OF ty_s_itab_di.  
INCLUDE TYPE struc_a.
TYPES: CHIND  TYPE bu_chind.
TYPES: END OF ty_s_itab_di.
TYPES: ty_t_itab_di  TYPE STANDARD TABLE OF ty_s_itab_di 
                     WITH DEFAULT KEY.
DATA:  
  gt_itab_old  TYPE ty_t_itab_di,

gt_itab_new TYPE ty_t_itab_di.

  • Fill itabs gt_Itab_old with the corresponding data of itab1 and gt_itab_new with the corresponding data of itab2.

*

  • Very important: sort you itabs either by all key fields or by all fields.

*

  • Call function CHANGEDOCUMENT_PREPARE_TABLES with the following parameters:

- CHECK_INDICATOR = ' '

- TABLE_NEW = gt_Itab_new

- TABLE_OLD = gt_itab_old

The function module will remove identical lines from both itabs. New entries in gt_itab_New will have CHIND = 'I' and deleted entries in gt_itab_old will have CHIND = 'D'.

Read the documentation of the function module and play around with it. You will see that this a quite easy yet powerful approach for comparing itabs.

Regards

Uwe

Former Member
0 Kudos

Hi all

Thanks for your quick reply. I want to also extract the changed records( i.e. the record is present in both the internal tables current and old. But some contents of current has changed with respect to the old internal table for the same record). What shall be done in this case?

Nirmal.

0 Kudos

Hello Nirmal

The function module CHANGEDOCUMENT_PREPARE_TABLES returns also this result: in this case the corresponding entry in gt_itab_new has CHIND = 'U'.

Regards

Uwe

PS: I add the documentation of the TABLES parameters.

 Table contains the changed data

     The table structure must begin with the structure specified under
     TABLENAME and an additional processing flag (TYPE C, length 1).

     The table must be passed sorted by key.

     During compression, identical lines in TABLE_OLD and TABLE_NEW are
     deleted and the processing flag set. The following cases are
     distinguished:

     o   Lines which exist with table key in TABLE_NEW, but not in TABLE_OLD:
         Processing flag = "I" for INSERT.

     o   Lines which exist with table key in TABLE_NEW and in TABLE_OLD, the
         contents of the TABLENAME structure fields in TABLE_NEW is not the
         same as in TABLE_OLD: Processing flag = "U" for UPDATE.

     o   Lines which exist with table key in TABLE_NEW and in TABLE_OLD, the
         contents of the TABLENAME structure fields in TABLE_NEW is the same
         as in TABLE_OLD: Delete the line in TABLE_NEW, Delete the line in
         TABLE_OLD.