).
```
- Dynamic specification of clauses in ABAP SQL statements
``` abap
"Dynamic SELECT list
DATA(select_list) = `CARRID, CONNID, COUNTRYFR, COUNTRYTO`.
SELECT (select_list)
FROM zdemo_abap_fli
INTO TABLE ...
"Dynamic FROM clause
DATA(table) = `ZDEMO_ABAP_FLI`.
SELECT *
FROM (table)
INTO TABLE ...
"Excursion: Dynamic FROM clause and compatible target data object
DATA(db_table) = `ZDEMO_ABAP_CARR`.
DATA itab TYPE REF TO data.
CREATE DATA itab TYPE TABLE OF (db_table).
SELECT *
FROM (db_table)
INTO TABLE @itab->*.
"Dynamic WHERE clause
"This is an example for using an internal table with a character-like row type
DATA(where_clause) = VALUE string_table( ( `CARRID = 'LH'` )
( `OR CARRID = 'AA'` ) ).
SELECT *
FROM zdemo_abap_fli
WHERE (where_clause)
INTO TABLE ...
```
- Dynamic invoke: Dynamically specifying [procedure](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenprocedure_glosry.htm "Glossary Entry") calls
``` abap
"Note that dynamic method calls require a CALL METHOD statement.
"Note: The following 3 examples assume that there are no
"mandatory parameters defined for the method.
"Method dynamically specified
CALL METHOD class=>(meth).
"Class dynamically specified
CALL METHOD (class)=>meth.
"Class and method dynamically specified
CALL METHOD (class)=>(meth).
"Assigning actual parameters to the formal parameters statically
CALL METHOD class=>(meth) EXPORTING p1 = a1 p2 = a2 ...
IMPORTING p1 = a1 p2 = a2 ...
"Assigning actual parameters to the formal parameters dynamically
DATA ptab TYPE abap_parmbind_tab.
ptab = ...
CALL METHOD class=>(meth) PARAMETER-TABLE ptab.
"Notes on PARAMETER-TABLE ptab
"- The table (of type abap_parmbind_tab; line type is abap_parmbind) must
" be filled and have a line for all non-optional parameters.
"- Components: name -> formal parameter name
" kind -> kind of parameter, e. g. importing
" value -> pointer to appropriate actual parameter,
" is of type REF TO data
"The addition EXCEPTION-TABLE for exceptions is not dealt with here.
```
(back to top)
## Runtime Type Services (RTTS)
[RTTS](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenrun_time_type_services_glosry.htm "Glossary Entry")
represent a hierarchy of [type description
classes](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abentype_class_glosry.htm "Glossary Entry")
containing methods for [Runtime Type Creation
(RTTC)](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenrun_time_type_creation_glosry.htm "Glossary Entry")
and [Runtime Type Identification
(RTTI)](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenrun_time_type_identific_glosry.htm "Glossary Entry").
Using these classes, you can ...
- get type information on data objects, data types or
[instances](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abeninstance_glosry.htm "Glossary Entry")
at runtime.
- define and create new data types at runtime.
The hierarchy of type description classes is as follows.
CL_ABAP_TYPEDESCR
|
|--CL_ABAP_DATADESCR
| |
| |--CL_ABAP_ELEMDESCR
| | |
| | |--CL_ABAP_ENUMDESCR
| |
| |--CL_ABAP_REFDESCR
| |--CL_ABAP_COMPLEXDESCR
| |
| |--CL_ABAP_STRUCTDESCR
| |--CL_ABAP_TABLEDESCR
|
|--CL_ABAP_OBJECTDESCR
|
|--CL_ABAP_CLASSDESCR
|--CL_ABAP_INTFDESCR
So, the
[superclass](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abensuperclass_glosry.htm "Glossary Entry")
`CL_ABAP_TYPEDESCR` has multiple
[subclasses](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abensubclass_glosry.htm "Glossary Entry"),
for example, to deal with each kind of type.
Working with this inheritance tree means making use of
[casts](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abencast_glosry.htm "Glossary Entry"),
especially
[downcasts](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abendown_cast_glosry.htm "Glossary Entry") when retrieving information at runtime.
Detailing out all the possibilities for the information retrieval and
type creation is beyond scope. Check the information, options and
various methods that can be used in the class documentation, e. g. using
F2 help information in
[ADT](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenadt_glosry.htm "Glossary Entry"),
for more details.
The following examples show the retrieval of information. Instead of the
cumbersome extra declaration of data reference variables, you can use
[inline
declarations](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abeninline_declaration_glosry.htm "Glossary Entry").
[Method
chaining](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenmethod_chaining_glosry.htm "Glossary Entry")
comes in handy, too.
``` abap
"The properties of a type are retrieved using RTTI
DATA(some_type) = cl_abap_typedescr=>describe_by_data( var ).
"The components of a structure are retrieved.
"Like above, the describe_by_data method is used together with a variable.
DATA(components) = CAST cl_abap_structdescr(
cl_abap_typedescr=>describe_by_data( some_struc )
)->components.
"The attributes of a global class are retrieved. In contrast to the
"example above the describe_by_name method is used together with the actual name.
DATA(attributes) = CAST cl_abap_classdescr(
cl_abap_classdescr=>describe_by_name( 'CL_SOME_CLASS' )
)->attributes.
```
The following example demonstrates the dynamic creation of data objects.
Note the [`TYPE HANDLE`](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapcreate_data_handle.htm)
addition as part of the `CREATE DATA` statement that is used when referring to dynamically created data types.
``` abap
"RTTC examples
"Creation of an anonymous data object using a type description object for a
"dictionary structure that is obtained using RTTI
"Declaring a data reference variable with a generic type
DATA dref TYPE REF TO data.
"Getting type description using RTTI
DATA(type) = CAST cl_abap_datadescr(
cl_abap_typedescr=>describe_by_name( 'ZDEMO_ABAP_CARR' ) ).
"Creating an anonymous data object using the retrieved type description
CREATE DATA dref TYPE HANDLE type.
"Creating an internal table dynamically
"Getting type description using RTTI
DATA(line_type) = CAST cl_abap_structdescr(
cl_abap_tabledescr=>describe_by_name( `ZDEMO_ABAP_CARR` ) ).
"Defining primary table keys of internal table type to be created
DATA(itab_keys) = VALUE abap_keydescr_tab( ( name = 'CARRID' )
( name = 'CARRNAME' ) ).
"Creating internal table type using the create method of cl_abap_tabledescr
DATA(table_type) = cl_abap_tabledescr=>create(
p_line_type = line_type
p_table_kind = cl_abap_tabledescr=>tablekind_sorted
p_unique = cl_abap_typedescr=>true
p_key = itab_keys ).
"Creating internal table based on the created table type
DATA ref_tab TYPE REF TO data.
CREATE DATA ref_tab TYPE HANDLE table_type.
```
(back to top)
## Further Information
- It is recommended that you also consult section [Dynamic Programming Techniques (F1 docu for standard ABAP)](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abendynamic_prog_technique_gdl.htm) in the ABAP Keyword Documentation since it provides important aspects that should be considered when dealing with dynamic programming in general (e. g. security aspects or runtime error prevention).
- There are even further dynamic programming techniques in the unrestricted language scope like the
generation or execution of programs at runtime. They are not part of this cheat sheet. Find more details on the related syntax (e. g. `GENERATE SUBROUTINE POOL`, `READ REPORT` and `INSERT REPORT` in the ABAP Keyword Documentation for Standard ABAP: [Dynamic Program Development (F1 docu for standard ABAP)](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenabap_language_dynamic.htm)
## Executable Example
[zcl_demo_abap_dynamic_prog](./src/zcl_demo_abap_dynamic_prog.clas.abap)
Note the steps outlined [here](README.md#-getting-started-with-the-examples) about how to import and run the code.