Skip to Content
-3

loop through table where two fields value are not equal

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.

Add a comment
10|10000 characters needed characters exceeded

  • What is the error with first code set?

  • 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.

Assigned Tags

Related questions

4 Answers

  • Posted on Sep 01 at 05:50 AM

    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.
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 01 at 01:56 AM

    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. 
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 01 at 09:52 PM

    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<<<<<<<<<<<<<

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 01 at 04:36 AM
    -1
    • 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).
    Add a comment
    10|10000 characters needed characters exceeded

    • 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.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.