2020 Jan 27 12:03 PM
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
2020 Jan 27 2:31 PM
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>).
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
2020 Jan 27 12:20 PM
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>.
2020 Jan 27 2:23 PM
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
2020 Jan 27 3:04 PM
Prefer RTTS to this non-official method which works with GENERATE SUBROUTINE POOL (limited to 36 calls in one program then runtime error).
2020 Jan 27 2:31 PM
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>).
2020 Jan 27 3:48 PM
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
2020 Jan 28 7:37 AM
fcousin Please do not forget to take care of exceptions 🙂
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |