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

Creating table at runtime w/o knowing the structure

Former Member
0 Likes
1,733

Hi,

I have searched high and far to find a solution for my needs, but have not come across anything. Hopefully, the knowledge here is greater than my searching skills

In an ABAP called during Dynamic Actions I have the need for storing data retrieved from the Call Stack via an ASSIGN command:

DATA: gv_ps_ref TYPE char50 VALUE '(SAPFP50P)PS[]'.

FIELD-SYMBOLS: <gt_ps_table> TYPE STANDARD TABLE.

ASSIGN (gv_ps_ref) TO <gt_ps_table>.

The ASSIGN part works beautifully - in debugging I now have access to the data in the PS table. But I need to store the data locally, as PS gets refreshed by this, which then also refreshes my <GT_PS_TABLE>:

PERFORM initialize_ps(sapfp50p).

The thing is, I need the data for later use, so I want to separate the link and create a local copy.

This code (most of it copied from other solutions here on SDN) works fine, but notice that the DESCRIBE_BY_DATA method is called with the GS_DATA structure. This means that the TYPE of the table that I want to create dynamically is known.

DATA: BEGIN OF gt_data OCCURS 5,

char TYPE text5,

numc TYPE numc5,

dec TYPE pad_amt7s,

END OF gt_data.

DATA: gs_data LIKE LINE OF gt_data.

DATA: go_struct TYPE REF TO cl_abap_structdescr,

go_new_type TYPE REF TO cl_abap_structdescr,

go_new_tab TYPE REF TO cl_abap_tabledescr,

gt_comp TYPE cl_abap_structdescr=>component_table,

go_data TYPE REF TO data.

FIELD-SYMBOLS: <fs_any> TYPE ANY TABLE.

go_struct ?= cl_abap_typedescr=>describe_by_data( gs_data ). "see alternate reference below, i.e. my need

gt_comp = go_struct->get_components( ).

go_new_type = cl_abap_structdescr=>create( gt_comp ).

go_new_tab = cl_abap_tabledescr=>create(

p_line_type = go_new_type

p_table_kind = cl_abap_tabledescr=>tablekind_std

p_unique = abap_false ).

CREATE DATA go_data TYPE HANDLE go_new_tab.

ASSIGN go_data->* TO <fs_any>.

<fs_any> = gt_data[].

As indicated, having only a reference to the PS table (<GT_PS_TABLE>) is not enough, I want to create a copy of PS locally, without knowing how it is declared in FP50PPSB (partly via a structure, partly via separate fields), as this will make my coding insensitive to any change in the structure.

This is my visualization of what I want to do, or the likes - I believe you get the idea. But is it possible?

go_struct ?= cl_abap_typedescr=>describe_by_data( <gt_ps_table> ).

The syntax checker allows for this, but it dumps at runtime

1 ACCEPTED SOLUTION
Read only

jrg_wulf
Active Contributor
0 Likes
1,410

Hi,

besides that your posting is allmost unreadable (try preview before posting), your basic idea is correct. You just overdid it a bit.

Just try something like this


  data: a_descr TYPE REF TO CL_ABAP_TYPEDESCR.
  FIELD-SYMBOLS: <val> type any.
     a_descr = cl_abap_datadescr=>describe_by_data( p_data =  st_val ).
    CREATE DATA me->d_ref type (a_descr->ABSOLUTE_NAME).
    assign me->d_ref->* to <val>.
    <val> = st_val.

this should work fine with all types of data, structures or tables.

Regards

Jörg

Hi,

I have searched high and far to find a solution for my needs, but have not come across anything. Hopefully, the knowledge here is greater than my searching skills

In an ABAP called during Dynamic Actions I have the need for storing data retrieved from the Call Stack via an ASSIGN command:

DATA: gv_ps_ref TYPE char50 VALUE '(SAPFP50P)PS[]'.

FIELD-SYMBOLS: <gt_ps_table> TYPE STANDARD TABLE.

ASSIGN (gv_ps_ref) TO <gt_ps_table>.

The ASSIGN part works beautifully - in debugging I now have access to the data in the PS table. But I need to store the data locally, as PS gets refreshed by this, which then also refreshes my <GT_PS_TABLE>:

PERFORM initialize_ps(sapfp50p).

The thing is, I need the data for later use, so I want to separate the link and create a local copy.

This code (most of it copied from other solutions here on SDN) works fine, but notice that the DESCRIBE_BY_DATA method is called with the GS_DATA structure. This means that the TYPE of the table that I want to create dynamically is known.

DATA: BEGIN OF gt_data OCCURS 5,

char TYPE text5,

numc TYPE numc5,

dec TYPE pad_amt7s,

END OF gt_data.

DATA: gs_data LIKE LINE OF gt_data.

DATA: go_struct TYPE REF TO cl_abap_structdescr,

go_new_type TYPE REF TO cl_abap_structdescr,

go_new_tab TYPE REF TO cl_abap_tabledescr,

gt_comp TYPE cl_abap_structdescr=>component_table,

go_data TYPE REF TO data.

FIELD-SYMBOLS: <fs_any> TYPE ANY TABLE.

go_struct ?= cl_abap_typedescr=>describe_by_data( gs_data ). "see alternate reference below, i.e. my need

gt_comp = go_struct->get_components( ).

go_new_type = cl_abap_structdescr=>create( gt_comp ).

go_new_tab = cl_abap_tabledescr=>create(

p_line_type = go_new_type

p_table_kind = cl_abap_tabledescr=>tablekind_std

p_unique = abap_false ).

CREATE DATA go_data TYPE HANDLE go_new_tab.

ASSIGN go_data->* TO <fs_any>.

<fs_any> = gt_data[].

As indicated, having only a reference to the PS table (<GT_PS_TABLE>) is not enough, I want to create a copy of PS locally, without knowing how it is declared in FP50PPSB (partly via a structure, partly via separate fields), as this will make my coding insensitive to any change in the structure.

This is my visualization of what I want to do, or the likes - I believe you get the idea. But is it possible?

go_struct ?= cl_abap_typedescr=>describe_by_data( <gt_ps_table> ).

The syntax checker allows for this, but it dumps at runtime

2 REPLIES 2
Read only

jrg_wulf
Active Contributor
0 Likes
1,411

Hi,

besides that your posting is allmost unreadable (try preview before posting), your basic idea is correct. You just overdid it a bit.

Just try something like this


  data: a_descr TYPE REF TO CL_ABAP_TYPEDESCR.
  FIELD-SYMBOLS: <val> type any.
     a_descr = cl_abap_datadescr=>describe_by_data( p_data =  st_val ).
    CREATE DATA me->d_ref type (a_descr->ABSOLUTE_NAME).
    assign me->d_ref->* to <val>.
    <val> = st_val.

this should work fine with all types of data, structures or tables.

Regards

Jörg

Read only

Former Member
0 Likes
1,410

Hi Jörg,

Thanks for your help - it actually works

Sorry for the composition of my text - I must admit that I didn't check it, as it looked ok when typing. I will keep an eye on that next time.

Points have been awarded - thanks again.