Dynamic Internal Table
This Tutorial details about creation of dynamic internal
table with different scenarios
Internal Tables are local tables within a program containing a series of lines having same
data type. ABAPTM Open SQL allows single field, range of fields,
entire database table or view into an internal table.
In technical terms
Internal table is a dynamic sequential dataset in which all records have the
same data structure and a key.
A
static internal table can be declared in
an ABAPTM program initially, when the structure of the internal
table is fixed and known to the user.
Dynamic internal table is an extension to internal table
concept, used when the number of fields is not known at the design time or
until the compile time.
Scenario 1:
Display
the table of contents in grid format using the dynamic internal table.
Creating
Dynamic internal table
PARAMETERS: p_table (10)
TYPE C.
DATA:
w_tabname TYPE
w_tabname,
W_dref TYPE REF TO
data,
W_grid TYPE REF TO cl_gui_alv_grid.
FIELD-SYMBOLS:
<t_itab> TYPE ANY TABLE.
w_tabname =
p_table.
CREATE DATA
w_dref TYPE TABLE OF (w_tabname).
ASSIGN
w_dref->* TO <t_itab>.
Populating Dynamic internal
table
SELECT *
FROM (w_tabname) UP TO 20 ROWS
INTO TABLE <t_itab>.
Displaying dynamic internal table
using Grid.
CREATE OBJECT
w_grid
EXPORTING i_parent = cl_gui_container=>screen0.
CALL METHOD
w_grid->set_table_for_first_display
EXPORTING
i_structure_name = w_tabname
CHANGING
it_outtab = <t_itab>.
CALL SCREEN
100.
Scenario 2:
Create a dynamic internal table with
the specified number of columns.
Creating Dynamic internal table
TYPE-POOLS: slis.
FIELD-SYMBOLS:
<t_dyntable> TYPE STANDARD TABLE, “ Dynamic internal table name
<fs_dyntable>,
“Field symbol to create work area
<fs_fldval> type
any.
“Field symbol to assign values
PARAMETERS: p_cols (5)
TYPE
c.
“Input number of columns
DATA:
t_newtable TYPE REF TO data,
T_newline TYPE REF TO data,
T_fldcat TYPE slis_t_fldcat_alv,
T_fldcat TYPE lvc_t_fcat,
Wa_it_fldcat TYPE lvc_s_fcat,
wa_colno (2) TYPE n,
wa_flname (5) TYPE c.
* Create fields.
DO p_cols
TIMES.
CLEAR wa_it_fldcat.
move sy-index to wa_colno.
concatenate 'COL'
wa_colno
into wa_flname.
wa_it_fldcat-fieldname = wa_flname.
wa_it_fldcat-datatype = 'CHAR'.
wa_it_fldcat-intlen = 10.
APPEND wa_it_fldcat TO t_fldcat.
ENDDO.
* Create dynamic
internal table and assign to FS
CALL METHOD
cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = t_fldcat
IMPORTING
ep_table = t_newtable.
ASSIGN
t_newtable->* TO <t_dyntable>.
* Create dynamic work
area and assign to FS
CREATE DATA t_newline
LIKE LINE OF <t_dyntable>.
ASSIGN
t_newline->* TO <fs_dyntable>.
Populating
Dynamic internal table
DATA: fieldname(20)
TYPE c.
DATA:
fieldvalue(10) TYPE c.
DATA: index(3)
TYPE c.
DO p_cols
TIMES.
index = sy-index.
MOVE sy-index TO wa_colno.
CONCATENATE 'COL'
wa_colno
INTO wa_flname.
* Set up fieldvalue
CONCATENATE 'VALUE' index INTO
fieldvalue.
CONDENSE fieldvalue NO-GAPS.
ASSIGN COMPONENT wa_flname
OF STRUCTURE <fs_dyntable> TO <fs_fldval>.
<fs_fldval> = fieldvalue.
ENDDO.
* Append to the
dynamic internal table
APPEND
<fs_dyntable> TO <t_dyntable>.
Displaying
dynamic internal table using Grid.
DATA: wa_cat LIKE LINE
OF fs_fldcat.
DO p_cols
TIMES.
CLEAR wa_cat.
MOVE sy-index TO wa_colno.
CONCATENATE 'COL'
wa_colno
INTO wa_flname.
wa_cat-fieldname = wa_flname.
wa_cat-seltext_s = wa_flname.
wa_cat-outputlen
= '10'.
APPEND wa_cat TO fs_fldcat.
ENDDO.
* Call ABAP List
Viewer (ALV)
CALL FUNCTION
'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = fs_fldcat
TABLES
t_outtab = <t_dyntable>.
What Are Different
Types Of Internal Tables and Their Usage
Standard Internal Tables
Standard tables have a linear
index. You can access them using either the index or the key. If you use the
key, the response time is in linear relationship to the number of table
entries. The key of a standard table is always non-unique, and you may not include
any specification for the uniqueness in the table definition.
This table type is particularly
appropriate if you want to address individual table entries using the index.
This is the quickest way to access table entries. To fill a standard table,
append lines using the (APPEND) statement. You should read, modify and delete
lines by referring to the index (INDEX option with the relevant ABAP
command). The response time for accessing a standard table is in linear
relation to the number of table entries. If you need to use key access,
standard tables are appropriate if you can fill and process the table in
separate steps. For example, you can fill a standard table by appending
records and then sort it. If you then use key access with the binary search option
(BINARY), the response time is in logarithmic relation to
the number of table entries.
Sorted Internal Tables
Sorted tables are always saved
correctly sorted by key. They also have a linear key, and, like standard
tables, you can access them using either the table index or the key. When you
use the key, the response time is in logarithmic relationship to the number
of table entries, since the system uses a binary search. The key of a sorted
table can be either unique, or non-unique, and you must specify either UNIQUE
or NON-UNIQUE in the table definition. Standard tables and sorted
tables both belong to the generic group index tables.
This table type is particularly
suitable if you want the table to be sorted while you are still adding
entries to it. You fill the table using the (INSERT) statement, according to
the sort sequence defined in the table key. Table entries that do not fit are
recognised before they are inserted. The response time for access using the
key is in logarithmic relation to the number of
table entries, since the system automatically uses a binary search. Sorted tables are appropriate for partially sequential processing in a LOOP, as long as the WHERE condition contains the beginning of the table key.
Hashed Internal Tables
Hashes tables have no internal
linear index. You can only access hashed tables by specifying the key. The
response time is constant, regardless of the number of table entries, since
the search uses a hash algorithm. The key of a hashed table must be unique,
and you must specify UNIQUE in the table definition.
This table type is particularly
suitable if you want mainly to use key access for table entries. You cannot
access hashed tables using the index. When you use key access, the response
time remains constant, regardless of the number of table entries. As with
database tables, the key of a hashed table is always unique. Hashed tables
are therefore a useful way of constructing and
using internal tables that are similar to database tables.
Index Tables
Index table is only used to
specify the type of generic parameters in a FORM or FUNCTION. That means that
you can't create a table of type INDEX.
Internal tables are not DB tables.
Standard and Sorted tables in combined are basically called as Index tables and
there nothing else. Here is the hierarchy
ANY TABLE
| ------------------------------------ | | Index Tables Hashed Table | ------------------------------------ | | Standard Table Sorted Table |
No comments:
Post a Comment