Application Development and Automation 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: 
Read only

loop through table where two fields value are not equal

former_member625844
Participant
0 Likes
5,751

I want to loop through a itab for these rows which value is not equal.

So I wrote code like

LOOP AT gt_alv3 INTO gs_alv3 where product_id <> old_product_id .
......
ENDLOOP.

And it has error. And the following code has no error.

LOOP AT gt_alv3 INTO gs_alv3 where product_id <> gs_alv3-old_product_id .
......
ENDLOOP.

But I wonder is this correct? And by writng this will it only loop through those line under the condition?Thx.

1 ACCEPTED SOLUTION
Read only

michael_piesche
Active Contributor
3,691

Unfortunately, with LOOP you cannot compare two attributes of an internal table against each other.

The first operand (operand1) is a component of the internal table and the second operand (operand2) is any object other then one of the internal table. Please always check the SAP ABAP documentation when trying to understand ABAP statements.

(You can however do this logic with a SELECT statement, maybe that would be another possibility for you.)

So to solve your problem for a given internal table, you need to check for a difference in product_id and old_product_id from within the loop. Is old_product_id always filled, even if it is the same, otherwise you would also have to check for not initial.

LOOP AT gt_alv3 INTO gs_alv3.
  CHECK gs_alv3-product_id <> gs_alv3-old_product_id.
  CHECK gs_alv3-old_product_id IS NOT INITIAL.
  ...
ENDLOOP.
7 REPLIES 7
Read only

Abinathsiva
Active Contributor
3,690

HI,

Create gt_alv_temp as a temporary table

gt_alv_temp[] =  gt_alv3[].
delete gt_alv_temp where product_id ne old_product_id .
loop at gt_alv_temp.

Endloop. 
Read only

GK817
Active Contributor
0 Likes
3,690

What is the error with first code set?

Read only

michael_piesche
Active Contributor
0 Likes
3,690
  • What is the error?
    The community can only help to fix an error, if they know the error and dont have to guess it. Since you are not describing the entire relevant scenario and you are not showing all the relevant coding, answers will be based on guessing only.
  • Is it a syntax check error and the programm doesnt run without fixing the error,
    Or is it a functional error when running the programm and the coding doesnt work as expected?
  • Check the underlying data type for old_product_id and compare it to the one of gs_alv3-old_product_id. They should be the same in order to work properly
  • Is old_product_id accessible? Where has old_product_id been defined vs. gs_alv3, in the same 'block' (event/form/method) or on a global level? Both should be accessible in the same block.
  • How does the old_product_id value get filled vs. the value for gs_alv3-old_product_id. When you check those in the debugger, does one have leading zeros and the other doesnt? In this case the values can only be compared if they are adjusted for either input (leading zeros) or output mode (no leading zeros).
Read only

3,690

It's a syntax check error in Eclipse and I can't activate it with this error.. The error code is

The field "OLD_PRODUCT_ID" is unknown, but there is a field with the similar name "LV_PRODUCT_ID". "LV_PRODUCT_ID".

what I want is in internal table gt_alv3, there are columns product_id and old_product_id, and I want to loop through rows which these two columns has different value. and I'm sure they exist in the table and has same data type.

Read only

michael_piesche
Active Contributor
3,692

Unfortunately, with LOOP you cannot compare two attributes of an internal table against each other.

The first operand (operand1) is a component of the internal table and the second operand (operand2) is any object other then one of the internal table. Please always check the SAP ABAP documentation when trying to understand ABAP statements.

(You can however do this logic with a SELECT statement, maybe that would be another possibility for you.)

So to solve your problem for a given internal table, you need to check for a difference in product_id and old_product_id from within the loop. Is old_product_id always filled, even if it is the same, otherwise you would also have to check for not initial.

LOOP AT gt_alv3 INTO gs_alv3.
  CHECK gs_alv3-product_id <> gs_alv3-old_product_id.
  CHECK gs_alv3-old_product_id IS NOT INITIAL.
  ...
ENDLOOP.
Read only

Sandra_Rossi
Active Contributor
0 Likes
3,690

I post here the important part of the question added by the OP (this comment😞

"It's a syntax check error in Eclipse and I can't activate it with this error.. The error code is

The field"OLD_PRODUCT_ID" is unknown, but there is a field with the similar name "LV_PRODUCT_ID".

"what I want is in internal table gt_alv3, there are columns product_id and old_product_id, and I want to loop through rows which these two columns has different value. and I'm sure they exist in the table and has same data type.

Read only

kartikrajag
Participant
0 Likes
3,690

Per WHERE clause in LOOP documentation, the second operand(old_product_id) can NOT be a component of internal table; but it can be a component of a VARIABLE (gs_alv3).

<<<<<<<<<< portion of text from help.sap.com<<<<<<<<<<<<<<<<

The remaining operands of a relational expression are general expression positions at which any suitable individual operands or expressions can be specified, but no components of the internal table.

>>>>>>>>>end<<<<<<<<<<<<<