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

Duplicate internal table definition and add additional fields

fcousin
Explorer
0 Likes
2,515

Hi,

What is the easiest way to dynamically define an internal table based on an existing internal table but add additional fields to the second table. This is to be done dynamically, not through a DATA statement.

types: begin of t_ds,
ds type DDOBJNAME,
struct type DDOBJNAME,
type(01) type c,
data type DDOBJNAME,
febko type febko, "DEEP STRUCTURE
febep type febep, "DEEP STRUCTURE
febre type t_febre,
end of t_ds .

types:
t_ds_tab type table of t_ds .

I want to dynamically define an internal table exactly like t_ds but with an additional field (deep):
bsid type bsid.

Thanks,
Fred

1 ACCEPTED SOLUTION
Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
2,122

RTTS can do this.

Example code (not clean and not handled exceptions!)

DATA: lo_structdescr_in  TYPE REF TO cl_abap_structdescr,
      lo_structdescr_out TYPE REF TO cl_abap_structdescr,
      lo_datadescr       TYPE REF TO cl_abap_datadescr,
      lt_components      TYPE cl_abap_structdescr=>component_table,
      lref_tab           TYPE REF TO data.

TYPES: BEGIN OF lty_test_s,
         t100        TYPE t100, "structure
         test_field1 TYPE string,
       END OF lty_test_s.

*--------------------------------------------------------------------*

"Get original components
lo_structdescr_in ?= cl_abap_structdescr=>describe_by_name( 'LTY_TEST_S' ).
lt_components = lo_structdescr_in->get_components( ).

"Update components
lo_datadescr ?= cl_abap_elemdescr=>describe_by_name( 'BSID' ).
APPEND VALUE cl_abap_structdescr=>component( name = 'BSID'
                                             type = lo_datadescr ) TO lt_components.

"Create new dynamic structure and new table
lo_structdescr_out ?= cl_abap_structdescr=>create( p_components = lt_components ).
DATA(lo_tabdesc) = cl_abap_tabledescr=>create( lo_structdescr_out ).
CREATE DATA lref_tab TYPE HANDLE lo_tabdesc.
ASSIGN lref_tab->* TO FIELD-SYMBOL(<lt_tab>).
-- Tomas --

Hi,

What is the easiest way to dynamically define an internal table based on an existing internal table but add additional fields to the second table. This is to be done dynamically, not through a DATA statement.

types: begin of t_ds,
ds type DDOBJNAME,
struct type DDOBJNAME,
type(01) type c,
data type DDOBJNAME,
febko type febko, "DEEP STRUCTURE
febep type febep, "DEEP STRUCTURE
febre type t_febre,
end of t_ds .

types:
t_ds_tab type table of t_ds .

I want to dynamically define an internal table exactly like t_ds but with an additional field (deep):
bsid type bsid.

Thanks,
Fred

6 REPLIES 6
Read only

FredericGirod
Active Contributor
0 Likes
2,122

Hi Fred

you could create an internal table based on a field catalog (like ALV Grid)


  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = t_fldcat
    IMPORTING
      ep_table        = o_newtable.
  ASSIGN o_newtable->* TO <t_dyntable>. 
Read only

0 Likes
2,122

Thank you Frederic.

That is the road I am taking but was wondering if there was an easier way.
My original table is also a dynamically create table so I need to extract its definition in the field catalog and insert the new field.

Just cumbersome 🙂

Merci,
Fred

Read only

2,122

Prefer RTTS to this non-official method which works with GENERATE SUBROUTINE POOL (limited to 36 calls in one program then runtime error).

Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
2,123

RTTS can do this.

Example code (not clean and not handled exceptions!)

DATA: lo_structdescr_in  TYPE REF TO cl_abap_structdescr,
      lo_structdescr_out TYPE REF TO cl_abap_structdescr,
      lo_datadescr       TYPE REF TO cl_abap_datadescr,
      lt_components      TYPE cl_abap_structdescr=>component_table,
      lref_tab           TYPE REF TO data.

TYPES: BEGIN OF lty_test_s,
         t100        TYPE t100, "structure
         test_field1 TYPE string,
       END OF lty_test_s.

*--------------------------------------------------------------------*

"Get original components
lo_structdescr_in ?= cl_abap_structdescr=>describe_by_name( 'LTY_TEST_S' ).
lt_components = lo_structdescr_in->get_components( ).

"Update components
lo_datadescr ?= cl_abap_elemdescr=>describe_by_name( 'BSID' ).
APPEND VALUE cl_abap_structdescr=>component( name = 'BSID'
                                             type = lo_datadescr ) TO lt_components.

"Create new dynamic structure and new table
lo_structdescr_out ?= cl_abap_structdescr=>create( p_components = lt_components ).
DATA(lo_tabdesc) = cl_abap_tabledescr=>create( lo_structdescr_out ).
CREATE DATA lref_tab TYPE HANDLE lo_tabdesc.
ASSIGN lref_tab->* TO FIELD-SYMBOL(<lt_tab>).
-- Tomas --
Read only

0 Likes
2,122

Thank you Tomas,

It worked!
I had to adjust the code to the following since my original internal table (DS) was dynamically defined:

lo_descr ?= cl_abap_typedescr=>describe_by_data( DS ).
lo_type = lo_descr->get_table_line_type( ).
lo_struct ?= cl_abap_typedescr=>describe_by_name( lo_type->absolute_name ).
lt_components = lo_struct->get_components( ).

lo_datadescr ?= cl_abap_elemdescr=>describe_by_name( 'BSID' ).
APPEND VALUE cl_abap_structdescr=>component( name = 'BSID'
type = lo_datadescr ) TO lt_components.

"Create new dynamic structure and new table
lo_structdescr_out ?= cl_abap_structdescr=>create( p_components = lt_components ).
DATA(lo_tabdesc) = cl_abap_tabledescr=>create( lo_structdescr_out ).
CREATE DATA lref_tab TYPE HANDLE lo_tabdesc.
ASSIGN lref_tab->* TO FIELD-SYMBOL(<lt_tab>).

Thanks,
Fred

Read only

0 Likes
2,122

fcousin Please do not forget to take care of exceptions 🙂

-- Tomas --