Supported by
Supported by Inetum

Insert without raising exception in internal table with unique key

images/thumbnail.jpg - Thumbnail

How many times in your ABAP consultant life did you have to deal with dumps happening as a consequence of a program trying to insert duplicate lines into an internal table defined with a UNIQUE KEY?

Enough.

Since the 20th century that we’ve gotten used to always using the APPEND command to insert in an internal table. But it’s a been a while since SAP is telling us to stop using it and use INSERT INTO TABLE instead.

APPEND always adds a line at the end of the table. This is fine for STANDARD TABLEs, but if you try it on a SORTED TABLE you’ll probably run into problems at some point. But INSERT INTO TABLE works with every kind of table.

INSERT INTO TABLE has another advantage: it doesn’t raise exceptions. Instead, it uses SY-SUBRC to signal errors. This error must be handled, of course. But I don’t really see a reason to use APPEND anymore.

This said, I have nothing against exceptions and my now favourite way to insert entries in an internal table is the new VALUE #() which raises exceptions when there are duplicate keys in tables with UNIQUE KEYs. It would be even more useful if they’d add a DISTINCT option to it.

Greetings from Abapinho.

Photo credit: Jo Zimny