问题
I'm trying to create a method that will import a structure assuming that all structure elements are flat and the same data type. Then I want to convert it to a table with that type dynamically and return it. Where should I start? I've been trying to get this to work for several hours and am getting nowhere.
回答1:
You can use the create_dynamic_table
form using cl_alv_table_create
like so:
* Create dynamic internal table and assign to FS
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = ifc
i_length_in_byte = 'X' "added by Paul Robert Oct 28, 2009 17:04
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table>.
* Create dynamic work area and assign to FS
CREATE DATA dy_line LIKE LINE OF <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
Here is a link that will help you get started.
回答2:
There is absolutely no need to use CL_ALV_TABLE_CREATE
- pure overkill. Use the Run Time Type Services (RTTS) to get information about your input data (and validate your assumption about the component types), then use the dynamic form of CREATE TABLE:
* asssuming that l_type_name contains the type name of a single element
CREATE DATA lr_table TYPE STANDARD TABLE OF (l_type_name).
来源:https://stackoverflow.com/questions/15235913/how-to-convert-a-structure-to-an-internal-table-dynamically-in-abap