01-30-2013 11:23 AM
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.
01-30-2013 11:29 AM
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
01-30-2013 11:29 AM
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
01-30-2013 11:36 AM
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
01-30-2013 1:16 PM
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.
01-30-2013 2:45 PM
Hi Venkat,
Thanks for your answer..
Sy-Tabix is going to give me the record number right?
Amine
01-30-2013 3:11 PM
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
01-31-2013 5:44 AM
01-31-2013 8:57 AM
Thanks Venkat,
I am more interested by the record itself not the record number.
Amine
01-31-2013 5:11 PM
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.
01-30-2013 2:57 PM
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
01-30-2013 3:07 PM
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
01-30-2013 3:17 PM
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
01-30-2013 3:23 PM
01-30-2013 3:29 PM
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
01-30-2013 4:27 PM
@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
01-30-2013 5:11 PM
02-01-2013 12:59 PM
02-01-2013 2:29 PM
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
02-01-2013 3:11 PM
01-30-2013 3:24 PM
02-01-2013 1:12 PM
Yes its right as said by Rudiger.
Right side content should be a value and should not be the name of field.