‎2009 Jan 20 3:25 PM
Need to define a data type at the runtime without referencing it to any structure or ddid objects whatsoever.
i.e. say I select a list of fields based on some condition and create an internal able whose structure is same as the list of fields that
I have selected.
I did go through documentation on field symbols but I could not find anything on field symbols that achieved this without referencing a predefined structure.
Say I get the following fields from some Z table based on a specific query,I need to construct a internal table based on this structure.
mandt
carrid
connid
‎2009 Jan 20 3:34 PM
Well, I think you will need to have the table name as well as the field name when getting the DDIC information. Just having the field name is not enough here. So if you have an internal table with the field names, you may want to add the table name as well or add another field to your internal table, which contains the table name. In the below example, I am adding fields dynamically to the GT_COMPONENTS tab, you will want to do this, instead you will loop at your internal table, and pass the names of the fields and the table/field name to the DESCRIBE_BY_NAME method.
TYPE-POOLS:
abap.
DATA:
gr_structdescr TYPE REF TO cl_abap_structdescr,
gr_tabledescr TYPE REF TO cl_abap_tabledescr,
gr_datadescr TYPE REF TO cl_abap_datadescr,
gt_components TYPE abap_component_tab,
gw_component TYPE LINE OF abap_component_tab,
gr_wa TYPE REF TO data,
gr_tab TYPE REF TO data.
FIELD-SYMBOLS: <fs_wa> TYPE ANY.
FIELD-SYMBOLS: <fs_tab> TYPE table.
START-OF-SELECTION.
* determine components of structure -> GT_COMPONENTS
MOVE 'MANDT' TO gw_component-name.
gw_component-type ?= cl_abap_elemdescr=>DESCRIBE_BY_NAME( 'SPFLI-MANDT' ).
INSERT gw_component INTO TABLE gt_components.
MOVE 'CARRID' TO gw_component-name.
gw_component-type ?= cl_abap_elemdescr=>DESCRIBE_BY_NAME( 'SPFLI-CARRID' ).
INSERT gw_component INTO TABLE gt_components.
MOVE 'CONNID' TO gw_component-name.
gw_component-type ?= cl_abap_elemdescr=>DESCRIBE_BY_NAME( 'SPFLI-CONNID' ).
INSERT gw_component INTO TABLE gt_components.
* get structure descriptor -> GR_STRUCTDESCR
gr_structdescr ?= cl_abap_structdescr=>create( gt_components ).
* create work area of structure GR_STRUCTDESCR -> GR_WA
CREATE DATA gr_wa TYPE HANDLE gr_structdescr.
ASSIGN gr_wa->* TO <fs_wa>.
gr_datadescr ?= gr_structdescr.
gr_tabledescr ?= cl_abap_tabledescr=>create( gr_datadescr ).
* Create dynmaic internal table
CREATE DATA gr_tab TYPE HANDLE gr_tabledescr.
ASSIGN gr_tab->* TO <fs_tab>.REgards,
Rich Heilman
‎2009 Jan 20 3:30 PM
‎2009 Jan 20 3:34 PM
Well, I think you will need to have the table name as well as the field name when getting the DDIC information. Just having the field name is not enough here. So if you have an internal table with the field names, you may want to add the table name as well or add another field to your internal table, which contains the table name. In the below example, I am adding fields dynamically to the GT_COMPONENTS tab, you will want to do this, instead you will loop at your internal table, and pass the names of the fields and the table/field name to the DESCRIBE_BY_NAME method.
TYPE-POOLS:
abap.
DATA:
gr_structdescr TYPE REF TO cl_abap_structdescr,
gr_tabledescr TYPE REF TO cl_abap_tabledescr,
gr_datadescr TYPE REF TO cl_abap_datadescr,
gt_components TYPE abap_component_tab,
gw_component TYPE LINE OF abap_component_tab,
gr_wa TYPE REF TO data,
gr_tab TYPE REF TO data.
FIELD-SYMBOLS: <fs_wa> TYPE ANY.
FIELD-SYMBOLS: <fs_tab> TYPE table.
START-OF-SELECTION.
* determine components of structure -> GT_COMPONENTS
MOVE 'MANDT' TO gw_component-name.
gw_component-type ?= cl_abap_elemdescr=>DESCRIBE_BY_NAME( 'SPFLI-MANDT' ).
INSERT gw_component INTO TABLE gt_components.
MOVE 'CARRID' TO gw_component-name.
gw_component-type ?= cl_abap_elemdescr=>DESCRIBE_BY_NAME( 'SPFLI-CARRID' ).
INSERT gw_component INTO TABLE gt_components.
MOVE 'CONNID' TO gw_component-name.
gw_component-type ?= cl_abap_elemdescr=>DESCRIBE_BY_NAME( 'SPFLI-CONNID' ).
INSERT gw_component INTO TABLE gt_components.
* get structure descriptor -> GR_STRUCTDESCR
gr_structdescr ?= cl_abap_structdescr=>create( gt_components ).
* create work area of structure GR_STRUCTDESCR -> GR_WA
CREATE DATA gr_wa TYPE HANDLE gr_structdescr.
ASSIGN gr_wa->* TO <fs_wa>.
gr_datadescr ?= gr_structdescr.
gr_tabledescr ?= cl_abap_tabledescr=>create( gr_datadescr ).
* Create dynmaic internal table
CREATE DATA gr_tab TYPE HANDLE gr_tabledescr.
ASSIGN gr_tab->* TO <fs_tab>.REgards,
Rich Heilman
‎2009 Jan 20 3:36 PM
Hi,
data:i_data type ref to data.
field-symbols: <fs_data> type ref to data.
assign i_data to <fs_data>.
*CREATING INTERNAL TABLE TO DISPLAY ALV DATA
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = i_fieldlist
importing
ep_table = <fs_data>
exceptions
generate_subpool_dir_full = 1
others = 2.
‎2009 Jan 20 3:37 PM