我想知道是否有任何解决方案。
TYPES: BEGIN OF ty_table1,
field1 TYPE string,
END OF ty_table1,
tt_table1 type STANDARD TABLE OF ty_table1 with EMPTY KEY,
BEGIN OF ty_table2,
field1 TYPE string,
END OF ty_table2,
tt_table2 type STANDARD TABLE OF ty_table2 with EMPTY KEY.
DATA: lt_table TYPE table OF ty_table1,
lt_final TYPE table OF ty_table2.
DATA(lt_table1) = VALUE tt_table1( FOR <fs_struct> IN lt_table WHERE ( field1 EQ 'TEST' )
( CORRESPONDING #( <fs_struct> ) ) ).
DATA(lt_table2) = VALUE tt_table2( FOR <fs_struct> IN lt_final WHERE ( field1 EQ 'TEST1' )
( CORRESPONDING #( <fs_struct> ) ) ).
Run Code Online (Sandbox Code Playgroud)
在这里,我们将在第二个构造函数表达式中得到一个语法错误,说明:
“<FS_STRUCT>”已经用“TY_TABLE1”类型声明,不能在此处与“TY_TABLE2”类型一起使用。
<FS_STRUCT>在第一条语句之后有什么方法可以“取消分配”吗?因此,它可以用于下一个即将用于不同表的 FOR 语句。
普通UNASSIGN语句不能用于取消分配,<fs_struct>因为它在 FOR 循环之外没有范围。
LET 表达式中指定的辅助字段在指定 LET 表达式的上下文中有效。这可以是完整的表达式或只是表达式的一部分。完整表达式的所有辅助字段都在同一个命名空间中。不能在同一表达式的进一步 LET 表达式中指定先前指定的辅助字段。此外,辅助字段与当前过程或程序的数据对象或字段符号位于同一名称空间中。如果表达式的过程或程序中已存在同名的数据对象或字段符号,则不能在 LET 表达式中定义辅助字段。相反,不能在带有 LET 表达式的表达式之后声明具有分配给辅助字段的名称的数据对象或字段符号。在不同表达式中重用辅助字段时,以下适用:
- If a helper field is defined for the first time in the current procedure or program, it is declared inline.
- If a helper field in the current procedure or program is defined again in a LET expression of a different expression and the derived data type matches, the helper field is bound to this expression and can be used there.
- If a helper field in the current procedure or program is defined again in a LET expression of a different expression and the derived data type does not match, the helper field cannot be used there and a syntax error occurs.
因此,实际上您的代码中有两个不同的声明(理论上可以包含不同的类型)。然而,ABAP 语言强制类型匹配,可能是为了避免具有相同名称的变量突然包含不同类型的混淆情况(这也可能需要在运行时防止冲突)。
也无法影响为用LET²声明的局部变量推断的类型。
从中我会说答案显然是否定的,并且您确实应该为不同的类型使用不同的名称。同样CORRESPONDING在通用字段符号上使用对我来说听起来很危险,我会避免这种情况。
²除非您更改派生类型的值,例如。通过CONV将一张桌子插入另一张桌子(尽管这可能不适用于您的情况):
DATA(second_result) = VALUE second_table_type(
FOR <entry> IN CONV first_table_type( second_source )
WHERE ( field1 EQ 'TEST1' )
( CORRESPONDING #( <entry> ) )
).
Run Code Online (Sandbox Code Playgroud)