SAP DYNAMIC SQL ABEXA
Get Example source ABAP code based on a different SAP table
SELECT>, Dynamic Token Specification This example demonstrates how tokens are specified dynamically in AB_SQL for the statement
SELECT>>.
ABAP_SOURCE_CODE
DEMO CL_DEMO_DYNAMIC_SQL
ABAP_DESCRIPTION
When executing the class, the name of one of the DDIC database tables SPFLI>, SCARR>, or SAPLANE> can be entered. The table entered is then joined with the table SFLIGHT> in accordance with its foreign key dependency. It is simply assumed that all fields with the same name have a foreign key relationship with each other.
The selected table name is assigned to the field right>. The RTTS class CL_ABAP_TYPEDESCR> is used to determine the type description for the columns in the left and right table of the join. The method GET_COMPONENTS> is used to store these in two type description tables comp_tab1> or comp_tab2>, which contain an entry for each column of the respective DDIC database table.
Then, a fragment of the FROM> clause is created, which contains the names of the tables to be joined; the FROM> clause is later completed using the ON> conditions.
All columns of the left table should be included in the SELECT> list, as well as those columns of the right table for which no columns with the same name exist in the left table. The content of the type description tables is processed in loops.
Initially, the columns of the left table are processed. The current column is incorporated first into the dynamic SELECT> list, which should be specified in the internal table select>. The column name comp1-name> is prefixed with the table name in left>, to ensure its uniqueness. The statement MOVE-CORRESPONDING> is used to copy the column description from comp_tab1> to an internal table comp_tab_join> whose type is suitable for creating objects later.
The columns of the right table are searched for in the left table. If the column comp2-name> only exists in the right table, it is, for the time being, included in the type description table comp_tab_join >. After that, it is added to all columns of the left table of the SELECT> list select>, just like before.
If the column in comp2-name> exists in both tables, an ON> condition that compares columns with the same name from the left and the right table, is added to the dynamic FROM> clause from> .
The type description table comp_tab_join> and the static method CREATE> of the class CL_ABAP_STRUCTDESCR> can now be used to create a suitable type description object for the structure of the target area of the statement SELECT>. This type description object could now be specified after the addition TYPE HANDLE> of the statement CREATE DATA> to create a structured work area. However, the next step uses the method CREATE> of the class CL_ABAP_TABLEDESCR> to create a type description object for an internal table of this row type and creates this table using CREATE DATA>. The dynamically created internal tables can be used by dereferencing the anonymous data object with tref->*> in operand positions.
The data is then read with a fully dynamic statement SELECT >. The content of the anonymous internal table is displayed.