Three ways to create dynamic inner tables in BAP
The first type:
If we need the same type of dynamic inner table fields or dynamic workspaces and data dictionaries, you can use create data directly and, of course, a custom type.
For example, to produce a dynamic inner table that is consistent with the MARA structure of the data table:
Data:dy_table type ref to data, Wa_line type ref to data.
Field-symbols: <DYN_TABLE> TYPE standard TABLE.
CREATE DATA dy_table TYPE TABLE of MARA.
ASSIGN dy_table->* to <dyn_table>.
CREATE DATA wa_line like Line of <dyn_table>.
ASSIGN wa_line->* to <dyn_wa>.
If you need to dynamically generate several different dynamic inner tables in your program, you can set the table name to a variable
CREATE DATA dy_table TYPE TABLE of (tabname).
The second type:
If you need to output the dynamic inner table, control the output order and other properties. You can use the following methods:
Call FUNCTION ' Lvc_fieldcatalog_merge '
Exporting
I_client_never_display = ' '
I_structure_name = TabName
changing
Ct_fieldcat = It_structure
EXCEPTIONS
Inconsistent_interface = 1
Program_error = 2
OTHERS = 3.
Use this method to create an input tabname corresponding to the field directory
The corresponding dynamic inner table structure is then generated according to the field directory
Call METHOD cl_alv_table_create=>create_dynamic_table
Exporting
It_fieldcatalog = It_structure
* I_length_in_byte = ' X '
Importing
ep_table = dy_table.
ASSIGN dy_table->* to <dyn_table>.
Dynamic workspaces are generated the same way as the first.
This method has a disadvantage and can only be used 36 times in a row. That is, the method of generating dynamic is to use the method of creating subroutines, when the continuous use of 36 times, there will be a child pool overflow exception, that the method is targeted at the processing of ALV technology, so used in other places need to be cautious.
The third type:
The third kind of basic idea is the same as the second one, except that the classes and methods used are different, resulting in different data input.
First, create the structure
Call FUNCTION ' Lvc_fieldcatalog_merge '
Exporting
I_client_never_display = ' '
I_structure_name = TabName
changing
Ct_fieldcat = It_structure
EXCEPTIONS
Inconsistent_interface = 1
Program_error = 2
OTHERS = 3.
Generate the corresponding interface parameter based on the field catalog generated by the field zcomponents
DATA zcomponents TYPE Abap_component_tab. Declare the TYPE-POOLS:ABAP before declaring the variable.
Abap_component_tab The fields in this structure are as follows:
Name TYPE string,
Type type REF to CL_ABAP_DATADESCR,
As_include TYPE Abap_bool,
Suffix TYPE string,
So the loop is generated before the table in the field directory,
The name field is a struct name or a table name that is TabName
Type is an object that can be obtained using the Cl_abap_datadescr=>describe_by_name method, where p_name This parameter is the table name or struct name + field name type used? = Symbol to get an instance of P_descr_ref
The latter two can not be filled
Generate structure
Call METHOD Cl_abap_structdescr=>create
Exporting
P_components = zcomponents
Receiving
P_result = Zresult.
Generate table
Call METHOD Cl_abap_tabledescr=>create
Exporting
P_line_type = Zresult
Receiving
P_result = Wresult.
CREATE DATA wa_line TYPE HANDLE zresult.
CREATE DATA dyn_table TYPE HANDLE wresult.
This method can avoid the disadvantage of Method 2.
Three ways to create dynamic inner tables in BAP (reproduced)