Ansicht
Dokumentation

ABAPSELECT_INTO_TARGET - SELECT INTO TARGET

ABAPSELECT_INTO_TARGET - SELECT INTO TARGET

ROGBILLS - Synchronize billing plans   Fill RESBD Structure from EBP Component Structure  
This documentation is copyright by SAP AG.
SAP E-Book

SELECT, INTO target

Short Reference



... ${     @dobj       $}
  $| ${     @DATA(dobj)$|@FINAL(dobj) $}
  $| ${ NEW @dref       $}
  $| ${ NEW @DATA(dref)$|@FINAL(dref) $} ...


Alternatives:

1. ... @dobj

2. ... @DATA(dobj)$|@FINAL(dobj)

3. ... NEW @dref

4. ... NEW @DATA(dref)$|@FINAL(dref)

Effect

Specifies a target area of the INTO clause of an query. The possible target areas of the INTO clause are as follows:

  • Elementary data objects elem1, elem2, ... in a comma-separated list.
  • Individual work areas, wa.
  • Internal tables itab after TABLE.

Each target area can be specified as follows:

  • If the addition NEW is not used, dobj specifies the data object directly to which the data from the result set is written. The target area is one of the following:

The addition NEW is used to create an anonymous data object to which data from the result set is written and to which a data reference variable dref points. The data reference variable is one of the following:

All alternatives can occur combined in a comma-separated list for elementary data objects as target areas. One of these alternatives must be selected when a single work area or an internal table is specified.

Note

In the case of the variant NEW @DATA(dref)$|@FINAL(dref), the SELECT list, the FROM clause, and any indicators can be specified dynamically. This is the only way of combining a dynamically defined result set with inline declarations. The same applies to inline declarations after FETCH.

Alternative 1

... @dobj


Effect

Specifies a previously declared host variable @dobj as a target area of the INTO clause. The data in the result set is written directly to the host variable dobj. Depending on the operand position, the host variable can be one of the following:

  • In the comma-separated list elem1, elem2, ..., an elementary data object that matches the type of the associated column of the result set or whose data type can be converted to from the type of the associated column of the result set.
  • As a single work area wa, a data object that meets the prerequisites.
  • As an internal table, any table category whose row structure meets the prerequisites.

Writes to the host variable are made as described in the INTO clause.

Example

Use of different existing host variables as target areas of SELECT statements.

Alternative 2

... @DATA(dobj)$|@FINAL(dobj)


Effect

Specifies a host variable @dobj declared inline as the target area of the INTO clause. The data in the result set is written directly to the host variable dobj. The inline declaration is made with one of the declaration operators DATA or FINAL, which must be prefixed with the escape character @ here. Depending on the operand position, the host variable is declared as follows:

  • An elementary variable is declared in the comma-separated list elem1, elem2, ... The data type of the variables is constructed as follows from the associated column of the result set:
  • The ABAP type to which the dictionary type of a column of a data source is assigned is used for this column.

  • The ABAP type of a host variable is used directly for this variable specified as a single SQL expression.

  • A flat data object wa is declared as a single work area in INTO @DATA(wa) or INTO @FINAL(wa). A standard table itab with an empty table key is declared as an internal table in INTO TABLE @DATA(itab) INTO TABLE @FINAL(itab). The data type of wa or the row type of the internal table are constructed as follows in accordance with the structure of the result set defined after SELECT and the number of data sources specified after FROM:
  • If the result set in the SELECT list is defined using a single specified column col_spec for which no name can be identified, the data type of wa or the row type of itab is its elementary type.

  • If the result set in the SELECT list is defined using a single specified column col_spec for which no name can be identified, the data type of wa or the row type of itab is a structure with a component, with its elementary type.

  • If the result set in the SELECT list is defined using a single data_source~* or a list of multiple specified columns col_spec, the data type of wa or the row type of itab is a structure with elementary components. The data types of the components are the elementary types of the columns in the result set in the order defined there.

  • If data_source~* is specified in the SELECT list together with other elements, the data type of wa or the row type of itab is a nested structure. For each data source data_source specified in this way, a substructure is generated with the name or alias name of the table or view. The data types of the components of the substructures are the elementary types of the data source in the order defined there. If the data source is a common table expression declared using WITH, the first character + of its name is ignored for the name of the substructure.

