Create dynamic internal table

First, I will introduce some usages of abap’s reference(pointer) variable
1. Define reference variable
Statement: DATA f TYPE REF TO DATA.
Effect: Declares the data object f as a reference variable.
Reference variables contain references(pointers) to data objects.

2. Instantiating reference variable
Statement: CREATE DATA dref TYPE type.
Effect: Creates a new data object (field) with the specified type.
Once it has been created, the data object contains the appropriate initial value for its type.
If you want to access the contents of the data object,you must use ASSIGN to assign it to a field symbol.

3. Access content of reference variable
Statement: ASSIGN dref->* TO <fs>.
Effect: The field to which the data reference dref is pointing is assigned to the field symbol <fs>.
At runtime, the field symbol <fs> points to the same contents as the data reference dref.

Now we have understand abap’s reference variable, creating dynamic table is easy, just a class’s method call.
data: dy_table type ref to data.
call method cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = ifc
IMPORTING
ep_table = dy_table.

In the exporting parameters, it_fieldcatalog is a structure that describe target internal table.
if you are familiar with alv list function, you will find that it is same as parameter it_fieldcatalog in function module REUSE_ALV_LIST_DISPLAY.
The importing parameter dy_table is a reference to generated internal table, you can use it like this:

field-symbols: <dyn_table>type standard table.
assign dy_table->* to <dyn_table>.

Because <dyn_table>is defined as a generic variable, we can’t use it like <dyn_table>-fieldname.
Instead, we must use assign statement to access fields in <dyn_table>, here is a sample:

field-symbols: <dyn_wa>,
               <dyn_field>.
data: dy_line type ref to data.

CREATE DATA dy_line LIKE LINE OF <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
ASSIGN COMPONENT 1 OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field>= ‘XXX’.
APPEND <dyn_wa> TO <dyn_table>.

One Response

Subscribe to comments with RSS.