Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  ABAP SQL →  ABAP SQL - Reads →  SELECT clauses 

SELECT - FROM

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... FROM { {
data_source [AS tabalias]} 
         | join
         | {(source_syntax) [AS tabalias]} }
         [ client_handling ] ...

Alternatives

1. ... data_source

2. ... join
3. ... (source_syntax)

Addition

... AS tabalias

Effect

The information after FROM specifies whether a query accesses a database table, a classic view, a CDS entity, a hierarchy, or an internal table as a data source data_source, or whether multiple data sources are accessed in a join expression. The optional addition AS defines an alias name for the data source. The optional additions client_handling modify the way client handling is performed.

Alternative 1

... data_source

Effect

Specifies a single data source data_source.


Note

If a data source is specified multiple times after FROM in a join expression, an alias name tabalias must be defined after AS to avoid ambiguities.


Example

Specifies the database table SCARR as the only data source of a SELECT statement.

SELECT FROM scarr 
       FIELDS * 
       INTO TABLE @DATA(result). 

Alternative 2

... join

Effect

Specifies a join expression that joins multiple data sources with one another.


Example

Specifies the database tables SCARR and SPFLI in a join expression.

SELECT FROM scarr 
         INNER JOIN spfli 
           ON scarr~carrid = spfli~carrid 
       FIELDS scarr~carrname, 
              spfli~connid, 
              spfli~cityfrom, 
              spfli~cityto 
       INTO TABLE @DATA(result). 

Alternative 3

... (source_syntax)

Effect

Instead of specifying information statically, a data object source_syntax can be specified in parentheses. When the statement is executed, the data object must contain the syntax displayed for the statically specified information. The data object source_syntax can be a character-like data object or a standard table with a character-like row type. The syntax in source_syntax is not case-sensitive. When an internal table is specified, the syntax can be distributed across multiple rows. Invalid syntax raises a handleable exception from the class CX_SY_DYNAMIC_OSQL_ERROR.

The addition AS used to specify an alias name statically can be specified only if source_syntax contains only the name of a single data source. The addition has the same meaning for this data source as when specified statically. In source_syntax, static attributes or constants of a class cannot be accessed from outside in cases where the class has a static constructor and the constructor was not yet executed.

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

  • If source_syntax is an internal table with a header line, the header line and not the table body is evaluated.
  • The addition AS can be used as part of the dynamic syntax in source_syntax without restrictions.
  • More specifically, if AS is specified statically outside of the dynamically specified syntax, source_syntax cannot contain any path expressions.
  • The class CL_ABAP_DYN_PRG contains methods that make it possible to create correct and secure dynamic code.

  • The literals of the dynamically specified ABAP SQL statements can span multiple rows of a token specified dynamically as an internal table.

  • When specified dynamically, ABAP SQL statements can contain the comment characters * and " as follows:

  • In a dynamic token specified as a character-like data object, all content is ignored from the first comment character ".

  • In a dynamic token specified as an internal table, all rows are ignored that start with the comment character *. In the row, all content is ignored from the first comment character ".
Comment characters placed within literals are, however, part of the literal.

Example

Displays the flight connections (flight date, airline name, and flight number) for a departure city and a destination city. The FROM clause (including any inner joins) is dynamic. The column specified after SELECT is also dynamic. When specified dynamically, the input values are are specified indirectly using the name of the data object in question and are not chained directly. If they were, a special security check would be required for this input.

DATA: cityfr TYPE spfli-cityfrom VALUE 'FRANKFURT', 
      cityto TYPE spfli-cityto   VALUE 'NEW YORK'. 

cl_demo_input=>new( 
  )->add_field( CHANGING field = cityfr 
  )->add_field( CHANGING field = cityto )->request( ). 

DATA: 
  BEGIN OF wa, 
    fldate TYPE sflight-fldate, 
    carrname TYPE scarr-carrname, 
    connid   TYPE spfli-connid, 
  END OF wa, 
  itab LIKE SORTED TABLE OF wa 
            WITH UNIQUE KEY fldate carrname connid. 

DATA(column_syntax) = `c~carrname, p~connid, f~fldate`. 

DATA(source_syntax) = `( ( scarr AS c ` 
  & ` INNER JOIN spfli AS p ON p~carrid  = c~carrid` 
  & ` AND p~cityfrom = @cityfr` 
  & ` AND p~cityto   = @cityto )` 
  & ` INNER JOIN sflight AS f ON f~carrid = p~carrid ` 
  & ` AND f~connid = p~connid )`. 

SELECT (column_syntax) 
       FROM (source_syntax) 
       INTO CORRESPONDING FIELDS OF TABLE @itab. 

cl_demo_output=>display_data( itab ). 

Executable Example

SELECT, Dynamically Specified Tokens

Addition

... AS tabalias

Effect

An alias name tabalias can be assigned to the data source using the addition AS. This name is valid during the SELECT statement only, and in all other positions where this specified data source is addressed, and the actual name does not need to be used.

The alias name tabalias must follow the naming conventions for internal program names and the name table_line cannot be used. This is checked in strict mode of the syntax check from Release 7.54.


Notes


Example

Defines alias names for two data sources of a join expression and their use in the SELECT list and in the ORDER BY clause.

SELECT FROM scarr AS c 
         INNER JOIN spfli AS p 
           ON c~carrid = p~carrid 
       FIELDS c~carrname, p~connid, p~cityfrom, p~cityto 
       ORDER BY c~carrname, p~connid, p~cityfrom, p~cityto 
       INTO TABLE @DATA(result). 

Continue

SELECT - FROM data_source

SELECT - FROM JOIN

SELECT - USING CLIENT, CLIENTS