Академический Документы
Профессиональный Документы
Культура Документы
DATE: 11.06.2013
_____________________________________________________________________________________
___________________________
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.
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.
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
FIELD-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.
Data References
Syntax.
Declaration.
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.
Suppose I have an internal table IT_DEMO containing three columns – vendor name (vend),
Month(month), Amount Due(amt).
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
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 an internal table of type lvc_t_fcat (The line type of this internal table is lvc_s_fcat).
*Loop through the internal table and 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.
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>.
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>.
* 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.
Now the dynamic table has been created and has been populated with the values based on the
contents of the initial internal table.
REPORT zdynamic_table.
FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,
<gfs_dyn_table> TYPE STANDARD TABLE,
<fs1>.
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 :/.
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.
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.
WRITE :/.
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.