Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to define data types dynamically

Private_Member_17805
Participant
0 Likes
2,458

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

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,408

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

4 REPLIES 4
Read only

Sm1tje
Active Contributor
0 Likes
1,408

what about class CL_ALV_TABLE_CREATE?

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,409

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

Read only

dev_parbutteea
Active Contributor
0 Likes
1,408

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.

Read only

Former Member
0 Likes
1,408

hi,

chk out the following link

http://www.sapdev.co.uk/sap-help/dynamic-structure.htm

regards

Jayapriya