In reads from a single data source data_source, the data type of wa or the row type of itab is the same as in a definition of the result set using a single data_source~* (see above), that is, a structure with elementary components.
In reads from multiple data sources data_source1, data_source2, ... using a join, the data type of wa or the row type of itab is the same as in a definition of the result set using data_source1~*, data_source2~*, .... (see above), that is, a structure with a substructure for each data source.
  • The names of the elementary components of a structure match the names of the associated columns from the result set. Any alias names defined there are respected.

  • As with an elementary data object in a comma-separated list, the elementary data type of an elementary data object or of an elementary component of a structure is constructed from the type of the associated column of the result set (see above).

  • If the addition INDICATORS is used, a substructure called null_ind is added at the end of the structure or row structure declared inline. For each column in the result set, this substructure contains an identically named component of type x and length 1 in the same order. If preceding components of the structure declared inline are substructures, the substructure null_ind is also structured accordingly.

The prerequisites for an inline declaration are as follows:

  • The result set can be specified dynamically only when combined with the addition NEW. If the addition NEW is not specified, the structure of the result set must be known statically. The SELECT list and any indicators must be specified statically.
  • After FETCH, an inline declaration can only be made together with the addition NEW.
  • The result set defined in the SELECT list cannot have multiple columns with the same name. This can be bypassed using alias names.
  • Alias names of the SELECT list must comply with the naming conventions for internal program names. More specifically, they cannot contain a minus sign -.
  • If the obsolete addition CLIENT SPECIFIED is used to access a client-dependent CDS entity, a name must be specified for the client column at the same time.

Notes

  • Specified columns for which no name can be identified are SQL expressions and aggregate expressions without alias name.
  • When an inline declaration @DATA(itab) or @FINAL(itab) is specified after APPENDING TABLE, this addition usually works like INTO TABLE and is therefore pointless in this combination.
  • When inline declarations are used, the syntax check is performed in a strict mode, which handles the statement more strictly than the regular syntax check.

Example

Reading of individual columns of a result set into different target areas declared inline. carrname, carrid, and url are elementary data objects. wa is a structure with elementary components. itab is a standard table with the corresponding row type.

Example

Reading of all columns of a result set into an inner join in an internal table whose row type is declared as a nested structure with the same structure as the result set. The first component of the nested structure is called SCARR and includes all columns of this DDIC database table. The second component of the nested structure is called SPFLI and includes all columns of this DDIC database table. The content of the columns MANDT and CARRID in both tables is redundant. For the output, the internal table with a nested row type is converted to an output table without substructures.

Inline Declarations

Alternative 3

... NEW @dref


Effect

The addition NEW creates an anonymous data object as the target area of the INTO clause. dref expects a previously declared data reference variable that points to the data object after the object is created. The data of the result set is written to the new anonymous data object. The data reference variable dref can be typed completely or generically.

  • If the data reference variable dref is typed completely, its static type must follow the same rules as for a directly specified data object @dobj:
  • In a comma-separated list (..., dref, ...), it must be an elementary data type to which the type of the associated column of the result set can be converted.

  • In a single work area wa, the type must meet the prerequisites for work areas.

  • In an internal table, it must be a table type with any table category whose row structure meets the prerequisites.

The anonymous data object is created with the type of the data reference variable. The static type of the data reference variable matches the dynamic type. Writes to the anonymous data object are made as described in the INTO clause.
  • If the data reference variable dref is typed generically with data, the data type of the anonymous data object or the dynamic type of dref is constructed depending on the operand position in exactly the same way as the data type of dobj in the inline declaration @DATA$|FINAL(dobj) described above. This means the following:
  • In a comma-separated list (..., dref, ...), an elementary data object is created whose type is determined by the type of the associated column of the result set.

  • In a single work area in INTO NEW @dref, a flat data object is created. A standard table with an empty table key is created as an internal table in INTO TABLE NEW @dref. The type of the new data object or the row type of the internal table is constructed in the same way as in an inline declaration using @DATA$|FINAL(dobj).

