5

I have an ABAP internal table. Structured, with several columns (e.g. 25). Names and types are irrelevant. The table can get pretty large (e.g. 5,000 records).

| A   | B   | ... |
| --- | --- | --- |
| 7   | X   | ... |
| 2   | CCC | ... |
| 42  | DD  | ... |

Now I'd like to set one of the columns (e.g. B) to a specific constant value (e.g. 'Z').

What is the shortest, fastest, and most memory-efficient way to do this?

My best guess is a LOOP REFERENCE INTO. This is pretty efficient as it changes the table in-place, without wasting new memory. But it takes up three statements, which makes me wonder whether it's possible to get shorter:

LOOP AT lt_table REFERENCE INTO DATA(ls_row).
  ls_row->b = 'Z'.
ENDLOOP.

Then there is the VALUE operator which reduces this to one statement but is not very efficient because it creates new memory areas. It also gets longish for a large number of columns, because they have to be listed one by one:

lt_table = VALUE #( FOR ls_row in lt_table ( a = ls_row-a
                                             b = 'Z' ) ).

Are there better ways?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Florian
  • 4,821
  • 2
  • 19
  • 44
  • Have you compared (measured) `REFERENCE INTO` vs. `ASSIGNING`? Other than that, this is probably the fastest method. For the `VALUE` expression, you might be able to use `CORRESPONDING ... MAPPING ...` but since I don't have any performance measurements (and am not on a hunt for points), that's not enough for an answer... – vwegert Mar 18 '18 at 10:39
  • `REFERENCE INTO` and `ASSIGNING` turned out equally fast. Though the one required reference seems to use a tiny bit more memory - on a scale that's irrelevant to me. `CORRESPONDING` doesn't work because it only supports moving columns from one table to the next, but not setting them to fixed values. – Florian Mar 19 '18 at 09:35

3 Answers3

5

The following code sets PRICE = 0 of all lines at a time. Theoritically, it should be the fastest way to update all the lines of one column, because it's one statement. Note that it's impossible to omit the WHERE, so I use a simple trick to update all lines.

DATA flights TYPE TABLE OF sflight.
DATA flight TYPE sflight.

SELECT * FROM sflight INTO TABLE flights.
flight-price = 0.
MODIFY flights FROM flight TRANSPORTING price WHERE price <> flight-price.

Reference: MODIFY itab - itab_lines

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • Collected some second opinions from my team and we think this is indeed the shortest and most efficient way. Checking this as answer, as it also got the syntax correct. – Florian Mar 22 '18 at 14:16
0

If you have a workarea declared...

workarea-field = 'Z'.
modify table from workarea transporting field where anything.

Edition: After been able to check that syntax in my current system, I could prove (to myself) that the WHERE clause must be added or a DUMP is raised. Thanks Sandra Rossi.

VXLozano
  • 317
  • 1
  • 7
  • 3
    That won't work, the ABAP syntax requires to indicate WHERE. Moreover, the internal table cannot be named `table`, because it is confusing the ABAP parser with the other construct `modify table itab from workarea`. **Here is the valid syntax**: `modify itab from workarea transporting field where field <> workarea-field.` – Sandra Rossi Mar 16 '18 at 02:39
  • 1
    Glad to see your enhancement of my answer. In the systems I work with, that kind of syntax is allowed, I cannot tell you if it's deprecated, obsolete or just plain dump. But in any case, it's not a full working code snipet, as I don't provide them: I like to point people in the fish school direction and teach how to throw the net better than just handle them a fish ;) – VXLozano Mar 16 '18 at 08:01
  • Without WHERE, your code works only inside a loop, one line at a time. – Sandra Rossi Mar 16 '18 at 21:15
  • @VXLozano, you keep posting extremely low-quality answers and then rely on others to fix the stuff you left behind. – vwegert Mar 18 '18 at 10:37
  • I'm not here for the reputation nor for your approval, I'm just trying to learn and to give some in exchange. And this "some" is just my knowledge and hints, not exact answers with a "gimme points if useful". As I told you in my last comment, I do not provide working pieces of code but just the first steps in the direction I think it's the correct one. It's the community who will decide if those answers are appropiate or not by voting them or not. If someone before me gave a better answer I'll gladly vote for it, and let mine sink, because it's the way this site works. – VXLozano Mar 19 '18 at 12:45
  • The documentation suggests that the variant without `WHERE` works for tables with header lines. However, their use is discouraged, such that they are usually only encountered in legacy code. So agree that the `WHERE` should be included for a full answer. – Florian Mar 22 '18 at 14:19
0

Based on Sandra Rossis Answer if you don't have a workarea you can use:

DATA flights TYPE TABLE OF sflight.
SELECT * FROM sflight INTO TABLE flights.
MODIFY flights FROM VALUE #( price = 1 ) TRANSPORTING price WHERE price <> 0.
NKN30
  • 33
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 28 '23 at 15:50