简体   繁体   English

我不明白 LOOP 中的 READ TABLE 是如何工作的

[英]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.阅读 sap 文档后,我得出的结论是,我需要使用读取表将每次迭代填充到稍后我将处理的工作区域中。

I have declarated the following tables:我已经声明了以下表格:

  • it_sap with the content of a join from VBRK and VBRP tables it_sap带有来自 VBRK 和 VBRP 表的连接内容

  • wa_sap as working area wa_sap作为工作区

  • it_ext with content from an another join from an external database it_ext包含来自外部数据库的另一个连接的内容

  • wa_ext as working area. wa_ext作为工作区。

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 .我知道通过使用条件sy-subrc = 0我可以看到前面的语句是真还是假,但我不太明白它是如何使用READ而不是SELECT steament 的

Thank you guys!谢谢你们!

LOOP AT iterates over all rows of an internal table. LOOP AT迭代内部表的所有行。

READ TABLE retrieves at most one row from an internal table. READ TABLE从内部表中最多检索一行。

sy-subrc provides you details about how well the previous statement worked. sy-subrc为您提供有关上sy-subrc语句工作情况的详细信息。 Its values differ with the statement.它的值因语句而异。 In case of READ TABLE , it tells you whether a row was found ( = 0 ) or not ( <> 0 ).READ TABLE情况下,它会告诉您是否找到了 ( = 0 ) 行( <> 0 )。 The IF sy-subrc = 0 after your READ TABLE thus means "if you found such a row". IF sy-subrc = 0在您的READ TABLE之后意味着“如果您找到这样的行”。

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.正如 József Szikszai 在对您的问题的评论中指出的那样, READ TABLE的条件依赖于wa_ext ,它从未填充在您的示例代码中,因此乍一看,这看起来根本不起作用,或者您的示例不包含一些初始化代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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