Skip to Content
Technical Articles
Author's profile photo Susmitha Susan Thomas

Dynamic Internal Table iIlustrated with an example of creating the transpose of internal table

Build a Dynamic Internal Table in 1 – 2 – 3  Steps

To create the dynamic table using the much recommended methods in RTTS, refer this document. Create Dynamic Table using RTTS and display in ALV

The method mentioned below is now obsolete and is not preferred anymore.

To learn about field symbols and data references, continue reading.

Creating a dynamic table is not a big deal. Once you understand the concept, it is as simple as 1-2-3. So if your picture about dynamic internal table is something complicated, you need to change that mindset first before proceeding to learn about it.

Dynamic internal table is an internal table with variable rows and columns which can be defined during run time. The different attributes that can be defined at run time includes the field name, column attributes, data type, width, reference table and reference fields. There are just three main steps involved in it.

  1. Create the structure of the table
  2. Create the dynamic internal table with this structure
  3. Populate the dynamic table.

And then you have your dynamic internal table just like any other internal table.

Pre-requisites

Before we start to create a dynamic internal table, we must have an idea of field symbols and data references. If you know them, skip this session and go directly to dynamic table creation.

Field Symbols

Field symbols are like pointers in C. (Technically, they are not the same, using this analogy just to get the picture). They just point to fields. They are like place holders or pseudonyms or alias for other fields.

When you assign a field symbol to a variable, whatever you do to that field symbol, it will be instantly reflected in the variable it points to (or in ABAP Language, the variable it is assigned to)

Syntax

Declarations
-SYMBOLS: <fs1>.
Assignment

ASSIGN f TO <fs1>.

It is like two containers that are connected with a pipe. As long as the field symbol is assigned to the object, whatever you put in the field symbol, it will flow to the data object. And whatever is in the data object can be accessed through the field symbol.

Any changes you make in <fs1> will be instantly reflected in the data object f.

<fs1> = 10.

Now variable f will have the value 10.

It can point to any object determined at run time. And for the same reason, it can adopt to any data type or size at run time depending on the object it is pointing to.

Data References

Data references are used to create data objects dynamically.

Syntax.

Declaration.

DATA <dref> TYPE REF TO DATA.

Creation at run time

CREATE DATA <dref> TYPE <type>|LIKE <obj>.

Now the data type of the object <Dref> will be <type> or the data type of object <obj>

To access the contents of the data object to which a data reference is pointing, you must deference it. This is where we need field symbols.

ASSIGN <dref>->* TO <FS>.

Now whatever statements are performed on Field symbols, it will be reflected in the object dref that we created at run time. Accessing field symbol <Fs> is equivalent to accessing data reference object dref.

Ok. So now we are all set to create dynamic table.

Dynamic Table Creation

Let’s take an example to learn the concept.

Suppose I have an internal table IT_DEMO containing three columns – vendor name (vend), Month(month), Amount Due(amt).

VENDOR MONTH AMOUNT DUE
V100 Jan 100
V100 Feb 250
V200 Feb 216
V300 Feb 550
V200 Mar 200
V300 Mar 310
V100 Apr 145
V100 May 350
V200 May 600
V300 May 200
V400 May 800

I need to create something like a transpose for this table dynamically. The output should look like this.

VENDOR JAN13 FEB13 MAR13 APR13 MAY13
V100 100 250 145 350
V200 216 200 600
V300 550 310 200
V400 800

Step 1 – Create Structure.

We create structure using field catalog.

If you have used ALV, you must be familiar with field catalog and its fields.

Some of the components of field catalog structure is field name, table name, column text, output length. These are the attributes that can be defined for each field of the dynamic internal table we are creating.

Declare a structure of type lvc_s_fcat.

Declare an internal table of type lvc_t_fcat  (The line type of this internal table is  lvc_s_fcat).

Field Catalog Declaration.

gw_dyn_fcat       TYPE lvc_s_fcat,
gt_dyn_fcat         TYPE lvc_t_fcat.

** This would create structure Vendor Jan13 Feb13 Mar13 ....
DATA : gv_pos TYPE i.
DATA : fname TYPE string.

* Declaring the first column - vendor
gv_pos = gv_pos + 1.

gw_dyn_fcat-fieldname = 'VEND'.          “ Field Name
gw_dyn_fcat-outputlen = 5.                     “ Output Length
gw_dyn_fcat-tabname   = 'IT_DEMO'.    “ Internal Table Name
gw_dyn_fcat-coltext   = 'VENDOR'.        “ Header text for the column
gw_dyn_fcat-col_pos   = gv_pos.             “ Column position 
gw_dyn_fcat-key = 'X'.                            “ Key attribute is set for the field vend. 
APPEND gw_dyn_fcat TO gt_dyn_fcat.
clear gw_dyn_fcat.

 

