).
```
3. **Dynamic specification of clauses in ABAP SQL statements**
For example, a token that includes the `WHERE` clause conditions in a `SELECT` statement. The token can also be an internal table of a character-like line type.
Examples:
``` abap
"Dynamic SELECT list
DATA(select_list) = `CARRID, CONNID, COUNTRYFR, COUNTRYTO`.
SELECT (select_list)
FROM zdemo_abap_fli
INTO TABLE @itab.
"Dynamic FROM clause
DATA(table) = `ZDEMO_ABAP_FLI`.
SELECT *
FROM (table)
INTO TABLE @itab.
"Dynamic WHERE clause
DATA(where_clause) = `CARRID = 'LH'`.
SELECT *
FROM zdemo_abap_fli
WHERE (where_clause) INTO TABLE @itab.
```
4. **Dynamic specification of [procedures](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenprocedure_glosry.htm "Glossary Entry")**
Names are specified dynamically, e. g. the names of classes and methods.
Examples:
``` abap
"Dynamic method calls
"Note that these calls require a CALL METHOD statement.
"Method dynamically specified.
CALL METHOD class=>(meth).
"Class dynamically specified.
CALL METHOD (class)=>meth.
"Class and method dynamically specified.
CALL METHOD (class)=>(meth).
"Specifying parameters
CALL METHOD class=>(meth) IMPORTING param = ... .
"Parameters and exceptions can also be specified dynamically in tables.
CALL METHOD class=>(meth) PARAMETER-TABLE ptab.
CALL METHOD class=>(meth) PARAMETER-TABLE ptab EXCEPTION-TABLE etab.
```
Regarding the addition `PARAMETER-TABLE`, you can assign [actual
parameters](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenactual_parameter_glosry.htm "Glossary Entry")
to [formal
parameters](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenformal_parameter_glosry.htm "Glossary Entry")
dynamically using the table `ptab` that is of type
`ABAP_PARMBIND_TAB`. The table must be filled and have a
line for all non-optional parameters. The line type is
`ABAP_PARMBIND`. The following fields are relevant:
- `name`: The name of the formal parameter.
- `kind`: Specifies the kind of parameter, e. g. importing or exporting parameter. You can make use of the constants defined in class `CL_ABAP_OBJECTDESCR`. Note that if the method signature has an importing parameter, it must be specified as exporting parameter here and vice versa.
- `value`: Specifies a data reference to the actual parameter.
Errors raise catchable exceptions of class `CX_SY_DYN_CALL_ERROR`. Using the addition `EXCEPTION-TABLE` and an internal table of type `ABAP_EXCPBIND_TAB`, you can handle non-[class-based
exceptions](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenclass_based_exception_glosry.htm "Glossary Entry").
(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. Among them, there are, for
example, structures or tables. Working with this superclass and its
subclasses 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").
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.
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 creation of an internal table
type based on a [DDIC
type](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenddic_type_glosry.htm "Glossary Entry").
Furthermore, an internal table is created based on this type. The type
itself is a sorted table (constants can also be used here). Unique keys
are defined in a dedicated table of type
`ABAP_KEYDESCR_TAB` that is part of the
`cl_abap_tabledescr=>create` method call.
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
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(key_tab) = VALUE abap_keydescr_tab( ( name = 'CARRID' )
( name = 'CARRNAME' ) ).
"Creating internal table type
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 = key_tab ).
"Create 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](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.