ABAP - Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Reads →  SELECT clauses → 

SELECT - FROM

Quick Reference

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 specified after FROM specifies whether on a query on a database table, a classic view, a CDS entity is accessed as a data source data_source, or whether multiple data sources are accessed in a join expression. The optional addition AS defines an alternative table name. 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 alternative name tabalias must be specified after AS to avoid ambiguities.

Alternative 2

... join


Effect

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

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 (as is the case in ABAP Editor). When an internal table is specified, the syntax can be distributed across multiple rows.

The addition AS used to specify an alternative table 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

Comment characters placed within literals are, however, part of the literal.

Example

Displaying the flight connections (flight date, airline name, and flight number) for the user input of a departure city and a destination city. The inner joins are constructed dynamically at runtime. The column specified after SELECT is also dynamic. The values entered on the selection screen are specified dynamically using the name of the parameter in question. They are not chained directly. If they were, a special security check would be required for these parameters.

PARAMETERS: p_cityfr TYPE spfli-cityfrom,
            p_cityto TYPE spfli-cityto.

DATA: BEGIN OF wa,
         fldate TYPE sflight-fldate,
         carrname TYPE scarr-carrname,
         connid   TYPE spfli-connid,
       END OF wa.

DATA itab LIKE SORTED TABLE OF wa
               WITH UNIQUE KEY fldate carrname connid.
DATA: column_syntax TYPE string,
      source_syntax TYPE string.

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

source_syntax = `( ( scarr AS c `
  & ` INNER JOIN spfli AS p ON p~carrid  = c~carrid`
  & ` AND p~cityfrom = p_cityfr`
  & ` AND p~cityto   = p_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, Dynamic Token Specification

Addition

... AS tabalias

Effect

An alternative table 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 alternative table name tabalias can have a maximum of 30 places and can contain letters, digits, the minus sign (-), and the underscore (_) in any order.

Notes



Continue
SELECT - data_source
SELECT - JOIN
SELECT - CLIENT