The anonymous data object is created with this type. The static type of the data reference variable is more general than the dynamic type. Writes to the anonymous data object are made as described in the INTO clause. The columns of the result set defined in the SELECT list must have unique names.

Unlike in inline declarations with @DATA|FINAL(dobj), the type of the anonymous data object can also be created at runtime. This means that the addition NEW can also be specified in the following cases:

  • If the structure of the result set cannot be known statically due to the use of dynamic tokens.

If possible, the type check takes place as part of the syntax check and otherwise at runtime. If an error is not detected until runtime, an exception of the class CX_SY_DYNAMIC_OSQL_SEMANTICS is raised.

The following restrictions apply:

  • The addition NEW can only be specified after INTO and not after APPENDING.
  • If multiple FETCH statements access a database cursor opened using OPEN CURSOR, a data reference variable used after NEW can be typed generically only if the first of these FETCH statements has the addition NEW with a generically typed data reference variable in the appropriate operand position.

Notes

  • The addition NEW works in a similar way as creating an anonymous data object with the instance operator NEW directly in front of the statement and using the dereferenced reference variable as a target area. The addition NEW has the advantage that the data type of the anonymous data object is constructed in a suitable way and this works especially for dynamic tokens as well.
  • If the addition NEW is used, the syntax check is performed in strict mode from Release .

Example

Use of a generically typed and a completely typed data reference variable after NEW. The anonymous data objects created here both have the same type and the same content. The third SELECT statement writes to an anonymous data object created previously using the instance operator NEW demonstrating roughly how the NEW addition works.

Example

In this example, three anonymous data objects of the type string are created. The columns of the result set are converted to string.

Example

This example compares an INTO clause with an inline declaration of the target area (see above) with an INTO clause with the addition NEW. In both cases, the same data type is constructed, namely an internal table with a nested row structure. As long as individual components are not accessed, the exact data type does not need to be known in the program.

Example

As in the previous example, but after FETCH and with a dynamically specified SELECT list and the FROM clause after OPEN CURSOR. In this case, it is not possible to make a direct inline declaration of the target area in the INTO clause. It is, however, possible to use the addition NEW.

Example

The program DEMO_SELECT_INTO_NEW_VARIANTS demonstrates how the NEW addition is used in different variants of the INTO clause.

Alternative 4

... NEW @DATA(dref)$|@FINAL(dref)


Effect

The addition NEW in front of an inline declaration with the declaration operators @DATA or @FINAL works like the previous variant, however the data reference variable dref that points to the new anonymous data object is declared inline. The static type of the data reference variable dref is defined as follows:

  • If the data type of the new anonymous data object is known statically, dref is also typed with this type.
  • If the data type of the new anonymous data object cannot be known until runtime, dref is typed with the generic type data. This is the case when the structure of the result set is not known statically due to the use of dynamic tokens and in inline declarations after FETCH.

The columns of the result set defined in the SELECT list must have unique names. If multiple FETCH statements access a database cursor opened using OPEN CURSOR, NEW and an inline declaration can be used only if this is also the case in the first of these FETCH statements in the corresponding operand position.

Note

The addition NEW can also be used to make an inline declaration together with dynamic tokens and after FETCH. The data reference variable that points to the target area created as an anonymous data object is declared, however, instead of the direct target area.

Example

Creation of anonymous data objects as target areas together with inline declarations of the data reference variables. After SELECT with static tokens, the data reference variable dref_scarr has the static type of an internal table with the row type SCARR from the ABAP Dictionary. dref_data, on the other hand is typed generically with data after FETCH. This is demonstrated using RTTI methods.






CPI1466 during Backup   SUBST_MERGE_LIST - merge external lists to one complete list with #if... logic for R3up  
This documentation is copyright by SAP AG.

Length: 28378 Date: 20240425 Time: 163856     sap01-206 ( 425 ms )