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

ABAP - 7.4 new syntax for LOOP AND READ

ishwarya_doss
Participant
79,114

Hi Experts,

Could you pls share ABAP 7.4 new syntax for loop and read?

I've used for in iterations for loop. But how to include read in the loop ?

Thanks in Advance

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
0 Likes
46,166

The error you mention is because your code is completely meaningless. Let me explain what you should indicate.

gt_fin = VALUE tt_fin( 
         FOR lw_j IN lt_join
         ( line to be added, must be like line type of tt_fin ) ).

Possibly, you want something like that:

TYPES : BEGIN OF ty_fin,
          sernr TYPE string,
          matnr TYPE string,
        END OF ty_fin,
        tt_fin TYPE STANDARD TABLE OF ty_fin WITH EMPTY KEY.
DATA(lt_join) = VALUE tt_fin( ( ) ).
DATA(lt_ser) = VALUE tt_fin( ).
DATA(gt_fin) = VALUE tt_fin(
         FOR lw_j IN lt_join
         ( matnr = VALUE #( lt_ser[ sernr = lw_j-sernr ]-matnr OPTIONAL )
           sernr = lw_j-sernr
         ) ).

You must define a standalone type for lines of lt_ser (I presume it's named ty_ser). If lt_ser is declared inline, you can do like that after lt_ser inline declaration:

TYPES ty_ser LIKE LINE OF lt_ser.
22 REPLIES 22
Read only

FredericGirod
Active Contributor
46,166

Maybe if you post your code it will be easier to understand your objective

Read only

ishwarya_doss
Participant
0 Likes
46,166

Thanks for your quick response. Heres the sample old way of coding.

LOOP AT lt_join INTO DATA(wa_join).

READ TABLE lt_ser INTO DATA(wa_ser) WITH KEY sernr = wa_join-serial.
IF sy-subrc NE 0.

append xxx to yyy.

endif.

endloop.

Read only

ishwarya_doss
Participant
0 Likes
46,167

Tried like this. But its not working.

gt_fin = VALUE tt_fin(
FOR lw_j IN lt_join
LET <ser> = lt_ser[ sernr = lw_j-serial ]
IN (
equnr = lw_j-equnr
) ).

Pls suggest

Read only

maheshpalavalli
Active Contributor
46,167

Hi anabaperlife,

Which data you want to fill into the final internal table(yyy)..? I mean what are the values in xxx(wa_join or wa_ser or both)?

-Mahesh

Read only

BiberM
Contributor
46,167

First of all: the ABAP keyword documentation is available online at https://help.sap.com/doc/abapdocu_740_index_htm/7.40/en-US/index.html. Especially the release specific content is of interest.

To answer your question:

gt_fin = VALUE tt_fin(

FOR lw_j IN lt_join

( lt_ser[ sernr = lw_j-serial ] )

).

This should work. Please note the brackets which state the line creation.

Read only

FredericGirod
Active Contributor
46,167

You scratch the internal table result, you need a BASE or to append the result

Read only

ishwarya_doss
Participant
0 Likes
46,167

I need to fill the loop table's values or from both sometimes. Do base keyword needed?

Read only

ishwarya_doss
Participant
0 Likes
46,167

I tried this. But getting ty_fin cannot be converted to lt_ser error.

gt_fin = VALUE tt_fin(

FOR lw_j IN lt_join
( lt_ser[ sernr = lw_j-serial ] )

equnr = lw_j-equnr
matnr = <ser>-matnr
sernr = lw_j-serial
sap = | ESTO - In { p_werks } warehouse |
snacc = | Not available in SNACC Table - ZZSNT_SERTAB |
&& |for given plant { p_werks } |
).

Read only

Sandra_Rossi
Active Contributor
46,167

Please explain what error you get, if it's a syntax error or a run time error, etc. Do you mean there's a line in lt_ser which doesn't exist and that fails? (if yes, use matnr = VALUE ty_ser( lt_ser[ ... ] OPTIONAL )-matnr ).

People try random answers because you don't explain well.

Read only

ishwarya_doss
Participant
0 Likes
46,167

Thanks Sandra..It is giving syntax error - ty_fin cannot be converted to lt_ser .

Ty_fin is the type of gt_fin , lt_Ser is an inline table. SHould i need to declare it before? TIA

Read only

ishwarya_doss
Participant
0 Likes
46,167

Also, this is my requirement.

Loop main table.

read sub table with key = main-key.

if sy-subrc NE 0. - I'm doing negative check as i cant have negative case in READ .

Is it possible in new way? If there are no matching keys in sub table i need to fill it in my final itab.

Read only

maheshpalavalli
Active Contributor
0 Likes
46,167

You can try like below to check using the new constructor expressions (FLTER & CORRESPONDING)

    TYPES:
      BEGIN OF line1,
        field1 TYPE i,
        field2 TYPE c LENGTH 1,
      END OF line1,
      tt_line1 TYPE SORTED TABLE OF line1 WITH NON-UNIQUE KEY field1,
      BEGIN OF line2,
        field1 TYPE i,
        field3 TYPE i,
      END OF line2,
      tt_line2 TYPE SORTED TABLE OF line2 WITH NON-UNIQUE KEY field1,
      BEGIN OF line3,
        field1 TYPE i,
        field2 TYPE c LENGTH 1,
        field3 TYPE i,
      END OF line3,
      tt_line3 TYPE STANDARD TABLE OF line3 with DEFAULT KEY.


    DATA(itab1) = VALUE tt_line1(
      ( field1 = 1  field2 = ' ' )
      ( field1 = 3  field2 = 'X' )
      ( field1 = 12 field2 = '4' )
      ( field1 = 11 field2 = 'X' ) ).


    DATA(itab2) = VALUE tt_line2(
      ( field1 = 1  field3 = 1  )
      ( field1 = 1  field3 = 3  )
      ( field1 = 3  field3 = 4  ) ).


    DATA(itab3) = CORRESPONDING tt_line3( FILTER tt_line1( itab1 EXCEPT IN itab2
                        WHERE field1 = field1 ) ).

The internal tables that you are using should be of type sorted tables in FILTER.

-Mahesh

Read only

Sandra_Rossi
Active Contributor
0 Likes
46,167

The error you mention is because your code is completely meaningless. Let me explain what you should indicate.

gt_fin = VALUE tt_fin( 
         FOR lw_j IN lt_join
         ( line to be added, must be like line type of tt_fin ) ).

Possibly, you want something like that:

TYPES : BEGIN OF ty_fin,
          sernr TYPE string,
          matnr TYPE string,
        END OF ty_fin,
        tt_fin TYPE STANDARD TABLE OF ty_fin WITH EMPTY KEY.
DATA(lt_join) = VALUE tt_fin( ( ) ).
DATA(lt_ser) = VALUE tt_fin( ).
DATA(gt_fin) = VALUE tt_fin(
         FOR lw_j IN lt_join
         ( matnr = VALUE #( lt_ser[ sernr = lw_j-sernr ]-matnr OPTIONAL )
           sernr = lw_j-sernr
         ) ).

You must define a standalone type for lines of lt_ser (I presume it's named ty_ser). If lt_ser is declared inline, you can do like that after lt_ser inline declaration:

TYPES ty_ser LIKE LINE OF lt_ser.
Read only

0 Likes
46,167

Thanks Sandra.. But could you pls clarify me on this..

gt_fin =VALUE tt_fin(FOR lw_j IN lt_join
         ( equnr = lw_j-equnr
           matnr =VALUE ty_ser( lt_ser[...]OPTIONAL)-matnr
           sernr = lw_j-serial
           sap=| ESTO -In{ p_werks } warehouse |
           snacc =|Not available in SNACC Table- ZZSNT_SERTAB |&&|for given plant { p_werks }|)).

Is this  matnr =VALUE ty_ser( lt_ser[...]OPTIONAL)-matnr like read table lt_Ser with matnr? Im not sure lt_ser[...] what should be filled here :(
Read only

0 Likes
46,167

Sorry, corrected (same as what you proposed). I guess that lt_ser contains components sernr and matnr.

Read only

0 Likes
46,167

thanks for your response.

But i got ITAB_LINE_NOT_FOUND run time error for the below code.

TYPES ty_ser LIKE LINE OF lt_ser.

gt_fin = VALUE tt_fin(
FOR lw_j IN lt_join
( equnr = lw_j-equnr
matnr = lw_j-matnr
sernr = VALUE ty_ser( lt_ser[ sernr = lw_j-serial ] ) - lw_j-serial
sap = | ESTO - in { p_werks } warehouse |
snacc = | Not available in SNACC table - ZZSNT_SERTAB |
&& |for given plant { p_werks } | )
).

Read only

46,166

I have added all the "fixture" so that I can test myself. See my answer corrected.

Read only

46,166

You should try by yourself. You'll learn quicker.

Now, about:

VALUE#( lt_ser[ sernr = lw_j-sernr ]-matnr OPTIONAL )

it's the same as:

lt_ser[ sernr = lw_j-sernr ]-matnr

except that CX_SY_ITAB_LINE_NOT_FOUND doesn't occur, it's replaced with initial value.

As Michael said, look at the documentation:

https://help.sap.com/doc/abapdocu_740_index_htm/7.40/en-US/index.html.

Especially, table expressions: https://help.sap.com/doc/abapdocu_753_index_htm/7.53/en-US/index.htm?file=abentable_expressions.htm

Read only

ishwarya_doss
Participant
0 Likes
46,166

Thanks a lot , Sandra for your patience in helping me.

Just a doubt.

Here i have defined tt_Fin. But lt_Ser is of a different type as it is inline. Will this work only if both are of same type?

DATA(gt_fin) = VALUE tt_fin(FOR lw_j IN lt_join
         ( matnr =VALUE#( lt_ser[ sernr = lw_j-sernr ]-matnr OPTIONAL)
           sernr = lw_j-sernr  )).
Read only

ishwarya_doss
Participant
0 Likes
46,166

Thanks sandra.. Should lt_Ser should be of same type always?

Read only

ishwarya_doss
Participant
0 Likes
46,166

DATA(gt_fin)=VALUE tt_fin(FOR lw_j IN lt_join

         ( matnr =VALUE#( lt_ser[ sernr = lw_j-sernr ]-matnr OPTIONAL)
           sernr = lw_j-sernr  )).

As im confused,Would be so helpful if you could just let me know what is this doing. so that i can use it in other cases by my own. Thanks
Read only

michael_piesche
Active Contributor
0 Likes
46,166

If your requirement is to make a joined table out of a loop through table1 and joining it with table2, assuming there is one matching row (otherwise values from tabl2 are initial), you can do it this way:

DATA(itab3) = VALUE ty_itab(
      FOR wa IN itab1 
        ( key  = wa-key
          valx = wa-valx
          valy = VALUE #( itab2[ key =  wa-key ]-valy OPTIONAL ) ) ).<br>