*Loop through the internal table and creatinga column for every distinct month in the internal table
LOOP AT it_zdemo INTO wa_zdemo.
gv_pos = gv_pos + 1.
CONCATENATE wa_zdemo-month '13' INTO fname.
read table gt_dyn_fcat into gw_dyn_fcat with key fieldname = wa_zdemo-month.
if sy-subrc NE 0.
gw_dyn_fcat-fieldname = wa_zdemo-month.
gw_dyn_fcat-tabname   = 'IT_DEMO'.
gw_dyn_fcat-coltext   = fname.
gw_dyn_fcat-outputlen = 10.
gw_dyn_fcat-col_pos   = gv_pos.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
endif.
clear gw_dyn_fcat.
ENDLOOP.

Now gt_dyn_fcat contains the structure of the table.

Step 2 – Create Dynamic Table.

Dynamic internal tables can be created using method CREATE_DYNAMIC_TABLE in class CL_ALV_TABLE_CREATE.

Importing parameter is the field catalog created in step 1 and the exporting parameter is the dynamic table. The dynamic table must have been declared as dynamic data using data reference.

DATA : gt_dyn_table  TYPE REF TO data.,

gw_line          TYPE REF TO data,
gw_line1         TYPE REF TO data,

* Create a dynamic internal table with this structure.

CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
i_style_table             = 'X'
it_fieldcatalog           = gt_dyn_fcat
IMPORTING
ep_table                  = gt_dyn_table
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS                    = 2.

Now we have the dynamic table gt_dyn_table.  To access the data, we use field symbols.

We shall create two work areas gw_line and gw_line1 like line of gt_dyn_table. (or like line of <gfs_dyn_table> which is the field-symbol assigned to gt_dyn_table). The work area gw_line will be accessed by field-symbol <gfs_line> and gw_line1 will be accessed by field symbol <gfs_line1>.

IF sy-subrc EQ 0.
* Assign the new table to field symbol
ASSIGN gt_dyn_table->* TO <gfs_dyn_table>.
* Create dynamic work area for the dynamic table
CREATE DATA gw_line LIKE LINE OF <gfs_dyn_table>.
CREATE DATA gw_line1 LIKE LINE OF <gfs_dyn_table>.
ASSIGN gw_line->* TO <gfs_line>.
ASSIGN gw_line1->* TO <gfs_line1>.
ENDIF.

Note : Field symbols were declared previously with the following statement.

FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,
<gfs_dyn_table> TYPE STANDARD TABLE,
<fs1>.

Step 3 – Populating the dynamic table

Each cell in the dynamic table is accessed using field symbols. We use the field symbol <fs1> to point to each component of work area <gfs_line> (alias gw_line). The values are moved to the work area, component by component through this field symbol <fs1>.

LOOP AT it_zdemo INTO wa_zdemo.
* Avoid duplicate entries for key field VEND. 
READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY ('VEND') = wa_zdemo-vend.
IF sy-subrc = 0.
CONTINUE.
ENDIF.

* The component vendor of the workarea is assigned to <fs1>
ASSIGN COMPONENT 'VEND' OF STRUCTURE <gfs_line> TO <fs1>.

* The value for vendor in the current loop wa_zdemo-vend  flows to the work area through <fs1>
<fs1> = wa_zdemo-vend.
UNASSIGN <fs1>.
* Move the amount for that vendor for each month in the dynamic table. Each month in the dynamic table can be looped using the field catalog table.

LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
IF gw_dyn_fcat-fieldname = 'VEND'. “ Move amount only for month fields, not vendor
CONTINUE.
ENDIF.
READ TABLE it_zdemo WITH KEY vend = wa_zdemo-vend month = gw_dyn_fcat-fieldname INTO wa_zdemo1.
IF sy-subrc = 0.
ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO <fs1>.
<fs1> = wa_zdemo1-amt.
UNASSIGN <fs1>.
ENDIF.
clear : wa_zdemo1.
ENDLOOP.

* Append the dynamic work area to the dynamic table. 
APPEND <gfs_line> TO <gfs_dyn_table>.
CLEAR: <gfs_line>.
clear: wa_zdemo, wa_zdemo1.
ENDLOOP.

Now the dynamic table has been created and has been populated with the values based on the contents of the initial internal table.

Drawbacks of Dynamic Internal table

  • Programs with many dynamic internal tables are less readable.
  • They are less secure since errors cannot be detected by syntax check, but only at runtime
  • Performance is not as good as static internal table.

Given below is the complete code for the above program.

REPORT zdynamic_table.

*Author ; Susmitha Susan Thomas

TYPES : BEGIN OF gfirst_typ,
vend(6) TYPE c,
month(5) TYPE c,
amt TYPE i.
TYPES : END OF gfirst_typ.

DATA : it_zdemo TYPE TABLE OF gfirst_typ.
DATA : wa_zdemo LIKE LINE OF it_zdemo,
wa_zdemo1 LIKE LINE OF it_zdemo.

