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: 

Adding fields to an internal table at runtime

Former Member
0 Kudos
1,679

Hi,

Would you know of way to add additional fields/columns to an existing internal table at runtime?

I know how to generate a table dynamically from scratch, but here I want to add fields to an <B>EXISTING INTERNAL TABLE</B>. Even it is WAS 6.40 let me know.

Any idea will be appreciated ..

Regards,

Ravi

5 REPLIES 5

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos
355

The only way is to build it from the beginning, once you create an instance of the internal table, you can't change it.

Here is a small sample program which uses a dynamic internal table.

report zrich_0003
       no standard page heading.

type-pools: slis.

field-symbols: <dyn_table> type standard table,
               <dyn_wa>.

data: alv_fldcat type slis_t_fieldcat_alv,
      it_fldcat type lvc_t_fcat.


selection-screen begin of block b1 with frame title text-001.
parameters: p_check type c.
selection-screen end of block b1.

start-of-selection.

  perform build_dyn_itab.
  perform build_report.

  loop at <dyn_table> into <dyn_wa>.
    write:/ <dyn_wa>.
  endloop.


************************************************************************
*  Build_dyn_itab
************************************************************************
form build_dyn_itab.

  data: index(3) type c.

  data: new_table type ref to data,
        new_line  type ref to data,
        wa_it_fldcat type lvc_s_fcat.

* Create fields
  clear index.
  do 10 times.
    index = sy-index.
    clear wa_it_fldcat.
    concatenate 'Field' index into
             wa_it_fldcat-fieldname .
    condense  wa_it_fldcat-fieldname no-gaps.
    wa_it_fldcat-datatype = 'CHAR'.
    wa_it_fldcat-intlen = 5.
    append wa_it_fldcat to it_fldcat .
  enddo.

* Create dynamic internal table and assign to FS
  call method cl_alv_table_create=>create_dynamic_table
               exporting
                  it_fieldcatalog = it_fldcat
               importing
                  ep_table        = new_table.

  assign new_table->* to <dyn_table>.

* Create dynamic work area and assign to FS
  create data new_line like line of <dyn_table>.
  assign new_line->* to <dyn_wa>.

endform.

*********************************************************************
*      Form  build_report
*********************************************************************
form build_report.

  data: fieldname(20) type c.
  data: fieldvalue(5) type c.
  data: index(3) type c.
  field-symbols: <fs1>.

  do 10 times.

    index = sy-index.

* Set up fieldname
    concatenate 'FIELD' index into
             fieldname .
    condense   fieldname  no-gaps.

* Set up fieldvalue
    concatenate 'FLD' index into
             fieldvalue.
    condense   fieldvalue no-gaps.

    assign component  fieldname  of structure <dyn_wa> to <fs1>.
    <fs1> =  fieldvalue.

  enddo.

* Append to the dynamic internal table
  append <dyn_wa> to <dyn_table>.

endform.

Regards,

Rich Heilman

0 Kudos
355

Rich,

I know this.

Remember my weblog about the new BADI.

/people/ravikumar.allampallam/blog/2005/12/05/need-a-way-to-change-appearance-of-a-standard-existing-alv-report

I want to add more fields to the output of the report here. I am able to fields to the FIELDCAT, however the actual internal table is not being changed.

regards,

Ravi

0 Kudos
355

Oh yes, Good weblog. You know more about than I do. I was under the impression that once you create your internal table either statically or at runtime, that you can not change it.

Regards,

Rich Heilman

0 Kudos
355

create a dynamic table(it_new) and fieldcatalog for the additional fields

<b>as u had told that fieldcatalog is ready</b>

by using field symbols

Assign component 'fieldname' of structure it_fieldcat

to <fld_symbol>

this will work

if u want sample code i will send

Former Member
0 Kudos
355

pls let me know if there any clarifications

as i had got the same problem few days ago and solved it

lets say fld1 , fld2 and fld3 are the additional fields

these 3 flds can be updated to fieldcatalog.

now ur problem is to update the values for these 3 fields in the internal table , right??

Assign componet 'FLD1' of structure it_fielcat to <fld_symbol>

<fld_symbol> = your internal table value for the field

this will update the internal table field with the value in fld1