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

dynamical columns in internal table

Former Member
0 Likes
776

Hi all.

I need to make an internal table with dynamical columns(i have 3 static columns and the rest depends on the number of clients that i have in a month). If it's possible, how can i do it?

Can anyone please help.

Thanks & Regards

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
740

Define the field catalog like you were building an ALV report and create an internal table using the following method.



DATA: S_FCAT         TYPE LVC_S_FCAT,
      T_FCAT         TYPE LVC_T_FCAT,
      RE_TABLE       TYPE REF TO DATA,
      RE_HEAD        TYPE REF TO DATA.

FIELD-SYMBOLS: <re_table> TYPE TABLE,
               <re_head>.

ADD 1 TO L_CNT.
MOVE: L_CNT  TO S_FCAT-COL_POS,
      'TYPE' TO S_FCAT-FIELDNAME,
      'C'    TO S_FCAT-INTTYPE,
      '1'    TO S_FCAT-INTLEN.
APPEND S_FCAT TO T_FCAT.

ADD 1 TO L_CNT.
MOVE: L_CNT  TO S_FCAT-COL_POS,
      'DATE' TO S_FCAT-FIELDNAME,
      'D'    TO S_FCAT-INTTYPE,
      '8'    TO S_FCAT-INTLEN.
APPEND S_FCAT TO T_FCAT.

ADD 1 TO L_CNT.
MOVE: L_CNT  TO S_FCAT-COL_POS,
      'TIME' TO S_FCAT-FIELDNAME,
      'T'    TO S_FCAT-INTTYPE,
      '6'    TO S_FCAT-INTLEN.
APPEND S_FCAT TO T_FCAT.

ADD 1 TO L_CNT.
MOVE: L_CNT  TO S_FCAT-COL_POS,
      'NAME' TO S_FCAT-FIELDNAME,
      'C'    TO S_FCAT-INTTYPE,
      '12'   TO S_FCAT-INTLEN.
APPEND S_FCAT TO T_FCAT.

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
  EXPORTING
    IT_FIELDCATALOG           = T_FCAT
  IMPORTING
    EP_TABLE                  = RE_TABLE
        .

T_FCAT contains the list of fields to be included.

After the table has been created you can access it using field-symbols.


ASSIGN RE_TABLE->* TO <re_table>.
CREATE DATA RE_HEAD LIKE LINE OF <re_table>.
ASSIGN RE_HEAD->* TO <re_head>.

<re_table> is your internal table and <re_head> is a header record defined like the table.

Hi all.

I need to make an internal table with dynamical columns(i have 3 static columns and the rest depends on the number of clients that i have in a month). If it's possible, how can i do it?

Can anyone please help.

Thanks & Regards

7 REPLIES 7
Read only

Former Member
0 Likes
741

Define the field catalog like you were building an ALV report and create an internal table using the following method.



DATA: S_FCAT         TYPE LVC_S_FCAT,
      T_FCAT         TYPE LVC_T_FCAT,
      RE_TABLE       TYPE REF TO DATA,
      RE_HEAD        TYPE REF TO DATA.

FIELD-SYMBOLS: <re_table> TYPE TABLE,
               <re_head>.

ADD 1 TO L_CNT.
MOVE: L_CNT  TO S_FCAT-COL_POS,
      'TYPE' TO S_FCAT-FIELDNAME,
      'C'    TO S_FCAT-INTTYPE,
      '1'    TO S_FCAT-INTLEN.
APPEND S_FCAT TO T_FCAT.

ADD 1 TO L_CNT.
MOVE: L_CNT  TO S_FCAT-COL_POS,
      'DATE' TO S_FCAT-FIELDNAME,
      'D'    TO S_FCAT-INTTYPE,
      '8'    TO S_FCAT-INTLEN.
APPEND S_FCAT TO T_FCAT.

ADD 1 TO L_CNT.
MOVE: L_CNT  TO S_FCAT-COL_POS,
      'TIME' TO S_FCAT-FIELDNAME,
      'T'    TO S_FCAT-INTTYPE,
      '6'    TO S_FCAT-INTLEN.
APPEND S_FCAT TO T_FCAT.

ADD 1 TO L_CNT.
MOVE: L_CNT  TO S_FCAT-COL_POS,
      'NAME' TO S_FCAT-FIELDNAME,
      'C'    TO S_FCAT-INTTYPE,
      '12'   TO S_FCAT-INTLEN.
