将动态 REF TO DATA 结构转换为静态
Convert dynamic REF TO DATA structure to static
我创建了包含三个组件的结构,其中一个是数据类型引用和此结构的 table 类型。问题是,如何向 table 添加数据?
它总是有三个组成部分,但在处理过程中只发现了其中一个,我总是知道其中两个。因此,我总是使用整个 table 类型 ref to data
,然后确定此结构的类型并在其上创建 table。
这里的问题是,通过这样做,即使我知道其中两个组件,整个 itab 将是动态的,所以我必须在方法中使用它 exporting/importing 数据的类型引用,它是不方便。
下面的方法总是return一个table类型的数据引用,它是完全动态的(类型引用数据),但是table的结构总是像这样:
- 组件 1 -> 输入 pc261。
- 组件 2 -> 类型 pay99_international.
- 成分 3 -> 好吧,这总是一个谜嘿嘿
methods get_payroll
importing it_rgdir type hrpy_tt_rgdir
returning value(rt_value) type ref to data.
method get_payroll.
field-symbols: <lt_payroll> type standard table.
create data rt_value type standard table of (mv_py_struct_type).
assign rt_value->* to <lt_payroll>.
...
endmethod.
我的意图是让 returning 值具有另一种类型,一种已知类型,通过它我可以更轻松地使用这两个已知组件。我的想法是创建一个只有未知字段作为数据引用的类型,而不是 table。
这样,我就可以在方法中使用它而无需工作,所以 "dynamicaly" 虽然工作得很好,但只有通过阅读代码才能理解它。
types begin of gty_s_generic_payroll.
types evp type pc261.
types inter type pay99_international.
types nat type ref to data.
types end of gty_s_generic_payroll.
types gty_t_generic_payroll type table of gty_s_generic_payroll.
问题是,如何使用上面声明的 gty_t_generic_payroll
类型的 itab?
我必须以某种方式创建组件 3,但我不知道该怎么做...
最后,我有一个通用字段符号,即类型 table,它具有两个已知组件 + 在处理期间发现的第三个组件。
那么如何将此字段符号的内容传递给 table 类型 gty_t_generic_payroll
?
data lt_payroll type ref to data.
field-symbols <lt_payroll> type any table.
lt_payroll = mo_payroll->get_payroll( lt_rgdir ). "this will return type ref to data
assign lt_payroll->* to <lt_payroll>.
执行此代码后 <lt_payroll>
具有所有值,但它是动态的 table,我无法在其中使用组件 <lt_payroll>[1]-inter
。
那么如何传递给 gty_t_generic_payroll
类型的变量,以便我可以在没有太多动态的情况下访问组件?
鉴于您的目标结构和 table 像这样:
TYPES:
BEGIN OF payroll_row_type,
known_first_component TYPE something_we_know,
known_second_component TYPE something_else_we_know,
discovered_component TYPE REF TO data,
END OF payroll_row_type.
TYPES payroll_table_type TYPE STANDARD TABLE OF payroll_row WITH EMPTY KEY.
如果您现在有另一个 table,其运行时类型为:
TYPES:
BEGIN OF discovered_row_type,
known_first_component TYPE something_we_know,
known_second_component TYPE something_else_we_know,
known_third_component TYPE some_data_type,
END OF discovered_row_type.
TYPES discovered_table_type TYPE STANDARD TABLE OF discovered_row WITH EMPTY KEY.
您可以使用
将一个移动到另一个
DATA source TYPE discovered_table_type.
DATA target TYPE payroll_table_type.
DATA resolved_component TYPE REF TO DATA.
DATA(descriptor) =
cl_abap_elemdescr=>describe_by_data( source_row-known_third_component ).
LOOP AT source INTO DATA(source_row).
DATA(target_row) =
VALUE payroll_row_type(
known_first_component = source_row-known_first_component
known_second_component = source_row-known_second_component ).
CREATE DATA target_row-discovered_component TYPE descriptor.
ASSIGN source_row-known_third_component TO FIELD-SYMBOL(<source_component>).
ASSIGN target_row-discovered_component TO FIELD-SYMBOL(<target_component>).
<target_component> = <source_component>.
INSERT target_row INTO TABLE target.
ENDLOOP.
看起来我能够将值从完全通用的 table(类型引用数据)传递到另一个 table,即 1/3 通用的(类型 gty_t_generic_payroll ):
methods get_payroll
importing it_rgdir type hrpy_tt_rgdir
returning value(rt_value) type gty_t_generic_payroll.
method get_payroll.
data lt_payroll type gty_t_generic_payroll.
data lt_payroll_aux type ref to data.
field-symbols: <lt_payroll_aux> type standard table.
create data lt_payroll_aux type standard table of (mv_py_struct_type).
assign lt_payroll_aux->* to <lt_payroll_aux> .
call function ' '.
call function ' '
exporting
= mv_relid
= mv_pernr
= xsdbool( gs_parm-use_natio <> abap_true )
tables
= it_rgdir
= <lt_payroll_aux> "table with the values I need
exceptions
= 0.
if sy-subrc <> 0.
return.
endif.
loop at <lt_payroll_aux> assigning field-symbol(<ls_payroll_aux>).
assign component 1 of structure <ls_payroll_aux> to field-symbol(<evp>).
assign component 2 of structure <ls_payroll_aux> to field-symbol(<inter>).
assign component 3 of structure <ls_payroll_aux> to field-symbol(<nat>).
data(ls_value) = value gty_s_generic_payroll(
evp = <evp>
inter = <inter>
).
get reference of <nat> into ls_value-nat.
append ls_value to rt_value. "returning table, with values I need and
"now with 2/3 known types
endloop.
endmethod.
一天结束时,我完成了我需要的,但不幸的是我确实失去了很多性能,因为我现在必须在结果中循环两次。
- 填充不那么动态的-table
- 做报告的实际过程(至少它变得更漂亮哈哈)
这是唯一的方法,因为我不能简单地使用insert lines of dynamic_itab to not_so_dynamic_itab
,因为第三个组件是参考。
问题和答案可能会让未来的访问者感到困惑(真正的问题是什么?),所以这是我的两分钱。
问题总结:
你调用一个外部代码 (1),它给你一个动态生成的内部 table,但你知道所有的组件总是相同的,除了一个变化但在相同的位置,因此您想静态地引用其组件,但变化的组件除外。
(1) 所以,你不能适配它。
您的解决方法是静态定义等效的内部 table 并且变化的组件将被定义为数据引用类型(指向任何数据对象的指针),然后通过从动态内部复制数据 table.
您要求另一个更好的解决方案,因为您的解决方案会消耗额外的内存(两个内部 tables)并降低性能(复制过程)。
答案:
- 没有更好的解决方案
我创建了包含三个组件的结构,其中一个是数据类型引用和此结构的 table 类型。问题是,如何向 table 添加数据?
它总是有三个组成部分,但在处理过程中只发现了其中一个,我总是知道其中两个。因此,我总是使用整个 table 类型 ref to data
,然后确定此结构的类型并在其上创建 table。
这里的问题是,通过这样做,即使我知道其中两个组件,整个 itab 将是动态的,所以我必须在方法中使用它 exporting/importing 数据的类型引用,它是不方便。
下面的方法总是return一个table类型的数据引用,它是完全动态的(类型引用数据),但是table的结构总是像这样:
- 组件 1 -> 输入 pc261。
- 组件 2 -> 类型 pay99_international.
- 成分 3 -> 好吧,这总是一个谜嘿嘿
methods get_payroll
importing it_rgdir type hrpy_tt_rgdir
returning value(rt_value) type ref to data.
method get_payroll.
field-symbols: <lt_payroll> type standard table.
create data rt_value type standard table of (mv_py_struct_type).
assign rt_value->* to <lt_payroll>.
...
endmethod.
我的意图是让 returning 值具有另一种类型,一种已知类型,通过它我可以更轻松地使用这两个已知组件。我的想法是创建一个只有未知字段作为数据引用的类型,而不是 table。
这样,我就可以在方法中使用它而无需工作,所以 "dynamicaly" 虽然工作得很好,但只有通过阅读代码才能理解它。
types begin of gty_s_generic_payroll.
types evp type pc261.
types inter type pay99_international.
types nat type ref to data.
types end of gty_s_generic_payroll.
types gty_t_generic_payroll type table of gty_s_generic_payroll.
问题是,如何使用上面声明的 gty_t_generic_payroll
类型的 itab?
我必须以某种方式创建组件 3,但我不知道该怎么做...
最后,我有一个通用字段符号,即类型 table,它具有两个已知组件 + 在处理期间发现的第三个组件。
那么如何将此字段符号的内容传递给 table 类型 gty_t_generic_payroll
?
data lt_payroll type ref to data.
field-symbols <lt_payroll> type any table.
lt_payroll = mo_payroll->get_payroll( lt_rgdir ). "this will return type ref to data
assign lt_payroll->* to <lt_payroll>.
执行此代码后 <lt_payroll>
具有所有值,但它是动态的 table,我无法在其中使用组件 <lt_payroll>[1]-inter
。
那么如何传递给 gty_t_generic_payroll
类型的变量,以便我可以在没有太多动态的情况下访问组件?
鉴于您的目标结构和 table 像这样:
TYPES:
BEGIN OF payroll_row_type,
known_first_component TYPE something_we_know,
known_second_component TYPE something_else_we_know,
discovered_component TYPE REF TO data,
END OF payroll_row_type.
TYPES payroll_table_type TYPE STANDARD TABLE OF payroll_row WITH EMPTY KEY.
如果您现在有另一个 table,其运行时类型为:
TYPES:
BEGIN OF discovered_row_type,
known_first_component TYPE something_we_know,
known_second_component TYPE something_else_we_know,
known_third_component TYPE some_data_type,
END OF discovered_row_type.
TYPES discovered_table_type TYPE STANDARD TABLE OF discovered_row WITH EMPTY KEY.
您可以使用
将一个移动到另一个DATA source TYPE discovered_table_type.
DATA target TYPE payroll_table_type.
DATA resolved_component TYPE REF TO DATA.
DATA(descriptor) =
cl_abap_elemdescr=>describe_by_data( source_row-known_third_component ).
LOOP AT source INTO DATA(source_row).
DATA(target_row) =
VALUE payroll_row_type(
known_first_component = source_row-known_first_component
known_second_component = source_row-known_second_component ).
CREATE DATA target_row-discovered_component TYPE descriptor.
ASSIGN source_row-known_third_component TO FIELD-SYMBOL(<source_component>).
ASSIGN target_row-discovered_component TO FIELD-SYMBOL(<target_component>).
<target_component> = <source_component>.
INSERT target_row INTO TABLE target.
ENDLOOP.
看起来我能够将值从完全通用的 table(类型引用数据)传递到另一个 table,即 1/3 通用的(类型 gty_t_generic_payroll ):
methods get_payroll
importing it_rgdir type hrpy_tt_rgdir
returning value(rt_value) type gty_t_generic_payroll.
method get_payroll.
data lt_payroll type gty_t_generic_payroll.
data lt_payroll_aux type ref to data.
field-symbols: <lt_payroll_aux> type standard table.
create data lt_payroll_aux type standard table of (mv_py_struct_type).
assign lt_payroll_aux->* to <lt_payroll_aux> .
call function ' '.
call function ' '
exporting
= mv_relid
= mv_pernr
= xsdbool( gs_parm-use_natio <> abap_true )
tables
= it_rgdir
= <lt_payroll_aux> "table with the values I need
exceptions
= 0.
if sy-subrc <> 0.
return.
endif.
loop at <lt_payroll_aux> assigning field-symbol(<ls_payroll_aux>).
assign component 1 of structure <ls_payroll_aux> to field-symbol(<evp>).
assign component 2 of structure <ls_payroll_aux> to field-symbol(<inter>).
assign component 3 of structure <ls_payroll_aux> to field-symbol(<nat>).
data(ls_value) = value gty_s_generic_payroll(
evp = <evp>
inter = <inter>
).
get reference of <nat> into ls_value-nat.
append ls_value to rt_value. "returning table, with values I need and
"now with 2/3 known types
endloop.
endmethod.
一天结束时,我完成了我需要的,但不幸的是我确实失去了很多性能,因为我现在必须在结果中循环两次。
- 填充不那么动态的-table
- 做报告的实际过程(至少它变得更漂亮哈哈)
这是唯一的方法,因为我不能简单地使用insert lines of dynamic_itab to not_so_dynamic_itab
,因为第三个组件是参考。
问题和答案可能会让未来的访问者感到困惑(真正的问题是什么?),所以这是我的两分钱。
问题总结:
你调用一个外部代码 (1),它给你一个动态生成的内部 table,但你知道所有的组件总是相同的,除了一个变化但在相同的位置,因此您想静态地引用其组件,但变化的组件除外。
(1) 所以,你不能适配它。
您的解决方法是静态定义等效的内部 table 并且变化的组件将被定义为数据引用类型(指向任何数据对象的指针),然后通过从动态内部复制数据 table.
您要求另一个更好的解决方案,因为您的解决方案会消耗额外的内存(两个内部 tables)并降低性能(复制过程)。
答案:
- 没有更好的解决方案