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

appending column to an dynamically created internal table

0 Likes
642

Hi folks,

i have an dynamically created internal table <dyn_tab> which is based on a parameter p_table ( containing the table name ). now i want to add one more column to this <dyn_tab> table where i can store further information in. how can i do that ?

i appreciate your help!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
626

Hi,

Please check this sample program.


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.      


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. 

*Add your new field(s) into table ifc here.
  ...  


endform. 


form create_dynamic_itab. 
* Create dynamic internal table and assign to FS 
  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. 

Regards,

Ferry Lianto

4 REPLIES 4
Read only

former_member194669
Active Contributor
0 Likes
626

  call function 'LVC_FIELDCATALOG_MERGE'
    exporting
      i_structure_name = p_table
    changing
      ct_fieldcat      = i_fcat
    exceptions
      others           = 1.

<b>

    • Here add your field name in the i_fcat internal table (field catalog)

</b>


  
 call method cl_alv_table_create=>create_dynamic_table
    exporting
      it_fieldcatalog = i_fcat
    importing
      ep_table        = i_content.
  if sy-subrc = 0.
    assign i_content->* to <ptab>.
  else.
    write: 'Error creating internal table'.
    stop.
  endif.

aRs

Read only

0 Likes
626

Hi, thanks for your immediate answer:

one question is left - as it dumps ( the i_content table stays initial ) - how did you define i_content and i_fcat ?

Message was edited by:

Clemens Leider

Read only

Former Member
0 Likes
627

Hi,

Please check this sample program.


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.      


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. 

*Add your new field(s) into table ifc here.
  ...  


endform. 


form create_dynamic_itab. 
* Create dynamic internal table and assign to FS 
  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. 

Regards,

Ferry Lianto

Read only

former_member194669
Active Contributor
0 Likes
626

field-symbols:
      <ptab>        type standard table.

data: i_fcat        type lvc_t_fcat.
data: i_content     type ref to data.

aRs