Skip to Content
0
Former Member
Aug 03, 2009 at 08:47 PM

Dynamic Internal Table Creation using selected field of a database table

89 Views

I am trying to create a dynamic internal table which will have some fields from any one database table. For example I need to create a dynamic internal table it_ekko which will have only fields mandt eblen and aedat from database table ekko. I am able to achieve this using field catalogs and cl_alv_table_create=>create_dynamic_table but this class is obsolete and has a limitation of 36 tables in one loop.

I want to achieve this using cl_abap_tabledescr=>create. I am having issues determining the wa_comp-type for the new internal table. With the below code it is creating all fields in the new internal table as char 30 wheras i need it to be the same datatype and lenght as in EKKO. What should be assigned to wa_comp-type? Any suggestions or input is highly appreciated.

Here is my code.


DATA:

linetype type ref to cl_abap_structdescr,

tabletype type ref to cl_abap_tabledescr ,

comp_tab type cl_abap_structdescr=>component_table,

wa_comp like line of comp_tab,

new_comp_tab like comp_tab,

descr_table_ref_tmp TYPE REF TO cl_abap_tabledescr,

descr_struct_ref_tmp TYPE REF TO cl_abap_structdescr,

ls_components_tmp TYPE abap_compdescr_tab,

wa_components_tmp like line of ls_components_tmp,

table_ref TYPE REF TO DATA.

FIELD-SYMBOLS:

<fs_tb> TYPE ANY TABLE.

CREATE DATA table_ref TYPE STANDARD TABLE OF (p_tabnam_low).

ASSIGN table_ref->* to <fs_tb>.

descr_table_ref_tmp ?= cl_abap_typedescr=>describe_by_data( <fs_tb> ).

descr_struct_ref_tmp ?= descr_table_ref_tmp->get_table_line_type( ).

ls_components_tmp = descr_struct_ref_tmp->components.

LOOP AT it_sel_field INTO wa_sel_field.

READ TABLE ls_components_tmp INTO wa_components_tmp WITH KEY name = wa_sel_field.

IF sy-subrc EQ 0.

CLEAR: wa_fcat, wa_comp.

wa_fcat-fieldname = wa_components_tmp-name.

wa_fcat-ref_table = p_tabnam_low.

wa_fcat-ref_field = wa_components_tmp-name.

APPEND wa_fcat TO it_fieldcatalog.

wa_comp-name = wa_components_tmp-name.

wa_comp-type ?= cl_abap_typedescr=>describe_by_data( wa_components_tmp-name ). "??????? This doesnt work

APPEND wa_comp TO new_comp_tab.

ENDIF.

ENDLOOP.

linetype = cl_abap_structdescr=>create( new_comp_tab ).

tabletype = cl_abap_tabledescr=>create(

p_line_type = linetype

p_table_kind = cl_abap_tabledescr=>tablekind_std ).

create data dataref type handle tabletype.

ASSIGN dataref->* TO <row>.