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

SELECT - FROM  Syntax Diagram

Short Reference

Syntax

... FROM { {dbtab [AS tabalias]}
         | join
         | {(dbtab_syntax) [AS tabalias]} }
         [CLIENT SPECIFIED]
         [UP TO n ROWS]
         [BYPASSING BUFFER]
        [CONNECTION con|(con_syntax)] ... .

Alternatives:

1. ... dbtab [AS tabalias]

2. ... join

3. ... (dbtab_syntax) [AS tabalias]

Extras:

1.... CLIENT SPECIFIED

2.... UP TO n ROWS

3.... BYPASSING BUFFER

Effect

Entries in source specify whether a database table, a view, or multiple database tables or views are accessed by a join expression. Optional additions perform client handling, specify whether SAP buffering is bypassed, and determine the maximum number of rows to be read.

Alternative 1

... dbtab [AS tabalias]


Effect

A database table or view defined in ABAP Dictionary can be specified for dbtab. An alternative table name tabalias can be assigned to the database table or the view using the addition AS. This name is valid during the SELECT statement only, and in all other positions where this specified database table is addressed, and the actual name does not need to be used.

Note

If a database table or a view appears multiple times after FROM in a join expression, the alternative name must be used to avoid ambiguities.

Example

Reading from the database table spfli and assigning the alternative name s. In this case, the prefix s~ after ORDER BY also does not need to be specified, because only one database table is read and the column name carrid is unique. The prefix spfli~ can no longer be used when assigning the alternative name.

DATA wa TYPE spfli.

SELECT *
       FROM spfli AS s
       INTO wa
       ORDER BY s~carrid.
  cl_demo_output=>write( wa ).
ENDSELECT.
cl_demo_output=>display( ).

Alternative 2

... join


Effect

Specifies a join expression that joins multiple database tables or views with one another.

Alternative 3

... (dbtab_syntax) [AS tabalias]


Effect

Instead of making static specifications, a data object dbtab_syntax can be specified in parentheses. When executing the statement, it must contain the syntax displayed when specified statically. The data object dbtab_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 dbtab_syntax is, as in ABAP Editor, not case-sensitive. When an internal table is specified, the syntax can be distributed across multiple rows.

The addition AS can be specified only if dbtab_syntax contains only the name of a single database table or a view. The addition has the same meaning for this database table or view as when specified statically.

When specifying the syntax in dbtab_syntax, the following restrictions apply:

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

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,
      dbtab_syntax TYPE string.

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

dbtab_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 (dbtab_syntax)
       INTO CORRESPONDING FIELDS OF TABLE itab.

cl_demo_output=>display_data( itab ).

Example

See SELECT, Dynamically Specified Tokens

Addition 1

... CLIENT SPECIFIED

Effect

This addition deactivates automatic client handling in Open SQL. When specifying a single database table or a single view, the addition must be inserted directly after dbtab in the join condition. When specifying a join expression, it must be inserted after the last addition ON of the join condition.

When using the addition CLIENT SPECIFIED, the first column of the client-specific database tables can be specified in the WHERE condition to determine the client identifier. In the addition ORDER BY, the column can be sorted explicitly according to client identifier.

Notes

Example

Reading all customers in client "800".

DATA wa_scustom TYPE scustom.

SELECT *
       FROM scustom CLIENT SPECIFIED
       INTO wa_scustom
       WHERE mandt = '800'.
ENDSELECT.

Addition 2

... UP TO n ROWS

Effect

This addition restricts the number of rows in the result set. n expects a data object of type i that can contain all non-negative numbers from the value range except its maximum value +2.147.483.647. A positive number in n indicates the maximum number of rows in the result set. If n contains the value 0, all selected rows are passed to the result set. If n contains a negative number or +2.147.483.647, a syntax error is produced or a non-handleable exception is raised.

Notes

Example

Reading the three business customers with the highest discount rates:

DATA: wa_scustom TYPE scustom.

SELECT *
       FROM scustom UP TO 3 ROWS
       INTO wa_scustom
       WHERE custtype = 'B'
       ORDER BY discount DESCENDING.
ENDSELECT.

Addition 3

... BYPASSING BUFFER

Effect

This addition causes the SELECT statement to bypass SAP buffering and to read directly from the database and not from the buffer on the application server




Continue
SELECT - JOIN
SELECT - CONNECTION
Joins