0

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!

marcdecline
  • 186
  • 4
  • 22
  • Looks correct to me. What exactly is the question? Does the code not do what you want? Or do you need an explanation why it works correctly? – Florian Sep 14 '20 at 10:54
  • Probably an explanation why it works correctly and why READ instead SELECT. Also, the fact of having to use so many tables to do any management makes my hair stand on end. Do you also think that it could be improved? I find it difficult to extrapolate my knowledge in other programming languages ​​to abap – marcdecline Sep 14 '20 at 11:03
  • 2
    I don't understand, what you don't understand, but the READ TABLE conditions are not correct. READ TABLE it_ext INTO wa_ext WITH KEY codalb = wa_ext-codalb... => you are using the wa_ext for reading into the result of the READ TABLE and using the field the wa_ext-codalb as condition for the READ, which field is definetly empty at that point and also does not make much sense to code it that way. shouldn't it be wa_sap-codalb (or something else?) – József Szikszai Sep 14 '20 at 12:40
  • Yes, I had several errors in the code so I thought it was at the conceptual level, that due to not understanding well how the ```READ``` worked, it was not going well. Thanks to your clarification I have seen that yes, the read conditional was wrong. In addition I have also seen other errors in the code that were outside the loop that I have shown you. Whatever the initial question of the thread is already more than answered by @Florian and @József Szikszai, so thanks to both :) – marcdecline Sep 14 '20 at 16:06
  • 1
    `READ TABLE it_ext INTO wa_ext WITH KEY codalb = wa_ext-codalb tipo_documento = wa_ext-tipo_documento.` this piece doesn't make sense, as you are using the same structure for result and for input of READ TABLE, and this READ TABLE will never return anything. Read [READ TABLE](https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-us/abapread_table_outdesc.htm) help more attentively – Suncatcher Sep 22 '20 at 12:09
  • @Suncatcher I didn't see it clear either at first, but declaring previously the fields with the same name in the source table lets me impute them when the key condition is met. This way I save myself declaring one more table in memory. I am finishing this program, so when I finish it I will put the logic and the table declaration so that you can see everything more clearly :) thanks for the documentation, I will need it hehe – marcdecline Sep 22 '20 at 13:13
  • `This way I save myself declaring one more table in memory` not sure what are you saving, the gain from not declaring another var is negligible, but the mess is enormous. – Suncatcher Sep 22 '20 at 13:22
  • This is how the logic of my program finally [looks like](https://pastebin.com/tM4svPSY) – marcdecline Sep 23 '20 at 09:04

1 Answers1

3

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.

Florian
  • 4,821
  • 2
  • 19
  • 44