1

我在循环语句期间遇到了这个问题。

我有一个循环:

loop at lt assigning <ls> where <condition> (im using loop instead of reaf table coz i need to use GE and LE logical statements)
     if sy-subrc = 0.
      result = <ls>-FIELD.
     else.
      result = ''.
     endif.
endloop.

所以问题是它跳过了 sy-subrc 检查。当循环执行并且没有找到记录(sy-subrc = 4)时,它不会将 '' 分配到结果字段中,而是保留初始语句。

有什么问题?

4

1 回答 1

3

返回码在循环之后设置(forselect和其他循环结构相同)。所以你需要类似的东西:

 loop at lt assigning <ls> where <condition>"(im using loop instead of reaf table coz i need to use GE and LE logical statements)
 endloop.
 if sy-subrc = 0.
  result = <ls>-FIELD.
 else.
  result = ''.
 endif.

在这种情况下,您应该使用read-statement(您提到了 GE/LE 的问题——这可能值得另一个问题)。

现在你循环所有条目。

作为替代方案,您可以在第一次进入后停止:

result = ''. "Initialize for not-found-entry.
loop at lt assigning <ls> where <condition>.
  result = <ls>-FIELD. "Take the found entry
  exit. "Stop after first entry
endloop.

如果没有,exit您将获得最后一个条目。如果订单相关,您还可以添加相关排序。

于 2015-12-06T19:12:17.537 回答