‎2009 Aug 03 10:05 PM
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. 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>.
‎2009 Aug 03 10:13 PM
Should be
wa_comp-name = wa_components_tmp-name.
wa_comp-type ?= cl_abap_elemdescr=>describe_by_data( wa_components_tmp-name ). "class cl_abap_elemdescr
APPEND wa_comp TO new_comp_tab.
Regards
Marcin
‎2009 Aug 03 10:13 PM
Should be
wa_comp-name = wa_components_tmp-name.
wa_comp-type ?= cl_abap_elemdescr=>describe_by_data( wa_components_tmp-name ). "class cl_abap_elemdescr
APPEND wa_comp TO new_comp_tab.
Regards
Marcin
‎2009 Aug 03 10:19 PM
Thanks Marcin,
I tried that option, but it still creates the new internal table with all fields as C(30) which i dont want.
‎2009 Aug 03 11:20 PM
Indeed strange issue. It returns C(30) type each time for each field. But I found some other solution. It works fine with method describe_by_name of cl_abap_elemdescr . Unfortunatelly it expects Data element of field you want type of. So you can do:
- use fm CY_GET_FIELD to get DFIES structure for each component name
- call method describe by name passing ROLLNAME field of DFIES struc. as name of component (data element) for typing
Then it will create your table with type refering to DDIC.
Though not state-of-the-art approach but will work. Still there has to be some way using RTTS but can't find any right now.
Regards
Marcin
‎2009 Aug 03 11:22 PM
Uwe answer solved my Issue:
Hello
You may have a look at my Wiki sample:
[Creating Flat and Complex Internal Tables Dynamically using RTTI |https://wiki.sdn.sap.com/wiki/display/Snippets/Creating%20Flat%20and%20Complex%20Internal%20Tables%20Dynamically%20using%20RTTI]
Regards
Uwe
Thanks