Application Development 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: 

Dynamic Internal table using another internal table values

Former Member
0 Kudos

Hello All,

I have an internal table ITAB1, which will get populated inside the program. This ITAB1 will have only one field.

I want to create an internal table dynamically with the values of ITAB1( single field internal table ) as fields.

Thanks in advance.

Best Regards,

Sasidhar Reddy Matli.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

u can do it like this....


  DATA: struct_type TYPE REF TO cl_abap_structdescr,
        comp_tab    TYPE cl_abap_structdescr=>component_table,
        dref        TYPE REF TO data,
        itab_type   TYPE REF TO cl_abap_tabledescr,
        wa_comp     LIKE LINE OF comp_tab,
        index1      TYPE char10.

  LOOP AT excel_tab.
      index1 = sy-tabix.
      CONCATENATE 'COL' '_' index1 INTO wa_comp-name.
      CONDENSE wa_comp-name NO-GAPS.
      wa_comp-type ?= cl_abap_datadescr=>describe_by_name( 'STRING' ).
      APPEND  wa_comp TO comp_tab.
  ENDLOOP.

  struct_type = cl_abap_structdescr=>create( comp_tab ).
  itab_type   = cl_abap_tabledescr=>create( struct_type ).
  CREATE DATA dref TYPE HANDLE itab_type.
  ASSIGN dref->* TO <dyn_table>.
  CREATE DATA dref TYPE HANDLE struct_type.
  ASSIGN dref->* TO <dyn_wa>.

4 REPLIES 4

Former Member
0 Kudos

Hi

try this code...



      wa_ifc-col_pos = '1'.
      wa_ifc-fieldname = 'LINE_COLOR'.
      wa_ifc-tabname = 1.
      wa_ifc-datatype = 'CHAR'.
      APPEND wa_ifc TO ifc.
      CLEAR wa_ifc.


   CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
          it_fieldcatalog = ifc
        IMPORTING
          ep_table        = dy_month.

      ASSIGN dy_month->* TO <dyn_month>.
      CREATE DATA dy_wa LIKE LINE OF <dyn_month>.
      ASSIGN dy_wa->* TO <wa_month>.

Arunima

0 Kudos

at this wiki

https://www.sdn.sap.com/irj/sdn/wiki?path=/display/abap/individualcellcoloringindynamic+alv

you can find an example where I build an internal table

you can look at the coding where I loop though the internal table and at the value to the fieldname.


* dynamic fields
LOOP AT ta_pernrs INTO wa_pernrs.

WRITE wa_pernrs-pernr TO h_ri_pernr.
is_lvc_cat-fieldname = wa_pernrs-pernr.

is_lvc_cat-ref_field = 'massn'.

is_lvc_cat-ref_table = 'PERNR'.

is_lvc_cat-just = 'C'.

CONCATENATE it_0002-inits it_0002-nachn(1) INTO is_lvc_cat-scrtext_s SEPARATED BY space.

CONCATENATE it_0002-inits it_0002-nachn INTO is_lvc_cat-scrtext_m SEPARATED BY space.

CONCATENATE it_0002-inits it_0002-nachn INTO is_lvc_cat-scrtext_l SEPARATED BY space.

APPEND is_lvc_cat TO it_lvc_cat.

and then ofcourse the creation of the table which is also mentioned in the earlier reply


CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_lvc_cat

IMPORTING

*ep_table = ta_output.*** Create a new Line with the same structure of the table.

ASSIGN ta_output->* TO <ta_output>.*

CREATE DATA new_line LIKE LINE OF <ta_output>.

ASSIGN new_line->* TO <l_line>. 


kind regards

arthur de smidt

Former Member
0 Kudos

u can do it like this....


  DATA: struct_type TYPE REF TO cl_abap_structdescr,
        comp_tab    TYPE cl_abap_structdescr=>component_table,
        dref        TYPE REF TO data,
        itab_type   TYPE REF TO cl_abap_tabledescr,
        wa_comp     LIKE LINE OF comp_tab,
        index1      TYPE char10.

  LOOP AT excel_tab.
      index1 = sy-tabix.
      CONCATENATE 'COL' '_' index1 INTO wa_comp-name.
      CONDENSE wa_comp-name NO-GAPS.
      wa_comp-type ?= cl_abap_datadescr=>describe_by_name( 'STRING' ).
      APPEND  wa_comp TO comp_tab.
  ENDLOOP.

  struct_type = cl_abap_structdescr=>create( comp_tab ).
  itab_type   = cl_abap_tabledescr=>create( struct_type ).
  CREATE DATA dref TYPE HANDLE itab_type.
  ASSIGN dref->* TO <dyn_table>.
  CREATE DATA dref TYPE HANDLE struct_type.
  ASSIGN dref->* TO <dyn_wa>.

Former Member
0 Kudos

Thank you for your answers.

Best Regards,

Sasidhar Reddy Matli.