简体   繁体   English

从循环中的内部表中删除当前行

[英]Delete the current row from an internal table in a loop

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?我应该将行的唯一标识符存储在临时 itab 中并在循环后运行DELETE lt_itab WHERE吗?

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.如果在循环内的DELETE语句之后没有发生任何其他事情,通常是安全的。 You can issue a CONTINUE statement right after the deletion to make sure that this is the case.您可以在删除后立即发出CONTINUE语句以确保确实如此。

Do not use DELETE lt_itab INDEX sy-tabix .不要使用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.如果您在检查中使用某些更改sy-tabix作为副作用的语句(例如,在检查表中查找某些条目 - 或调用这样做的功能模块/方法),您最终将删除错误的行.

Be aware that you can simply use the statement DELETE lt_itab.请注意,您可以简单地使用语句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.如果您的表可以有多个相同的行,则您的第二个变体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.没有“继续下一行” - 通过添加INTO var您实际上整行复制到您的变量中。 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.如果您改用字段符号,它将是UNASSIGNED ,这 - 再次 - 可能是您想要的 - 然后又可能不是。

Try this:尝试这个:

GET CURSOR LINE SY-CUROW .
DELETE ST_MAT INDEX SY-CUROW.

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

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