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 create dynamic nested internal table

Former Member
0 Likes
2,144

Hi Experts,

Pleae tell me or give sample code, how to create dynamic nested internal table ?

I have seen threads saying creation of dynamic internal tables using some table structure only. But now the requirement is to create dynamic nested internal table.

For example the internal table contains two fields viz., one is field1 of dynamic internal table and other is normal field2 and values as shown below:

Nested internal table:

field1 | field2 ...

-


<table content1> | value2..

<table content1> | value2..

Here the [table content] should also a dynamic internal table.

Let me know if you need any other info.

regards

Saravanan R

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,180

Hello Saravanan

You may have a look at my Wiki posting

[ Creating Flat and Complex Internal Tables Dynamically using RTTI |https://wiki.sdn.sap.com/wiki/display/Snippets/CreatingFlatandComplexInternalTablesDynamicallyusingRTTI]

Regards

Uwe

4 REPLIES 4
Read only

Former Member
0 Likes
1,180

You can create Nested Internal table Dynamically like Normal Internal table .

Give the Dataelement

Populate the fieldcatalog for other Fields.


data: 
      lt_comptab         TYPE cl_abap_structdescr=>component_table,
      ls_comp            LIKE LINE OF lt_comptab,
      lref_newstr        TYPE REF TO cl_abap_structdescr,
      lref_tab_type      TYPE REF TO cl_abap_tabledescr.


LOOP AT lt_fcat INTO ls_fcat.
      IF ls_fcat-ref_table IS NOT INITIAL.
        CLEAR ls_dd03p.
        CALL FUNCTION 'BUS_DDFIELD_GET'
          EXPORTING
            i_tabnm         = ls_fcat-ref_table
            i_fldnm         = ls_fcat-fieldname
          IMPORTING
            e_dd03p         = ls_dd03p
          EXCEPTIONS
            field_not_found = 1
            OTHERS          = 2.
        IF sy-subrc EQ 0.
          ls_comp-name = ls_fcat-fieldname.
          ls_comp-type ?= cl_abap_datadescr=>describe_by_name( ls_dd03p-rollname ).
          APPEND ls_comp TO lt_comptab.
          CLEAR ls_comp.
        ENDIF.
      ELSE.
        ls_comp-name = ls_fcat-fieldname.
        ls_comp-type ?= cl_abap_datadescr=>describe_by_name( ls_fcat-rollname ).
        APPEND ls_comp TO lt_comptab.
        CLEAR ls_comp.
      ENDIF.
    ENDLOOP.

*Now for the Field which you want deep table then you can do like this
      ls_fcat-fieldname  = 'NESTED_TABLE'.
      ls_fcat-inttype    = 'C'.
      ls_fcat-intlen     = '000006'.
      ls_fcat-rollname   = 'SFLIGHT_TAB1'. "For SFLIGHT   
      APPEND ls_fcat TO lt_fcat.


    ls_comp-name = ls_fcat-fieldname.
    ls_comp-type ?= cl_abap_datadescr=>describe_by_name( ls_fcat-rollname ).
    INSERT ls_comp INTO lt_comptab INDEX lv_count.
    CLEAR ls_comp.
    ADD 1 TO lv_count.

    lref_newstr = cl_abap_structdescr=>create( lt_comptab ).
    lref_tab_type = cl_abap_tabledescr=>create( lref_newstr ).

Now you can define your internal table using lref_tab_type.

Read only

0 Likes
1,180

Hi Vijay,

Thanks for giving quick reply!

It seems you have given part of code from the program. But the above code does not seem to work, it gives syntax error "COMPONENT_TABLE" is unkown"

Please check to help.

regards

Saravanan R

Read only

0 Likes
1,180

see the complete code..i am currently working in ECC6.0 EHP4. just check which version you are using..

REPORT  yst_test_000.

DATA:
      lt_comptab         TYPE cl_abap_structdescr=>component_table,
      ls_comp            LIKE LINE OF lt_comptab,
      lref_newstr        TYPE REF TO cl_abap_structdescr,
      lref_tab_type      TYPE REF TO cl_abap_tabledescr,
      lt_fcat            TYPE lvc_t_fcat,
      ls_fcat            TYPE lvc_s_fcat,
      ls_dd03p           TYPE dd03p,
      lt_data            type ref to data.
field-symbols: <fs_table> type standard table.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
  EXPORTING
    i_structure_name       = 'SCARR'
  CHANGING
    ct_fieldcat            = lt_fcat
  EXCEPTIONS
    inconsistent_interface = 1
    program_error          = 2
    OTHERS                 = 3.
IF sy-subrc NE 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

LOOP AT lt_fcat INTO ls_fcat.
  IF ls_fcat-ref_table IS NOT INITIAL.
    CLEAR ls_dd03p.
    CALL FUNCTION 'BUS_DDFIELD_GET'
      EXPORTING
        i_tabnm         = ls_fcat-ref_table
        i_fldnm         = ls_fcat-fieldname
      IMPORTING
        e_dd03p         = ls_dd03p
      EXCEPTIONS
        field_not_found = 1
        OTHERS          = 2.
    IF sy-subrc EQ 0.
      ls_comp-name = ls_fcat-fieldname.
      ls_comp-type ?= cl_abap_datadescr=>describe_by_name( ls_dd03p-rollname ).
      APPEND ls_comp TO lt_comptab.
      CLEAR ls_comp.
    ENDIF.
  ELSE.
    ls_comp-name = ls_fcat-fieldname.
    ls_comp-type ?= cl_abap_datadescr=>describe_by_name( ls_fcat-rollname ).
    APPEND ls_comp TO lt_comptab.
    CLEAR ls_comp.
  ENDIF.
ENDLOOP.

*Now for the Field which you want deep table then you can do like this
ls_fcat-fieldname  = 'NESTED_TABLE'.
ls_fcat-inttype    = 'C'.
ls_fcat-intlen     = '000006'.
ls_fcat-rollname   = 'SFLIGHT_TAB1'. "For SFLIGHT
APPEND ls_fcat TO lt_fcat.


ls_comp-name = ls_fcat-fieldname.
ls_comp-type ?= cl_abap_datadescr=>describe_by_name( ls_fcat-rollname ).
APPEND ls_comp TO lt_comptab.
CLEAR ls_comp.


lref_newstr = cl_abap_structdescr=>create( lt_comptab ).
lref_tab_type = cl_abap_tabledescr=>create( lref_newstr ).

create data lt_data type handle lref_tab_type.
assign lt_data->* to <fs_table>.
 
break-point.

Edited by: Vijay Babu Dudla on Apr 28, 2009 8:05 AM

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,181

Hello Saravanan

You may have a look at my Wiki posting

[ Creating Flat and Complex Internal Tables Dynamically using RTTI |https://wiki.sdn.sap.com/wiki/display/Snippets/CreatingFlatandComplexInternalTablesDynamicallyusingRTTI]

Regards

Uwe