从循环中的内部表中删除当前行 [英] Delete the current row from an internal table in a loop

查看:102
本文介绍了从循环中的内部表中删除当前行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在遍历内部表时安全地删除活动行吗?

例如,考虑以下代码:

LOOP AT lt_itab INTO ls_wa.如果 [...] ." 在DELETE lt_itab WHERE"中无法完成的检查删除 lt_itab INDEX sy-tabix"或者从 ls_wa 中删除 lt_itab.万一.端环.

像这样删除记录是否安全,或者这个逻辑会不会按预期运行?

我应该将行的唯一标识符存储在临时 itab 中并在循环后运行 DELETE lt_itab WHERE 吗?

我认为对当前迭代中加载的记录以外的记录进行删除操作肯定会导致问题,但我不确定这是否有效,更不用说好的做法了.

解决方案

它是否安全在很大程度上取决于您的编码技能.它有一个已定义的结果,正确使用命令取决于您.如果在循环内的 DELETE 语句之后没有其他事情发生,通常是安全的.您可以在删除后立即发出 CONTINUE 语句以确保确实如此.

不要使用 DELETE lt_itab INDEX sy-tabix. 如果你在支票中使用了一些改变 sy-tabix 的语句作为一边效果(例如,在检查表中查找某些条目 - 或调用这样做的功能模块/方法),您最终将删除错误的行.

请注意,您可以在示例中简单地使用语句 DELETE lt_itab.,因为要删除的行是当前行.

如果您的表可以有多个相同的行,则您的第二个变体 DELETE lt_itab FROM ls_wa. 将删除所有这些行,而不仅仅是当前的行 - 是否有意取决于您的要求.><小时>

重申定义的结果":删除当前行.没有继续下一行" - 添加 INTO var 你实际上复制整行到你的变量中.该变量不会被触及,它只是与表不同步.这可能是故意的 - 系统无法知道这一点.如果您改用字段符号,它将是 UNASSIGNED,这 - 再次 - 可能是您想要的 - 然后又可能不是.

Can I safely delete the active row while looping over an internal table?

As an example, consider this code:

LOOP AT lt_itab INTO ls_wa.
    IF [...] . " A check that can't be done inside a 'DELETE lt_itab WHERE'
        DELETE lt_itab INDEX sy-tabix
        " OR
        DELETE lt_itab FROM ls_wa.
    ENDIF.
ENDLOOP.

Is it safe to delete records like this or will this logic not behave as intended?

Should I instead store the unique identifier for the rows in a temporary itab and run a DELETE lt_itab WHERE after the loop?

I assume that delete operations on records other than the one that is loaded in the current iteration will definitely cause issues but I'm unsure if this is a valid, let alone good practice.

解决方案

Whether it is safe or not depends largely on your coding skills. It has a defined result, and it's up to you to use the commands correctly. It is usually safe if nothing else happens after the DELETE statement within the loop. You can issue a CONTINUE statement right after the deletion to make sure that this is the case.

Do not use DELETE lt_itab INDEX sy-tabix. If you use some statement within your check that changes sy-tabix as a side effect (for example, looking up some entry in a check table - or calling a function module/method that does so), you will end up deleting the wrong lines.

Be aware that you can simply use the statement DELETE lt_itab. in your example since the line to delete is the current one.

If your table can have multiple identical lines, your second variant DELETE lt_itab FROM ls_wa. will delete all of them, not just the current one - whether that is intended depends on your requirements.


EDIT: To reiterate the "defined result": The current line is deleted. There is no "continuing with the next line" - with the addition INTO var you actually copied the entire line into your variable. That variable won't be touched, it's just out of sync with the table. This might be intentional - the system has no way of knowing this. If you use a field symbol instead, it will be UNASSIGNED, which - again - might be what you intended - and then again maybe not.

这篇关于从循环中的内部表中删除当前行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