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

Dynamic Internal table field addition.

Former Member
0 Likes
454

Hi Gurus,

Can we add fields to Internal table dynamically during run time. If possible please tell me the procedure.

With regards,

Mahendiran.

1 ACCEPTED SOLUTION
Read only

mvoros
Active Contributor
0 Likes
428

Hi,

you can create table with dynamic structure and use it in your program. Here is a beautiful [presentation|https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/media/uuid/77ea5c70-0701-0010-e190-c93da2025704] about generic programming in ABAP. There is also [eClass|https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/media/uuid/77ea5c70-0701-0010-e190-c93da2025704] for it.

Cheers

3 REPLIES 3
Read only

mvoros
Active Contributor
0 Likes
429

Hi,

you can create table with dynamic structure and use it in your program. Here is a beautiful [presentation|https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/media/uuid/77ea5c70-0701-0010-e190-c93da2025704] about generic programming in ABAP. There is also [eClass|https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/media/uuid/77ea5c70-0701-0010-e190-c93da2025704] for it.

Cheers

Read only

Former Member
0 Likes
428

Check this Link

Read only

Former Member
0 Likes
428

Hi....

Please find following sample code in which dynamic internal table is created for MARA table ( You can also use Types here ) and if field is MATNR or ERSDA then it will be added dynamically at runtime to Internal table.



REPORT  ZPP_FS_CHINTAN.

DATA : G_TABNAME TYPE STRING VALUE 'MARA',
       G_DREF TYPE REF TO DATA.

DATA : SMARATYPE TYPE REF TO CL_ABAP_STRUCTDESCR,
       TABLETYPE TYPE REF TO CL_ABAP_TABLEDESCR,
       COMP_TAB TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE,
       NEW_COMP_TAB LIKE COMP_TAB,
       LINETYPE TYPE REF TO CL_ABAP_STRUCTDESCR,
       DREF TYPE REF TO DATA.

FIELD-SYMBOLS : <WA_COMP> LIKE LINE OF COMP_TAB,
                <FS_ITAB> TYPE ANY TABLE.

CREATE DATA G_DREF TYPE TABLE OF (G_TABNAME).

FIELD-SYMBOLS : <FS_STAB> TYPE STANDARD TABLE.

ASSIGN G_DREF->* TO <FS_STAB>.

SELECT * FROM MARA INTO TABLE <FS_STAB> WHERE MATNR = '0'.

SMARATYPE ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME('MARA').
COMP_TAB = SMARATYPE->GET_COMPONENTS( ).

LOOP AT COMP_TAB ASSIGNING <WA_COMP>.
  CASE <WA_COMP>-NAME.
    WHEN 'MATNR' or 'ERSDA'.
      APPEND <WA_COMP> TO NEW_COMP_TAB.
  ENDCASE.
ENDLOOP.

LINETYPE = CL_ABAP_STRUCTDESCR=>CREATE( NEW_COMP_TAB ).
TABLETYPE = CL_ABAP_TABLEDESCR=>CREATE(
P_LINE_TYPE = LINETYPE
P_TABLE_KIND = CL_ABAP_TABLEDESCR=>TABLEKIND_STD ).

CREATE DATA DREF TYPE HANDLE TABLETYPE.
ASSIGN DREF->* TO <FS_ITAB>.

SELECT * FROM MARA INTO CORRESPONDING FIELDS OF TABLE <FS_ITAB>.

Hope this will help u.

Regards,

Chintan.