ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Read Accesses →  SELECT →  SELECT - result → 

SELECT - columns

Short Reference

Syntax

... *
  | { {col1|aggregate( [DISTINCT] col1 )} [AS a1]
      {col2|aggregate( [DISTINCT] col2 )} [AS a2]  ... }
  | (column_syntax) ... .

Alternatives:

1. ... *

2. ... {col1|aggregate( [DISTINCT] col1 )} [AS a1]
      {col2|aggregate( [DISTINCT] col2 )} [AS a2] ...

3. ... (column_syntax)

Effect

The data in columns determines which columns are used to construct the results set.

Alternative 1

... *


Effect

If * is specified, the results set is constructed based on all columns in the database tables or views specified after FROM, in the order given there. The columns in the results set inherit the name and data type from the database tables or views. Only one data object can be specified after INTO.

Notes

Example

Reads all columns of multiple rows.

DATA wa TYPE spfli.

SELECT *
       FROM spfli
       INTO CORRESPONDING FIELDS OF wa
       WHERE carrid = 'LH'.
ENDSELECT.

Alternative 2

... {col1|aggregate( [DISTINCT] col1 )} [AS a1]
    {col2|aggregate( [DISTINCT] col2 )} [AS a2] ...


Effect

A list of column labels col1 col2 ... is specified in order to construct the results list from individual columns. An individual column can be specified directly or as an argument of an aggregate function aggregate. The order in which the column labels are specified is free and defines the order of the columns in the results list. Only if a column of the type LCHAR or LRAW is listed does the corresponding length field also have to be specified directly before it. An individual column can be specified more than once. Various specifications can be made after INTO, and their interaction is described by the column specified there.

The addition AS can be used to define an alternative column name a1 a2 ... (with a maximum of thirty characters) in the results set for every column label col1 col2 .... An alternative column name cannot be assigned more than once. You should also not use the name of column that does not have any alternative names assigned to it. The system uses the alternative column name in the additions INTO|APPENDING CORRESPONDING FIELDS and ORDER BY. A non-unique column name used after ORDER BY causes a syntax error or an exception.

Column Labels

The following column labels are possible:

  1. If only a single database table or a single view is specified after FROM, the column labels in the database table (that is, the names of the components comp1 comp2...) can be specified directly for col1 col2 ... in the structure from ABAP Dictionary.

  2. If the name of the component appears in multiple database tables of the addition FROM and the required database table or view dbtab is specified only once after FROM, the names dbtab~comp1 dbtab~comp2 ... must be specified for col1 col2 .... Here, comp1 comp2 ... are the names of the components in the structure from ABAP Dictionary and ~ is the column selector.

  3. If the required database table or view occurs multiple times after FROM, the names tabalias~comp1 tabalias~comp2 ... have to be specified for col1 col2 .... tabalias is the alternative table name of the database table or view defined after FROM, and comp1 comp2 ... are the names of the components in the structure from ABAP Dictionary and ~ is the column selector.

The data type of a single column in the results list is the data type of the corresponding component in ABAP Dictionary. The corresponding data object after INTO or APPENDING has to be selected accordingly.

Notes

Example

Produces the flight date and the average booking rate of all customers of Lufthansa flights with the flight number 0400. The alternative name avg of the aggregate expression is required to sort the results set and assign it to the columns of the target table using CORRESPONDING FIELDS.

TYPES: BEGIN OF wa,
         fldate  TYPE sbook-fldate,
         avg     TYPE sbook-loccuram,
       END OF wa.
DATA   itab TYPE TABLE OF wa WITH EMPTY KEY.

SELECT fldate AVG( loccuram ) AS avg
       INTO CORRESPONDING FIELDS OF TABLE itab
       FROM sbook
       WHERE sbook~carrid = 'LH' AND
             sbook~connid = '0400'
       GROUP BY fldate
       ORDER BY avg DESCENDING.

cl_demo_output=>display( itab ).

Alternative 3

... (column_syntax)


Effect

Instead of static data, a data object column_syntax in parentheses can be specified, which, when the command is executed, either contains the syntax shown with the static data, or is initial. The data object column_syntax can be a character-like data object or a standard table without secondary table keys and with a character-like data object. The syntax in column_syntax, as ABAP Editor, is not case-sensitive. When an internal table is specified, the syntax can be distributed across multiple rows.

If column_syntax is initial when the command is executed, columns is implicitly set to * and all columns are read.

If columns are specified dynamically without the SINGLE addition, the results set is always regarded as having multiple rows.

Security Note

If used wrongly, dynamic programming techniques can present a serious security risk. Any dynamic content that is passed to a program from the outside must be checked thoroughly or escaped before being used in dynamic statements. This can be done using the system class CL_ABAP_DYN_PRG or the predefined function escape. See SQL Injections Using Dynamic Tokens.

Notes

Example

Produces all departure or destination cities of Lufthansa flights, depending on whether 'CITYFROM' or 'CITYTO' is specified. A method of the class CL_ABAP_DYN_PRG is used to check whether the input values are valid.

DATA: comp      TYPE c LENGTH 16,
      dref  TYPE REF TO data.

cl_demo_input=>request( CHANGING field = comp ).
TRY.
    comp =
      cl_abap_dyn_prg=>check_whitelist_tab(
        val = to_upper( comp )
        whitelist = VALUE string_hashed_table( ( `CITYFROM` )
                                               ( `CITYTO` ) ) ).
  CATCH cx_abap_not_in_whitelist.
    cl_demo_output=>display( 'Not allowed' ).
    LEAVE PROGRAM.
ENDTRY.

DATA(long_name) = `SPFLI-` && comp.
CREATE DATA dref TYPE (long_name).
ASSIGN dref->* TO FIELD-SYMBOL(<fs>).

DATA output TYPE TABLE OF string WITH EMPTY KEY.
SELECT DISTINCT (comp)
       INTO <fs>
       FROM spfli
       WHERE carrid = 'LH'.
  APPEND |{ <fs> }| TO output.
ENDSELECT.
cl_demo_output=>display( output ).

Example

See SELECT, dynamic token specification