DATA : gv_pos TYPE i.
DATA : fname TYPE string.

* Dynamic Table Declarations


DATA : gt_dyn_table  TYPE REF TO data,
gw_line       TYPE REF TO data,
gw_line1       TYPE REF TO data,
gw_dyn_fcat         TYPE lvc_s_fcat,
gt_dyn_fcat         TYPE lvc_t_fcat.

* Field Symbols Declarations

FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,
<gfs_dyn_table> TYPE STANDARD TABLE,
<fs1>.

* Populate the initial input table. Usually this input table contents will be populated at run time, which raises the requirement of dynamic table. The table contents are filled here for illustration purpose.


wa_zdemo-vend = 'V100'.
wa_zdemo-month = 'JAN'.
wa_zdemo-amt = 100.
APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V100'.
wa_zdemo-month = 'FEB'.
wa_zdemo-amt = 200.
APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V200'.
wa_zdemo-month = 'FEB'.
wa_zdemo-amt = 200.
APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V300'.
wa_zdemo-month = 'FEB'.
wa_zdemo-amt = 150.
APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V200'.
wa_zdemo-month = 'MAR'.
wa_zdemo-amt = 250.
APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V300'.
wa_zdemo-month = 'MAR'.
wa_zdemo-amt = 300.
APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V100'.
wa_zdemo-month = 'APR'.
wa_zdemo-amt = 200.
APPEND wa_zdemo TO it_zdemo.



wa_zdemo-vend = 'V100'.
wa_zdemo-month = 'MAY'.
wa_zdemo-amt = 100.
APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V200'.
wa_zdemo-month = 'MAY'.
wa_zdemo-amt = 50.
APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V300'.
wa_zdemo-month = 'MAY'.
wa_zdemo-amt = 125.
APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V400'.
wa_zdemo-month = 'MAY'.
wa_zdemo-amt = 475.
APPEND wa_zdemo TO it_zdemo.



Write : / 'Initial Internal Table'.

WRITE :/.
write :/(6) 'Vendor'.
write : (12) 'Month' .
write : (3) 'Amt' .
LOOP AT it_zdemo INTO wa_zdemo.
WRITE :/ wa_zdemo-vend, wa_zdemo-month, wa_zdemo-amt.
ENDLOOP.



** This would create structure Vendor Jan13 Feb13 Mar13 etc ....


gv_pos = gv_pos + 1.
gw_dyn_fcat-fieldname = 'VEND'.
gw_dyn_fcat-outputlen = 5.
gw_dyn_fcat-tabname   = 'IT_DEMO'.
gw_dyn_fcat-coltext   = 'VENDOR'.
gw_dyn_fcat-col_pos   = gv_pos.
gw_dyn_fcat-key = 'X'.
gw_dyn_fcat-key_sel = 'X'.
APPEND gw_dyn_fcat TO gt_dyn_fcat.

clear gw_dyn_fcat.

* Loop through the internal table creating a column for every distinct month in the internal table

LOOP AT it_zdemo INTO wa_zdemo.
gv_pos = gv_pos + 1.
CONCATENATE wa_zdemo-month '13' INTO fname.
read table gt_dyn_fcat into gw_dyn_fcat with key fieldname = wa_zdemo-month.
if sy-subrc NE 0.
gw_dyn_fcat-fieldname = wa_zdemo-month.
gw_dyn_fcat-tabname   = 'IT_DEMO'.
gw_dyn_fcat-coltext   = fname.
gw_dyn_fcat-outputlen = 10.
gw_dyn_fcat-col_pos   = gv_pos.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
endif.
clear gw_dyn_fcat.
ENDLOOP.

** Create a dynamic internal table with this structure.

CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
i_style_table             = 'X'
it_fieldcatalog           = gt_dyn_fcat
IMPORTING
ep_table                  = gt_dyn_table
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS                    = 2.

IF sy-subrc EQ 0.
* Assign the new table to field symbol
ASSIGN gt_dyn_table->* TO <gfs_dyn_table>.
* Create dynamic work area for the dynamic table
CREATE DATA gw_line LIKE LINE OF <gfs_dyn_table>.
CREATE DATA gw_line1 LIKE LINE OF <gfs_dyn_table>.
ASSIGN gw_line->* TO <gfs_line>.
ASSIGN gw_line1->* TO <gfs_line1>.
ENDIF.




* Populate the dynamic table

LOOP AT it_zdemo INTO wa_zdemo.


* Avoid duplicate entries for key field PART.
READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY ('VEND') = wa_zdemo-vend.
IF sy-subrc = 0.
CONTINUE.
ENDIF.


ASSIGN COMPONENT 'VEND' OF STRUCTURE <gfs_line> TO <fs1>.
<fs1> = wa_zdemo-vend.
UNASSIGN <fs1>.


LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
IF gw_dyn_fcat-fieldname = 'VEND'.
CONTINUE.
ENDIF.
READ TABLE it_zdemo WITH KEY vend = wa_zdemo-vend month = gw_dyn_fcat-fieldname INTO wa_zdemo1.
IF sy-subrc = 0.
ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO <fs1>.
<fs1> = wa_zdemo1-amt.
UNASSIGN <fs1>.
ENDIF.
clear : wa_zdemo1.
ENDLOOP.
APPEND <gfs_line> TO <gfs_dyn_table>.
CLEAR: <gfs_line>.
clear: wa_zdemo, wa_zdemo1.
ENDLOOP.

WRITE :/.

Write : / 'Dynamic Internal Table'.
WRITE :/.
LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
WRITE (10) : gw_dyn_fcat-coltext.
ENDLOOP.
WRITE :/.
LOOP AT <gfs_dyn_table> INTO <gfs_line>.
LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO <fs1>.
WRITE : <fs1>.
ENDLOOP.
WRITE :/      .
ENDLOOP.

Output

Just a minor addition to the above program. Now if you want to display this as an ALV grid, you need to create another field catalog. Since the field catalog created above is not compatible with the field catalog passed as parameter for ALV display.

data : gw_alv_fieldcat     type slis_fieldcat_alv,
gt_alv_fieldcat     type slis_t_fieldcat_alv.

data:    lv_pos type i.


loop at gt_dyn_fcat into gw_dyn_fcat.
lv_pos = lv_pos + 1.
gw_alv_fieldcat-fieldname     = gw_dyn_fcat-fieldname.
gw_alv_fieldcat-tabname       = gw_dyn_fcat-tabname.
gw_alv_fieldcat-seltext_l     = gw_dyn_fcat-coltext.
gw_alv_fieldcat-outputlen     = gw_dyn_fcat-outputlen.
gw_alv_fieldcat-col_pos       = lv_pos.
gw_alv_fieldcat-do_sum        = gw_dyn_fcat-do_sum.
gw_alv_fieldcat-emphasize     = gw_dyn_fcat-emphasize.
gw_alv_fieldcat-key           = gw_dyn_fcat-key.
gw_alv_fieldcat-no_out        = gw_dyn_fcat-no_out.
append gw_alv_fieldcat to gt_alv_fieldcat.
endloop.

call function 'REUSE_ALV_GRID_DISPLAY'
exporting
i_callback_program = sy-repid
it_fieldcat        = gt_alv_fieldcat
i_default          = 'X'
i_save             = 'A'
tables
t_outtab           = <gfs_dyn_table>.

