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

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

问题描述

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



例如,请考虑以下代码:

 在lt_itab INTO ls_wa中循环。 
IF [...]。 无法在'Dlt lt_itab WHERE'
DELETE lt_itab INDEX sy-tabix
里面进行检查,或者
从ls_wa删除lt_itab。
ENDIF。
ENDLOOP。

删除这样的记录是否安全,还是这种逻辑行为不正常?



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

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

解决方案

是否安全是很大程度上取决于您的编码技能。它具有定义的结果,并且取决于您是否正确使用命令。如果在循环内的 DELETE 语句之后没有发生任何其他事情,通常是安全的。您可以在删除后立即发出 CONTINUE 语句,以确保是这种情况。



不要使用 Delete lt_itab索引sy-tabix 如果您在支票中使用某些语句来更改 sy-tabix 作为副作用(例如,在检查表中查找某些条目-或调用执行此操作的功能模块/方法),最终将删除错误的行。



请注意,由于要删除的行是当前行,因此在示例中只需使用语句 DELETE lt_itab。



如果表可以有多行相同的行,则第二个变体从ls_wa中删除lt_itab。会删除所有这些行,而不仅仅是当前的-是否要取决于您的要求。






编辑:重申定义的结果:当前行被删除。没有继续下一行-加上 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天全站免登陆