ARTIClE
SELECT, Dynamic Token Specification
The example demonstrates dynamic token specification in
OpenSQL for the statement SELECT .
ABAP_SOURCE_CODE
ABAP_EXEC
ABAP_DESCRIPTION
Once the program has started, the name of one of the database tables
SPFLI , SCARR , or SAPLANE can be entered. The table
entered is then associated with the table SFLIGHT in accordance
with its foreign key relationship. This assumes 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 database table in question.
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 entered 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 the
objects later.
The columns of the right table are searched in the left table. If the
column comp2-name only exists in the right table, it is, for the
time being, incorporated into 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 matching 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,
we go one step further and use the method CREATE of the class
CL_ABAP_TABLEDESCR to create a type description object for an
internal table of this row type and create this with CREATE DATA .
By assigning the dynamically created internal tables to a field symbol
itab , we create a name for the internal table that is
useable in the INTO clause of the statement SELECT . Direct
specification of a dereferencing tref- * in statements other than
ASSIGN is only possible if tref is not generically typed.
However, in our example, tref is generically typed with TYPE
REF TO data .
Subsequently, the data is read with a fully dynamic statement SELECT
. The content of the internal table itab is produced.
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved