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 2 columns with abap

amine_lamkaissi
Active Contributor
0 Kudos

Hi experts,

Is there any statement in abap which allows to compare 2 columns of an internal table?

If i take the follwing example:

Column A

Column B

A

A

A

A

A

A

B

A

At the end, I want to see that record B is present in Column A but not in B.

Thanks.

Amine

PS : To moderators, no need to lock my thread, i am junior consultant on BW side and i want to improve my knowledge in Abap and learn form experienced Abap consultans.

Thanks for your understanding.

1 ACCEPTED SOLUTION

rdiger_plantiko2
Active Contributor
0 Kudos

Amine:

Either you are looking for a solution with maximum elegance - or I don't understand your problem.

Here would be my solution. Not elegant. But straightforward.

field-symbols: <ls_line> type (line structure of itab)

loop at itab assigning <ls_line>.

  if <ls_line>-column_a ne <ls_line>-column_b.

    write: / sy-tabix, <ls_line>-column_a, <ls_line>-column_b. 

  endif.

endloop.

Regards,
Rüdiger

20 REPLIES 20

rdiger_plantiko2
Active Contributor
0 Kudos

Amine:

Either you are looking for a solution with maximum elegance - or I don't understand your problem.

Here would be my solution. Not elegant. But straightforward.

field-symbols: <ls_line> type (line structure of itab)

loop at itab assigning <ls_line>.

  if <ls_line>-column_a ne <ls_line>-column_b.

    write: / sy-tabix, <ls_line>-column_a, <ls_line>-column_b. 

  endif.

endloop.

Regards,
Rüdiger

0 Kudos

Hi Rüdiger,

Thanks for your answer. I want really to improve my abap skills, it's very usefull on BW side

Ok, I heard about an Abap option Comparing fields...

What do you think about it in my case?

Amine

PS : I like elegance too

Former Member
0 Kudos

Hi Amine,

Consider your internal table as ITAB_COLUMN and work area as WA_COLUMN.

Suppose your internal table contains two fields namely column1 and column2.

loop at itab_columns into wa_columns.

   if wa_column-column1 NE wa_column-column2

      write:/ wa_column-coumn1,

                wa_column-column2,

                sy-tabix.

endif.

Here sy-tabix indicates in which record the contents are mismatching.

0 Kudos

Hi Venkat,

Thanks for your answer..

Sy-Tabix is going to give me the record number right?

Amine

0 Kudos

Hi Amine,

U can acheive the requirement. U r right -> sy-tabix will give record no in a Loop / Endloop

1. If you want to compare the records for Col 1 with Col 2 -> U can use a loop statement as explained above.

2. If you just want to see whether 'B' is present in Col 1 and not in Col 2, Try this :

read table t_itab into wa_itab with key A = 'B'.  " A is the column name
IF sy-subrc eq 0.
   write 'present in Column A'.
   else.
     write 'absent'.
  ENDIF.


read table t_itab into wa_itab with key B = 'B'.  " B is the column name
IF sy-subrc eq 0.
   write 'present in Column B'.
   else.
     write 'absent'.
  ENDIF.

Thanks

Vivek

0 Kudos

Yes Amine, sy-tabix gives the record number.

0 Kudos

Thanks Venkat,

I am more interested by the record itself not the record number.

Amine

0 Kudos

Amine,

Thats why we are mentioning the record contents after write statment.

Using write statement we can display the contents of the record as per the requirement.

raymond_giuseppi
Active Contributor
0 Kudos

You can also reduce record read executing your check in the LOOP statement,

LOOP AT itab ASSIGNING <ls_line> WHERE column_a NE column_b.

  WRITE: / sy-tabix, <ls_line>-column_a, <ls_line>-column_b.

ENDLOOP.

Regards,

Raymond

0 Kudos

Thanks for your answer Raymond.

And what about if i want to store the records which are differente (in the line 2 of your code) in an internal table to use them later.

Thanks.

Amine

0 Kudos

Hi,

You could do the following:

Just include one more new field into your internal table 'DELFLAG' of type CHAR01.

LOOP AT itab ASSIGNING <ls_line> WHERE column_a NE column_b.  

  <ls_line>-delflag = 'X'.

ENDLOOP.

DELETE itab WHERE delflag NE 'X'.

Regards,

Kartik

0 Kudos

Celever

Thanks.

Amine

0 Kudos

Just put the append to another table in the loop

LOOP AT itab INTO workarea WHERE column_a NE column_b. 

  APPEND workarea TO itab2.

ENDLOOP. 

or even

DELETE itab WHERE column_a EQ column_b.

Regards,

Raymond

0 Kudos

@Raymond - elegant, but won't work:

loop at ... where COMP_A NE COMP_B .

will give a syntax error "Field COMP_B unknown. It is neither declared nor defined in one of the specified tables."

It's explicitly mentioned in the ABAP syntax docu for loop, addition "WHERE logexp", that component names of the current table can only appear on the left hand side of an operator.

If you are unsure with a solution, try it in the system before posting it. Otherwise you will mislead other people.

Kind regards.

Rüdiger

0 Kudos

WHERE COLUMN_A NE COLUMN_B won't work.

0 Kudos

Why Rudiger?

Could you explain please?

Thanks.

Amine

0 Kudos

Amin, you ask why it is not possible?

Because it is not implented.

In the where condition of a loop, you may use the component name of the table only in the first position of operators. If you think about it, this can be justified. What if you had a variable which by chance has the same name like the component you would like to address (like COLUMN_B in your example): What value should be used for the comparison: The value of the variable COLUMN_B or the value of the component COLUMN_B of the current table line? To avoid such ambiguities, the component names are only allowed on the left hand side of the operator.

Look at the ABAP Syntax docu, Statement "LOOP" details on the condition:

Static WHERE condition. All rows are processed for which the condition after WHERE is met. You can specify WHERE for all table categories.

You can specify a logical expression log_exp after WHERE in which the first operand of each operation is a component of the internal table. You cannot specify a predicate function. The components of the internal table must be specified as individual operands and not as part of an expression. You cannot use parenthesized character-like data objects to specify a component dynamically here. The remaining operands of a comparison are general expression positions at which any suitable inidividual operands or expressions can be specified, but no components of the internal table. All logical expressions are possible except IS ASSIGNED, IS SUPPLIED, and the obsolete IS REQUESTED. The specified components can have any data type. The relevant comparison rules apply to the evaluation.

Regards,

Rüdiger

0 Kudos

Thanks for your explanation Rüdiger.

Amine

amine_lamkaissi
Active Contributor
0 Kudos

Thanks to all for your contributions.

Amine

0 Kudos

Yes its right as said by Rudiger.

Right side content should be a value and should not be the name of field.