SELECT

SELECT

Basic form

SELECT result [target] FROM source [where] [GROUP BY fields] [ORDER BY order].

Effect

Retrieves a subset and/or a summary of data from a database table or view (see Relational Databases). SELECT belongs to the Open SQL command set.

Each SELECT command consists of a series of clauses specifying different tasks:

The SELECT result clause specifies

The INTO target clause specifies the target area into which the selected data is to be read. If the target area is an internal table, the INTO clause specifies

The INTO clause can also follow the FROM clause. You can omit it if:
- you specify '*' in the SELECT clause,
- you do not use a JOIN in the FROM clause, and
- you have defined a table work area dbtab in the program using the TABLES statement.

You can omit the INTO clause. The system then makes the data available in the table work area (see TABLES) dbtab. If the SELECT clause includes a "*", the command is processed like the identical SELECT * INTO dbtab FROM dbtab statement. If the SELECT clause contains a list a1 ... an, the command is executed like SELECT a1 ... an INTO CORRESPONDING FIELDS OF dbtab FROM dbtab.

If the result of the selection is meant to be a table, the data is usually read line by line within a processing loop introduced by SELECT and concluded by ENDSELECT (for further information, see INTO clause ). For each line read, the processing passes through the loop once. If the result of the selection is meant to be a single record, the closing ENDSELECT is omitted.

The FROM source clause names the source ( database table or view ) from which the data is to be selected. It also determines

The WHERE where clause specifies the conditions which the result of the selection must satisfy. It thus determines the lines of the result table. By default - i.e. unless a client field is specified in the WHERE clause - only data of the current client is selected. If you want to select across other clients, the FROM clause must include the addition ... CLIENT SPECIFIED.

The GROUP-BY fields clause combines groups of lines together into single lines. A group is a set of lines which contain the same value for every database field in the GROUP BY clause.

The HAVING clause contains conditions for the summarized lines in the internal table.

The ORDER-BY order clause stipulates how the lines of the result table are to be ordered.

Each time the SELECT statement is executed, the system field SY-DBCNT contains the number of lines read so far. After ENDSELECT, SY-DBCNT contains the total number of lines read.

SY-SUBRC = 8:

Only with SELECT SINGLE FOR UPDATE: Not all primary key fields in der WHERE condition were specified. No line was read.

Example

Output the passenger list for the Lufthansa flight 0400 on 28.02.1995:

TABLES SBOOK. 
 
SELECT * FROM SBOOK 
  WHERE 
	CARRID   = 'LH '	AND 
	CONNID   = '0400'	 AND 
	FLDATE   = '19950228' 
  ORDER BY PRIMARY KEY. 
  WRITE: / SBOOK-BOOKID, SBOOK-CUSTOMID,   SBOOK-CUSTTYPE, 
		 SBOOK-SMOKER, SBOOK-LUGGWEIGHT, SBOOK-WUNIT, 
		 SBOOK-INVOICE. 
ENDSELECT.

In client/server environments, storing database tables in local buffers (see SAP Buffering) can save a considerable amount of time because the time needed to gain access via the network is much greater than the time needed to access a locally buffered table.

A SELECT command on a table, for which SAP Buffering is defined in the ABAP/4 Dictionary, is normally satisfied from the SAP buffer by bypassing the database. This does not apply

- with SELECT SINGLE FOR UPDATE
- with SELECT DISTINCT in the SELECT clause,
- with BYPASSING BUFFER in the
FROM clause ,
- with ORDER BY f1 ... fn in the ORDER-BY clause,
- with aggregate functions in the
SELECT clause,
- when using IS [NOT] NULL in the
WHERE condition,
- if the generic key part is not qualified in the WHERE condition for a generically buffered table.

Related

OPEN CURSOR, FETCH and CLOSE CURSOR