简体   繁体   English

更新内部表中的列的最短表示法是什么?

[英]What is the shortest notation for updating a column in an internal table?

I have an ABAP internal table.我有一个 ABAP 内部表。 Structured, with several columns (eg 25).结构化,具有多个列(例如 25 个)。 Names and types are irrelevant.名称和类型无关紧要。 The table can get pretty large (eg 5,000 records).该表可能会变得非常大(例如 5,000 条记录)。

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

Now I'd like to set one of the columns (eg B) to a specific constant value (eg 'Z').现在我想将其中一列(例如 B)设置为特定的常量值(例如“Z”)。

What is the shortest, fastest, and most memory-efficient way to do this?执行此操作的最短、最快和最节省内存的方法是什么?

My best guess is a LOOP REFERENCE INTO .我最好的猜测是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.然后是VALUE运算符,它可以将其简化为一个语句,但效率不高,因为它会创建新的内存区域。 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?有更好的方法吗?

The following code sets PRICE = 0 of all lines at a time.以下代码一次设置所有行的 PRICE = 0。 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.请注意,不可能省略 WHERE,因此我使用一个简单的技巧来更新所有行。

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参考: 修改 itab - itab_lines

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.版本:能够在我当前的系统中检查该语法后,我可以证明(对自己)必须添加 WHERE 子句或引发 DUMP。 Thanks Sandra Rossi.谢谢桑德拉·罗西。

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

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