What is the shortest notation for updating a column in an internal table?
Asked Answered
H

3

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?

Haddington answered 15/3, 2018 at 16:11 Comment(2)
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...Wakeup
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.Haddington
F
6

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

Flanna answered 16/3, 2018 at 21:15 Comment(1)
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.Haddington
M
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.

Mammet answered 15/3, 2018 at 16:19 Comment(6)
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.Flanna
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 ;)Mammet
Without WHERE, your code works only inside a loop, one line at a time.Flanna
@VXLozano, you keep posting extremely low-quality answers and then rely on others to fix the stuff you left behind.Wakeup
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.Mammet
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.Haddington
F
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.
Fornax answered 27/6, 2023 at 11:38 Comment(1)
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.Tsang

© 2022 - 2024 — McMap. All rights reserved.