没有 BY 的 SORT 在标准内部表上的行为?安全吗?

Behavior of a SORT without BY on standard internal tables? Is it safe?

当 运行 在标准内部 table 上时,没有键规范的 SORT 语句究竟做了什么?根据 documentation:

If no explicit sort key is entered using the addition BY, the internal table itab is sorted by the primary table key. The priority of the sort is based on the order in which the key fields are specified in the table definition. In standard keys, the sort is prioritized according to the order of the key fields in the row type of the table. If the primary table key of a standard table is empty, no sort takes place. If this is known statically, the syntax check produces a warning.

主 table 键 being defined 为:

Each internal table has a primary table key that is either a self-defined key or the standard key. For hashed tables, the primary key is a hash key, for sorted tables, the primary key is a sorted key. Both of these table types are key tables for which key access is optimized and the primary key thus has its own administration. The key fields of these tables are write-protected when you access individual rows. Standard tables also have a primary key, but the corresponding access is not optimized, there is no separate key administration, and the key fields are not write-protected.

为了更好的衡量标准,标准键 is defined 为:

Primary table key of an internal table, whose key fields in a structured row type are all table fields with character-like data types and byte-like data types. If the row type contains substructures, these are broken down into elementary components. The standard key for non-structured row types is the entire table row if the row type itself is not a table type. If there are no corresponding table fields, or the row type itself is a table type, the standard key from standard tables is empty or contains no key fields.

所有这些主要让我感到困惑,因为我不确定我是否真的可以依靠基本的 SORT 语句来提供可靠或安全的结果。我真的应该在所有情况下都避免使用它,还是 如果使用得当,它是否有用

推而广之,如果我想 运行 一个 DELETE ADJACENT DUPLICATES FROM itab COMPARING ALL FIELDS,在简单的 SORT itab. 之后什么时候这样做才安全?仅当我在所有字段上添加一个键时?只有当我有一个带有 clikexsequence 列的内部 table 时,才没有显式密钥? 如果我想执行那个 DELETE 语句,在内部 table 上 运行 的最佳 SORT 语句是什么?

在所有情况下都应避免不带 BY 的排序,因为它 "makes the program difficult to understand and possibly unpredictable" (dixit ABAP documentation)。我认为如果您不提及 BY,代码检查器中的静态检查会发出警告。您应该使用 SORT itab BY table_line,其中 table_line 是一个特殊名称 ("pseudo-component"),意思是 "all fields of the line".

不是你的问题,但你也可以用主键和辅助键定义内部 table,这样你就不需要显式排序 - DELETE ADJACENT DUPLICATES 可以与其中任何一个一起使用键。

内部 tables 可以具有可以从 itab 基于或指定的结构继承的键。正如文档所说,没有 bysort 按主键排序,假设内部 table 正确实现, 安全的。

认为此功能被设计为与智能table按键设计一起使用的动态功能。如果操作正确,没有 bysort 可以让您的程序适应 table 将来的关键变化。 (因此,如果您的密钥发生变化,请对其进行排序)。当以奇怪的方式修改密钥时,可能会出现问题。

根据经验:

你的程序代码越具体,就越不容易出错(也更安全)。 所以 sort by key_id, key_date 将始终按这 2 个字段生成相同的排序。

应用程序中的动态组件使其更加灵活,但当它们所依赖的东西被修改时往往会出现(通常很难注意到)错误。 因此,如果您采用前面带有 2 个关键字段的示例,您在中间添加 1(假设在 2 个现有字段之间 key_is_active),排序结果可能会以您没有预料到的方式发生变化。 如果您有一个基于日期处理的算法,您的算法可能会被该更改破坏。

在您使用 delete adjacent 的特定情况下,我会遵循 Sandra Rossi 的 建议。