APPEND S_FCAT TO T_FCAT.

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
  EXPORTING
    IT_FIELDCATALOG           = T_FCAT
  IMPORTING
    EP_TABLE                  = RE_TABLE
        .

T_FCAT contains the list of fields to be included.

After the table has been created you can access it using field-symbols.


ASSIGN RE_TABLE->* TO <re_table>.
CREATE DATA RE_HEAD LIKE LINE OF <re_table>.
ASSIGN RE_HEAD->* TO <re_head>.

<re_table> is your internal table and <re_head> is a header record defined like the table.

Read only

0 Likes
740

Hi Michael.

This is very helpful, but how can i introduce data in the internal table?

Thanks & Regards

Read only

0 Likes
740

You can use another field-symbol to represent a specific field of your header record.


FIELD-SYMBOLS: <field>  TYPE ANY.

ASSIGN COMPONENT 'FIELD1'
  OF STRUCTURE <re_head> TO <field>.

Now <re_head>-FIELD1 can be populated by moving a value to <field>.

Once your header record is populated, just append it like normal.


APPEND <re_head> TO <re_table>. CLEAR <re_head>.

Read only

0 Likes
740

Thanks a lot Michael & Sudheer

Read only

Former Member
0 Likes
740

It is difficult to define an internal table with dynamic number of colns...but you can use the below logic.

itab

f1

f2

f3

client

value.

you can collect all the values in itab and when you want to print it you can use the below logic.

sort itab by client.

iclient[] = itab[]

delete adjacent duplicate from iclient.

*For headings

write : / 'Headf1', 'HeadF2', 'HeadF3'.

loop at iclient.

write iclient-client.

endloop.

loop at itab.

at new f3.

write : / itab-f1, itab-f2, itab-f3.

endat.

read table iclient with key client = itab-client.

lcnt = sy-tabix * 10. "10 is the field length.

write : (lcnt) itab-value.

endloop.

Read only

Former Member
0 Likes
740

Hi,

type-pools : abap. 
  field-symbols: <dyn_table> type standard table, 
               <dyn_wa>, 
               <dyn_field>. 
  data: dy_table type ref to data, 
      dy_line  type ref to data, 
      xfc type lvc_s_fcat, 
      ifc type lvc_t_fcat. 
  selection-screen begin of block b1 with frame. 
parameters: p_table(30) type c default 'T001'. 
selection-screen end of block b1. 
  start-of-selection. 
    perform get_structure. 
  perform create_dynamic_itab.  <b> *Creates a dyanamic internal table **</b>  
 perform get_data. 
  perform write_out. 
  form get_structure. 
  data : idetails type abap_compdescr_tab, 
       xdetails type abap_compdescr. 
  data : ref_table_des type ref to cl_abap_structdescr. 
  * Get the structure of the table. 
  ref_table_des ?=  
      cl_abap_typedescr=>describe_by_name( p_table ). 
  idetails[] = ref_table_des->components[]. 
    loop at idetails into xdetails. 
    clear xfc. 
    xfc-fieldname = xdetails-name . 
    xfc-datatype = xdetails-type_kind. 
    xfc-inttype = xdetails-type_kind. 
    xfc-intlen = xdetails-length. 
    xfc-decimals = xdetails-decimals. 
    append xfc to ifc. 
  endloop. 
  endform. 
  form create_dynamic_itab. 
  <b>* Create dynamic internal table and assign to FS</b> 
  call method cl_alv_table_create=>create_dynamic_table 
               exporting 
                  it_fieldcatalog = ifc 
               importing 
                  ep_table        = dy_table. 
    assign dy_table->* to <dyn_table>. 
  * Create dynamic work area and assign to FS 
  create data dy_line like line of <dyn_table>. 
  assign dy_line->* to <dyn_wa>. 
  endform. 
    
  form get_data. 
  * Select Data from table. 
  select * into table <dyn_table> 
             from (p_table). 
  endform. 
   Write out data from table. 
  loop at <dyn_table> into <dyn_wa>. 
    do. 
      assign component  sy-index   
         of structure <dyn_wa> to <dyn_field>. 
      if sy-subrc <> 0. 
        exit. 
      endif. 
      if sy-index = 1. 
        write:/ <dyn_field>. 
      else. 
        write: <dyn_field>. 
      endif. 
    enddo. 
  endloop. 

Regards

Sudheer

Read only

Former Member
0 Likes
740

Hi,

You can create the internal table with the help of above answers, after creating the Dynamic one, you can use as a normal internal table.. see the above example, you will find a Select statment and Loop at statment etc ...

Regards

Sudheer