简体   繁体   中英

I don't understand how READ TABLE inside LOOP works

In my code I need to iterate inside a loop, and for each iteration in the loop I have to fill in the data whose values ​​are different in certain cells.

Reading the sap documentation I have come to the conclusion that what I need is to use a read table to fill for each iteration into a working area that later I'll treat.

I have declarated the following tables:

  • it_sap with the content of a join from VBRK and VBRP tables

  • wa_sap as working area

  • it_ext with content from an another join from an external database

  • wa_ext as working area.

This is how my loop looks actually like:


  LOOP AT it_sap INTO wa_sap.

    wa_sap-tipo_documento = wa_sap-xblnr+2(1). " Linea

    tiket = wa_sap-xblnr+9(7).
    creationyear = wa_sap-fkdat+0(4).

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' 
      EXPORTING
        input  = tiket
      IMPORTING
        output = tiket.

    CONCATENATE creationyear '/' wa_sap-vkorg wa_sap-xblnr+7(2) '/' tiket INTO wa_sap-codalb.

    "CLEAR position.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
      EXPORTING
        input  = wa_sap-posnr
      IMPORTING
        output = position.

    READ TABLE it_ext INTO wa_ext
    WITH KEY codalb = wa_ext-codalb
             tipo_documento = wa_ext-tipo_documento.

    IF sy-subrc = 0.
      wa_sap-import_total = wa_ext-import_total.
      wa_sap-import = wa_ext-import.
      wa_sap-price = wa_ext-price.
      wa_sap-price_total = wa_ext-price_total.
      wa_sap-disccount = wa_ext-disccount.
      wa_sap-quantity = wa_ext-quantity.

      MODIFY it_sap FROM wa_sap.

      IF wa_sap-importe_total <> wa_sap-netwr. "AND position = 1 .
        APPEND wa_sap TO it_results.
      ENDIF.

    ENDIF.

  ENDLOOP.

How does it work? I understand that by using the conditional sy-subrc = 0 I can see if the previous statement gives true or false but I don't quite understand how it works by using READ instead SELECT steament .

Thank you guys!

LOOP AT iterates over all rows of an internal table.

READ TABLE retrieves at most one row from an internal table.

sy-subrc provides you details about how well the previous statement worked. Its values differ with the statement. In case of READ TABLE , it tells you whether a row was found ( = 0 ) or not ( <> 0 ). The IF sy-subrc = 0 after your READ TABLE thus means "if you found such a row".

It's hard to understand what your code is supposed to do and whether it does that correctly without some helpful sample data. As József Szikszai points out in the comments to your question, the READ TABLE 's conditions rely on wa_ext , which is never filled in your sample code, so at first glance this looks like it's not working at all or your sample does not include some initialisation code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM