03-14-2021 6:06 AM
Hi!
When you start this code:
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
TYPES tt_data TYPE STANDARD TABLE OF i WITH EMPTY KEY.
DATA mt_data TYPE tt_data.
METHODS: change_data,
get_reference
RETURNING VALUE(ro_data) TYPE REF TO lcl_app.
METHODS print.
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
METHOD change_data.
LOOP AT get_reference( )->mt_data ASSIGNING FIELD-SYMBOL(<lv_value>).
<lv_value> = <lv_value> + 1.
ENDLOOP.
ENDMETHOD.
METHOD get_reference.
ro_data = me.
ENDMETHOD.
METHOD print.
LOOP AT mt_data INTO DATA(lv_value).
WRITE: / lv_value.
ENDLOOP.
ULINE.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_app) = NEW lcl_app( ).
lo_app->mt_data = VALUE #( ( 1 ) ).
lo_app->change_data( ).
lo_app->print( ).
You will see that mt_data is not changed. Сan anyone explain why?
If you change code to this:
METHOD change_data.
DATA(lo_ref) = get_reference( ).
LOOP AT lo_ref->mt_data ASSIGNING FIELD-SYMBOL(<lv_value>).
<lv_value> = <lv_value> + 1.
ENDLOOP.
ENDMETHOD.
Or this:
METHOD change_data.
LOOP AT CAST lcl_app( get_reference( ) )->mt_data ASSIGNING FIELD-SYMBOL(<lv_value>).
<lv_value> = <lv_value> + 1.
ENDLOOP.
ENDMETHOD.
Everything will be OK
03-14-2021 7:22 AM
maheshkumar.palavalli, Thank you!
I think you're right!
"If the internal table is specified as the return value of a functional method, a constructor expression, or a table expression, the additions ASSIGNING and REFERENCE INTO can also be specified for LOOP (this is not the case with READ TABLE). The internal table is only available while the loop is being processed, which means that all field symbols and reference variables that point to rows in the internal table become invalid when the loop is exited."
https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abaploop_at_itab_result.htm
03-14-2021 6:49 AM
Is your scenario the same as what sap is trying to tell in the help? Internal table only available within the loop means that it is temporary data?(Not sure if that is what they are trying to say, maybe sandra.rossi knows something). I also observed that the field symbol is not accessed outside the loop as they mentioned unlike the loop of a normal table.
"If the internal table is specified as the return value of a functional method, a constructor expression, or a table expression, the additions ASSIGNING and REFERENCE INTO can also be specified for LOOP (this is not the case with READ TABLE). The internal table is only available while the loop is being processed, which means that all field symbols and reference variables that point to rows in the internal table become invalid when the loop is exited."
https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abaploop_at_itab_result.htm
03-14-2021 7:22 AM
maheshkumar.palavalli, Thank you!
I think you're right!
"If the internal table is specified as the return value of a functional method, a constructor expression, or a table expression, the additions ASSIGNING and REFERENCE INTO can also be specified for LOOP (this is not the case with READ TABLE). The internal table is only available while the loop is being processed, which means that all field symbols and reference variables that point to rows in the internal table become invalid when the loop is exited."
https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abaploop_at_itab_result.htm
03-14-2021 7:30 AM
maheshkumar.palavalli, I believe this too, and it's expressed more clearly in the documentation for LOOP AT itab - Basic Form.
If the internal table is specified as the return value or result of a functional method, a constructor expression, or a table expression, the value is persisted for the duration of the loop. Afterwards, it is no longer possible to access the internal table.
So "the value is persisted" means that we get a copy for the loop. We can use the field-symbol inside of the loop, but it refers only to the copy...
(And yes, sandra.rossi knows a great many things)
03-14-2021 9:05 AM