Assigned Tags

      52 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Yuvaraj Shanmugam
      Yuvaraj Shanmugam

      Hi Susmitha,

      Well explained and illustrated. I enjoyed reading it.

      But I believe dynamic table creation and manipulation is more structured when you apply RTTS classes ( cl_abap_*descr)  instead of cl_alv_table_create=>create_dynamic_table.

      Creating dynamic tables are easier and faster with the use of a reference to the class  cl_abap_tabledescr and a single statement like

      CREATE DATA me->o_status_table TYPE HANDLE o_table.

      What do you think?

      Author's profile photo Former Member
      Former Member

      Thanks for the explanation.  I've never had to create a dynamic table yet so I'll be sure to refer here when I do.  I have two things to add, though.

      First, field symbols are not like pointers in C since you can't actually see the address they hold.  Field-symbols are more like permanently dereferenced pointers.  Data references are actual pointers.

      And I believe after the 1st method call in step 2, you wrote gt_dyn_fcat a few times where you wanted to write gt_dyn_table.

      Overall the wording was very easy to read.  Thanks for sharing!

      Author's profile photo Susmitha Susan Thomas
      Susmitha Susan Thomas
      Blog Post Author

      Yes, you are right in that pointers are different from C when you go to the technicalities like the address they hold. Just used that analogy to get the picture of field-symbols of pointing to another variable, and the closest I could get was pointers. Hence used it. I have added your suggestion to avoid any confusion. Thank you.

      Thanks also for pointing out the slip in table name . I have corrected the same.

      Author's profile photo Naimesh Patel
      Naimesh Patel

      Hello Susmitha,

      Thanks for the document.

      Instead of using the method create_dynamic_table of class cl_alv_table_create, you should consider using the Runtime Type Services (RTTS). One of the example is: Create Dynamic Internal Table using RTTS. There are few advantages of using the RTTS over method call create_dynamic_table.

      • Internally cl_alv_table_create=>create_dynamic_table, generates a subroutine pool with the provided fields, creates a subroutine to pass back the data reference and calls the subroutine to get the data reference back from the generated program to calling program. Whereas RTTS uses mixture of SYSTEM-CALL and KERNEL methods to generate the TYPES. So, they are definitly faster.
      • create_dynamc_table - Here you need to supply the data type, length etc to generate the type for a field. Whereas Using RTTS, you can use the predefined data types
      • RTTS is based on OO model and helps to describe and generate data types/data objects

      Thanks,

      Naimesh Patel

      Author's profile photo Susmitha Susan Thomas
      Susmitha Susan Thomas
      Blog Post Author

      Wow, so many comments for RTTS. It should be really good! 🙂 Read your document. Interesting.  RTTS is a relativiely new concept for me. Will definitely explore it.

      Thanks Nimesh and  Yuva Raj for the suggestions.

      Author's profile photo Former Member
      Former Member

      RTTS was launched way back in ABAP release 640. Three releases later, but people are still going crazy over cl_alv_table_create=>create_dynamic_table( ). 🙁

      Author's profile photo Matthew Billingham
      Matthew Billingham

      RTTS doesn't have the restriction on the number of tables you can have dynamically. But still the old solution is promoted.

      Author's profile photo Rodrigo Paisante
      Rodrigo Paisante

      Nice job!

      I would like to share a problem we had months ago, could be the problem also someone:

      We had the need to create tables and structures dynamics, we used the method cl_alv_table_create => create_dynamic_table. However, the volume of dynamics information created in the same program was too big ... and the exception occurred, i do not remember but I believe the limit is 49 runs.

      Then we changed to the method cl_abap_tabledescr => create to create tables and cl_abap_structdescr => create to create structures dynamically.

      Best regards.

      Author's profile photo Kesavadas Thekkillath
      Kesavadas Thekkillath

      Hi,

      Thanks for the post.

      This is one of my favourite post http://scn.sap.com/thread/1725739 by our RTTS Guru Marcin Pciak

      The subroutine pool has a generation limit of 36 which is also applied when you call

      cl_alv_table_create=> create_dynamic_table



      Author's profile photo Former Member
      Former Member

      Good explanation Had a requirement similar to this some time back. Was able to solve with help of Sandra Rossi and Marcin Pciak. ( https://scn.sap.com/thread/1969335 ).

      There take on pros and cons of using CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

      and other classes of RTTC/RTTS was of great help.

      Author's profile photo Former Member
      Former Member

      Hello Susmitha ,

      Thank you for explanation. Very good .

      how would you proceed if you would have to sum values / vendor . Let's say one extra column =Total . (Vendor / Month 1 / 2 / 3../ Total )

      Best regards

      octav

      Author's profile photo Susmitha Susan Thomas
      Susmitha Susan Thomas
      Blog Post Author

      Just add that field to the structure.

      1. For the structure of table : When creating the structure, you first add vendor column, then enter the loop to create columns for the months and after the loop, just add one more column for total.

      gv_pos = gv_pos + 1.
      gw_dyn_fcat-fieldname = 'VEND'.
      gw_dyn_fcat-outputlen = 5.
      gw_dyn_fcat-tabname   = 'IT_DEMO'.
      gw_dyn_fcat-coltext   = 'VENDOR'.
      gw_dyn_fcat-col_pos   = gv_pos.
      gw_dyn_fcat-key = 'X'.
      *gw_dyn_fcat-key_sel = 'X'.
      APPEND gw_dyn_fcat TO gt_dyn_fcat.
      clear gw_dyn_fcat.
      *
      LOOP AT it_zdemo INTO wa_zdemo.
         gv_pos = gv_pos + 1.
         CONCATENATE wa_zdemo-month '13' INTO fname.
         read table gt_dyn_fcat into gw_dyn_fcat with key fieldname = wa_zdemo-month.
         if sy-subrc NE 0.
           gw_dyn_fcat-fieldname = wa_zdemo-month.
           gw_dyn_fcat-tabname   = 'IT_DEMO'.
           gw_dyn_fcat-coltext   = fname.
           gw_dyn_fcat-outputlen = 10.
           gw_dyn_fcat-col_pos   = gv_pos.
           APPEND gw_dyn_fcat TO gt_dyn_fcat.
         endif.
          clear gw_dyn_fcat.
      ENDLOOP.

      gv_pos = gv_pos + 1.
      gw_dyn_fcat-fieldname = 'TOTAL'.
      gw_dyn_fcat-outputlen = 7.
      gw_dyn_fcat-tabname   = 'IT_DEMO'.
      gw_dyn_fcat-coltext   = 'TOTAL'.
      gw_dyn_fcat-col_pos   = gv_pos.
      gw_dyn_fcat-key = 'X'.
      *gw_dyn_fcat-key_sel = 'X'.
      APPEND gw_dyn_fcat TO gt_dyn_fcat.

      2. To populate the total value - While populating other fields, keep a track on the total also. At the end, for each vendor, add the total also to that row.

      data : total type i.
      ASSIGN COMPONENT 'VEND' OF STRUCTURE <gfs_line> TO <fs1>.
         <fs1> = wa_zdemo-vend.
         UNASSIGN <fs1>.
         LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
           IF gw_dyn_fcat-fieldname = 'VEND'.
             CONTINUE.
           ENDIF.
           READ TABLE it_zdemo WITH KEY vend = wa_zdemo-vend month = gw_dyn_fcat-fieldname INTO wa_zdemo1.
           IF sy-subrc = 0.
             ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO <fs1>.
             <fs1> = wa_zdemo1-amt.
             total = wa_zdemo1-amt + total.
             UNASSIGN <fs1>.
           ENDIF.
           clear : wa_zdemo1.
         ENDLOOP.
        ASSIGN COMPONENT 'TOTAL' of STRUCTURE <gfs_line> to <fs1>.
        <fs1> = total.
         UNASSIGN <fs1>.
        clear total.
         APPEND <gfs_line> TO <gfs_dyn_table>.

      Rest everything is the same.

      Author's profile photo Amit Jaganade
      Amit Jaganade

      Hi Susmitha,

      Thank you for such useful information.

      I have used this code in my report but i am facing some issue that do_sum is not working and i have to show total and sub total for all respective coloumns in ALV grid.

      Thanks & regards,

      Amit

      Author's profile photo Susmitha Susan Thomas
      Susmitha Susan Thomas
      Blog Post Author

      Thanks Amit.

      While creating the field catalog for dynamic table, just set the do_sum to 'X' and inttype = 'I' for the columns that needs to be totalled (make sure the inttype is also set in the field catalog for the respective columns

      While creating field catalog,

      gw_dyn_fcat-fieldname = wa_zdemo-month.

      gw_dyn_fcat-tabname   = 'IT_DEMO'.
      gw_dyn_fcat-coltext   = fname.
      gw_dyn_fcat-outputlen = 10.

      gw_dyn_fcat-DO_SUM = 'X'.
      gw_dyn_fcat-inttype = 'I'.

      gw_dyn_fcat-col_pos   = gv_pos.
      APPEND gw_dyn_fcat TO gt_dyn_fcat.

      And finally for the ALV field catalog, this should also be moved to that field catalog.

      Author's profile photo Amit Jaganade
      Amit Jaganade

      Done ! Done ! Done !...

      Thank you very much Susmitha... I would like to mention some point here that you had given statment gw_dyn_fcat-inttype = 'I'. to make sum of figures but i wanted to make sum of figures with type p so i used gw_dyn_fcat-inttype = 'CURR'. and it works.

      It may help you and others.

      Thank you once again for instant reply..

      Regards,

      Amit

      Author's profile photo Pankaj Jagzap
      Pankaj Jagzap

      Hi ,

      I have an internal tabel in place of 5 coloumns and have created an alv list for it.

      CC          A  B   C  D

      1111       2   3   9   1

      1112       7   8   9   1

      1113       9   1   2   1

      1114       0   2   1   1

      But the requirement is that a  a new coloumn addidtion facility must be availaible , since E , F , etc any number of cloumns might be needed i near future, add hence total mentioned below must be displayed accordingly  . A B C D are present in one table .

      Author's profile photo Susmitha Susan Thomas
      Susmitha Susan Thomas
      Blog Post Author

      Could you explain your requirement again? It is not clear to me..

      Author's profile photo Pankaj Jagzap
      Pankaj Jagzap

      Sorry for delay in replying for clarification :

      There are  5 columns already in a report .

      but the req is that there should be a parameter on the selection screen to add the coloumns dyanamicaly. So that whenever user wants to add the coloumn he can add it .

      The scenario is that : for CC company  code 1111 there s count of 2 A's 3 B's and so on.

      So the newly added coloumn must also  have its corresponding count  for the company code

      Author's profile photo Ramesh Narayanasamy
      Ramesh Narayanasamy

      HI Susmitha,

      Thanks for your Post. It is very useful to me for doing my development.

      I need some more requirement in my development .

      My requirement is to add one more row  after the end of total, I have done doing calculation in final structure itself but i dono how to display the same after the row total at bottom.

      Please Suggest your input for this issue.

      Thanks,

      Ramesh

      Author's profile photo Saurabh Singh
      Saurabh Singh

      Hi Susmitha, thanks a lot for this dynamic post. It helped me a lot. I have used the do_sum option for showing zeros in the output table where no value exist for that particular date, but i want to hide the summation row at the end of table. Could you please suggest a possible to hide the last row of the output table.

      I hope to hear a positive solution soon.

      Thanks again,

      Saurabh.

      Author's profile photo Former Member
      Former Member

      Hi Susmitha,

      Thanku so much for this valuable post.

      I have same requirement. i have created successfully dynamic internal table according to your process.

      now my requirement is to move all data from dynamic internal table to my  internal table.so that i can loop and i can do some operation on that values.

      how it possible please get me back.

      Thanks,

      Author's profile photo Matthew Billingham
      Matthew Billingham

      This question has been covered frequently. Why not search the site for an answer?

      Author's profile photo Former Member
      Former Member

      Please post some url.

      i am unable to find same.

      Thanks,

      Author's profile photo Former Member
      Former Member

      I guess Matthew will post lmgtfy url.

      Author's profile photo Matthew Billingham
      Matthew Billingham

      Well, a kind of meta-lmgtfy.

      Author's profile photo Former Member
      Former Member

      🙂 You really do teach a man how to fish.

      Author's profile photo Former Member
      Former Member

      Dear Subeesh,

      Use "Modify" statement instead i does both jobs.

      Author's profile photo Former Member
      Former Member

      Hi Susmitha,

         I am having same requirement. Like Raw Material I have to display in column w.r.t FG.

      And this one very useful to me for understand dynamic report creation.

      Thank you very much.

      Author's profile photo Ngoc Linh Nguyen
      Ngoc Linh Nguyen

      hi Susmitha,

      Thank you for the excelllent illustration and explanation of the example. Just what I need 🙂

      Thanks,

      Author's profile photo Former Member
      Former Member

      Super, this is want i was looking for.

      Author's profile photo Vasanth Mohan
      Vasanth Mohan

      Hi Susmitha,

      This is an awesome illustration perfect to the current development i am doing...but i face an issue.. The row totals do not appear for me.

      This is my field catalog

      Capture.PNG

      But my output appears without the total.

      Capture.PNG

      Any thoughts as where am i wrong?

      Author's profile photo Subeesh Kannottil
      Subeesh Kannottil

      Hi,

      Is this the field catalog you are passing to create the dynamic internal table ? Then this only creates the dynamic internal table .When passing to the resue_alv_grid display you need to pass the field catalog to the function module .It will work fine

      Thanks and Regards,

      Subeesh Kannottil

      Author's profile photo Vasanth Mohan
      Vasanth Mohan

      Hi,

      The field catalog is passed to the ALV_GRID_DISPLAY...yet no output

      Author's profile photo Susmitha Susan Thomas
      Susmitha Susan Thomas
      Blog Post Author

      Hi Ken,

      In the field catalog for the dynamic table, set the inttype to I to get the sum.

      You will get the totals.

      gw_dyn_fcat-fieldname = wa_zdemo-month.

      gw_dyn_fcat-tabname   = 'IT_DEMO'.
      gw_dyn_fcat-coltext   = fname.
      gw_dyn_fcat-outputlen = 10.
      gw_dyn_fcat-do_sum = 'X'.
      gw_dyn_fcat-inttype = 'I'.

      * You could also use 'P' - gw_dyn_fcat-inttype = 'P'.
      gw_dyn_fcat-col_pos   = gv_pos.
      APPEND gw_dyn_fcat TO gt_dyn_fcat.



      If you want to give 'CURR', you must pass reference field and reference table also in the field catalog.



      For the field catalog to create dynamic table,

         gw_dyn_fcat-do_sum = 'X'.
           gw_dyn_fcat-inttype = 'CURR'.
           gw_dyn_fcat-ref_field = 'DMBTR'.
           gw_dyn_fcat-ref_table = 'MSEG'.


      For the field catalog for ALV display.

        

        gw_alv_fieldcat-do_sum        = gw_dyn_fcat-do_sum.

         gw_alv_fieldcat-inttype        = gw_dyn_fcat-inttype.

         gw_alv_fieldcat-ref_fieldname = gw_dyn_fcat-ref_field.

         gw_alv_fieldcat-ref_tabname   = gw_dyn_fcat-ref_table .


      BR,

      Susmitha

      Author's profile photo Former Member
      Former Member

      Dear Sushmita ma'am,

      Thanks a ton for such a nice explanation.

      I have one more requirement in addition to the stated requirement i.e. I have three fixed columns before the Month columns viz.

      1. VendorCode or Document type in same column
      2.Vendor Description

      3.Purchasing Document Number

      and have to sum up amounts in months row wise and Purchs. Docu or Vendor wise depending on which one is not blank.

      like

      Type

      Vendor Code/ Doc Type

      Vendor Description

      Purchasing Doc No

      April 2014

      May 2014

      Total

      Unsettle amount

      1. 348433.9

      15,450.00

      3,63,883.90

      Vendor

      189616

      PURSHOTTAM TRANSPORT

      9000

      111750

      1,20,750.00

      Vendor

      194393

      PERFECT CONSTRUCTION

      1. 214734.33

      2,14,734.33

      Vendor

      203252

      sanpada ELECTRICAL WORKS

      1. 229943.95

      2,29,943.95

      Vendor

      2500639

      PAB Upkeep Services

      1. 36708.01

      -1494.39

      35,213.62

      Vendor

      2505139

      Neha Enterprise

      1. 53626.04

      53,626.04

      Vendor

      2520221

      Vijay Electricals

      1. 7931.25

      7,931.25

      Doc Type

      N21

      -3665684.21

      1. 8867.64

      -36,56,816.57

      Doc Type

      SA1

      1. 372009.95

      3,72,009.95

      Doc Type

      SL1

      13057000

      1073000

      1,41,30,000.00

      pls tell what changes to be made in the code after call method

      Author's profile photo Former Member
      Former Member

      Hi Susmitha,

      Thank you for the nice document. Nice explaination and illustrations..! 🙂

      Author's profile photo Former Member
      Former Member

      Very nice explanation.

      Author's profile photo Former Member
      Former Member

      Very well written and explained. Thanks a lot Susmitha for sharing your knowledge.

      Author's profile photo Thierry RICARD
      Thierry RICARD

      Super

      Author's profile photo Former Member
      Former Member

      Hello Susmitha,

      It helps me a lot in one of my requirement.


      Thank you very much for posting such a nice useful document.


      Regards,

      Rakesh

      Author's profile photo Former Member
      Former Member

      Hi Susmitha,

      Thank you, it help me a lot.

      whould you explain, how to change code if i change your data like this?

      cause in feb we have 2 order with different date.

      Thanks

      VENDOR

      MONTH

      AMOUNT DUE

      V100

      Jan

      100

      V100

      Feb

      250

      V100

      Feb

      216

      V300

      Feb

      550

      V200

      Mar

      200

      V300

      Mar

      310

      V100

      Apr

      145

      V100

      May

      350

      V200

      May

      600

      V300

      May

      200

      V400

      May

      800

      I need to create something like a transpose for this table dynamically. The output should look like this.

      VENDOR

      JAN13

      FEB13

      MAR13

      APR13

      MAY13

      V100

      100

      466

      145

      350

      V200

      200

      600

      V300

      550

      310

      200

      V400

      800

      Author's profile photo Former Member
      Former Member

      Dear Susmitha, your wiki was indeed quite helpful. I had a request from business to get partner fields from quality notifications available on QM11 standard report and the instructions provided helped our development team out to achieve a great end result. Thank you!

      Author's profile photo Former Member
      Former Member

      Dear Susan,

      This is great. thank you for your post it help me so much. Since my company want a report in this format. I have develop a report where the column is dynamic.

      Best Regards,

      Weldin

      Author's profile photo Former Member
      Former Member

      Hi Susmitha,

      Good work, Thanks for explained steps.

      Author's profile photo Kali Charan
      Kali Charan

      Good Document.

      Author's profile photo vaibhav sonawane
      vaibhav sonawane

      Thanks Mr.peter.

      Author's profile photo Former Member
      Former Member

      Hi Susmitha,

      I am learning field symbols, but can not understand this code:

      "The component vendor of the workarea is assigned to <fs1>

      ASSIGN COMPONENT 'VEND' OF STRUCTURE <gfs_line> to <fs1>

      *The value for the vendor in the loop wa_zdemo-vend flows to the work area through <fs1>

      UNASSIGN <fs1>."

      Could you please explain it more ?

      Author's profile photo Former Member
      Former Member

      Hi, Susmitha. Thanks for this post is very usefull for explained steps.

      Author's profile photo Former Member
      Former Member

      Hello Susmitha ,

      Thank you for that amazing explanation.

      How would you proceed if you would have to add a row for TOTAL, not only displaying the totals int the ALV, but an additional row with totals.  Let’s say one extra row = Total, for example:

       

      Best regards

      megara

      Author's profile photo Former Member
      Former Member

      Hi friends;

      Thank you, it help me a lot.

      Help me .?

      This is my code :

      GW_DYN_FCATDO_SUM  ‘X’.
      GW_DYN_FCATINTTYPE  ‘CURR’.
      GW_DYN_FCATREF_FIELD  ‘KLMENG’.
      GW_DYN_FCATREF_TABLE  ‘VBAP’.

             COLLECT  <GFS_LINE> INTO <GFS_DYN_TABLE>.

      This is Eror :

      Author's profile photo Alexander Adam
      Alexander Adam

      If you do not want to use CL_ALV_TABLE_CREATE.

      Here is another way to create a dynamic table of a known structure

      DATA lr_table TYPE REF TO data.
      DATA lr_structdescr TYPE REF TO cl_abap_structdescr.
      lr_structdescr ?= cl_abap_typedescr=>describe_by_data_ref( ref #( ls_structure ) ).
      DATA lr_tabledescr TYPE REF TO cl_abap_tabledescr.
      lr_tabledescr ?= cl_abap_tabledescr=>create( p_line_type = lr_structdescr ).

      CREATE DATA lr_table TYPE HANDLE lr_tabledescr.
      ASSIGN lr_table->* TO FIELD-SYMBOL(<ft_table>).

       

      Author's profile photo Kenneth Moore
      Kenneth Moore

      Great BLOG!  I was able to follow it as a guide and create my own report.