Delete the current row from an internal table in a loop
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.