* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_MDG_UTIL=>SM34_TABLE_TO_TOTAL
* +-------------------------------------------------------------------------------------------------+
* | [--->] TABLE TYPE STANDARD TABLE
* | [--->] TABLE_NAME TYPE TABNAM
* | [<---] TOTAL TYPE STANDARD TABLE
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD sm34_table_to_total.
DATA:lr_table TYPE REF TO data.
"根据表名动态创建内表
DATA(lo_struct) = CAST cl_abap_structdescr( cl_abap_tabledescr=>describe_by_name( table_name ) ).
DATA(lt_comp) = lo_struct->get_components( ).
"添加action && mark字段
DATA(lo_element) = CAST cl_abap_datadescr( cl_abap_elemdescr=>describe_by_name( 'CHAR01') ).
lt_comp = VALUE #( BASE lt_comp ( name = 'ACTION' type = lo_element ) ( name = 'MARK' type = lo_element ) ).
lo_struct = cl_abap_structdescr=>create( p_components = lt_comp ).
DATA(lo_table) = cl_abap_tabledescr=>create( p_line_type = lo_struct ).
CREATE DATA lr_table TYPE HANDLE lo_table.
FIELD-SYMBOLS:<ft_table> TYPE STANDARD TABLE.
ASSIGN lr_table->* TO <ft_table>.
LOOP AT table ASSIGNING FIELD-SYMBOL(<table_line>).
APPEND INITIAL LINE TO total ASSIGNING FIELD-SYMBOL(<total_line>).
" <table_line> = <total_line>.
DATA:total_line_string TYPE string.
cl_abap_container_utilities=>fill_container_c(
EXPORTING
im_value = <table_line>
IMPORTING
ex_container = total_line_string
EXCEPTIONS
illegal_parameter_type = 1
OTHERS = 2
) .
IF sy-subrc <> 0.
* Implement suitable error handling here
ELSE.
<total_line> = total_line_string.
ENDIF.
ENDLOOP.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_MDG_UTIL=>SM34_TOTAL_TO_TABLE
* +-------------------------------------------------------------------------------------------------+
* | [--->] TOTAL TYPE ANY TABLE
* | [--->] TABLE_NAME TYPE TABNAM
* | [<-()] TABLE_REF TYPE REF TO DATA
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD sm34_total_to_table.
DATA:lr_table TYPE REF TO data.
"获取数据库表对应的结构
DATA(lo_struct) = CAST cl_abap_structdescr( cl_abap_tabledescr=>describe_by_name( table_name ) ).
DATA(lt_comp) = lo_struct->get_components( ).
"添加action && mark字段
DATA(lo_element) = CAST cl_abap_datadescr( cl_abap_elemdescr=>describe_by_name( 'CHAR01') ).
lt_comp = VALUE #( BASE lt_comp ( name = 'ACTION' type = lo_element ) ( name = 'MARK' type = lo_element ) ).
lo_struct = cl_abap_structdescr=>create( p_components = lt_comp ).
DATA(lo_table) = cl_abap_tabledescr=>create( p_line_type = lo_struct ).
CREATE DATA lr_table TYPE HANDLE lo_table.
FIELD-SYMBOLS:<ft_table> TYPE STANDARD TABLE.
ASSIGN lr_table->* TO <ft_table>.
LOOP AT total ASSIGNING FIELD-SYMBOL(<total_line>).
APPEND INITIAL LINE TO <ft_table> ASSIGNING FIELD-SYMBOL(<table_line>).
" <table_line> = <total_line>.
DATA:total_line_string TYPE string.
total_line_string = <total_line>.
cl_abap_container_utilities=>read_container_c(
EXPORTING
im_container = total_line_string
IMPORTING
ex_value = <table_line>
EXCEPTIONS
illegal_parameter_type = 1
OTHERS = 2
) .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDLOOP.
GET REFERENCE OF <ft_table> INTO table_ref.
ENDMETHOD.
标签:cl,lo,ABAP,TYPE,abap,table,TOTAL,DATA,SM34
From: https://www.cnblogs.com/break-point/p/18135422