Open SQL
Open SQL
Table of Contents
Table of Contents 2
Overview 3
What is OpenSQL? 4
Client Handling 7
Reads 9
■ SELECT 9
■ WITH 12
■ OPEN CURSOR 13
■ UNION 14
Write Accesses 15
Inserting Lines into Tables 16
Specifying a Database Table 16
Inserting a Single Line 17
Inserting Several Lines 18
Database Hints 19
Performance Notes 20
Exceptions 23
2
Overview
■ Open SQL
Language) part. The Open SQL statements use the Open SQL interface
■ Native SQL
3
Database Connectivity), a class-based API that enables object-oriented
this kind are not checked completely by the syntax check and are
database of an AS ABAP.
database, all access types (except for AMDP) also allow access to other
connections.
the central database in the SAP Web AS ABAP. The results of the operations
and any error messages are independent of the database system in use.
Open SQL thus provides a uniform syntax and semantics for all of the
database systems supported by SAP. ABAP programs that only use Open SQL
statements will work in any SAP system, regardless of the database system in
4
use. Open SQL statements can only work with database tables that have
tables to a database view (or view for short). In Open SQL statements, views
are handled in exactly the same way as database tables. Any references to
What is OpenSQL?
Open SQL is a set of instructions for querying the database. Open SQL is the
Overview
Open SQL defines the subset of ABAP statements that enable direct access to
data from the standard database of the current AS ABAP. The Open SQL
statements form the DML part of SQL in ABAP, which is supported by all
database systems. The Open SQL statements are transformed to
database-specific SQL in the Open SQL interface of the database interface.
They are then passed to the database system and executed there. By default,
Open SQL statements use automatic client handling to access only the data
of the current client. To improve performance when accessing the database,
table buffering can be activated for individual database tables, views, or CDS
entities to avoid accessing the database directly each time.
5
Open SQL is closely integrated with ABAP Dictionary. In Open SQL
statements, only those database tables. views, and CDS entities can be
accessed that are defined in ABAP Dictionary. These items are specified
directly in Open SQL statements without specifying a database schema. By
default, the Open SQL interface of the database interface accesses the
associated database objects in the ABAP database schema of the standard
database.
The Open SQL interface of the database interface respects the fact the order
of the columns in the data source in the database system can differ from the
order in the definition of the data source in ABAP Dictionary. If Open SQL is
used, the order in ABAP Dictionary applies and the database interface
performs a transformation if necessary. The function SQL Trace of the
Performance Trace tool (transaction ST05) can be used to analyze the SQL
statements actually passed to the database by the database interface.
6
Return Codes
All Open SQL statements fill the following two system fields with return
codes:
● sy-subrc
After every Open SQL statement, the system field sy-subrc contains the
● sy-dbcnt
After an open SQL statement, the system field sy-dbcnt contains the
7
Client Handling
A single SAP system can manage the application data for several separate
separate areas in the SAP system is called a client, and has a number. When
a user logs onto the SAP Web AS ABAP, they specify a client. The first column
client field (MANDT, from the German word for client). It is also the first field
of the table key. Only universal system tables are client-independent, and do
that access client-dependent application tables only use the data from the
current client. You cannot specify a condition for the client field in the WHERE
clause of an Open SQL statement. If you do so, the system will either return
an error during the syntax check or a runtime error will occur. You cannot
overwrite the MANDT field of a database using Open SQL statements. If you
automatically overwrites it with the current one before processing the Open
Should you need to specify the client specifically in an Open SQL statement,
8
directly after the name of the database table. This addition disables the
automatic client handling and you can use the field MANDT both in the
conditions that are passed from Open SQL to the database contain an
■ In work areas that are used as data sources in write statements, clients
different client.
following descriptions:
Automatic client handling can be switched off using the addition CLIENT
■ In work areas that are used as data sources in write statements, the
9
Operands and Expressions
alongside data sources (database tables, classic views, and CDS entities) and
Columns
expressions
■ ABAP objects
➢ Open SQL - Host variables: for more details see Host variables
expressions
Aggregate Expressions
expressions
functions
10
■ Parameter passing to CDS entities
passing
Reads
The Open SQL language elements for performing reads on the database are:
■ SELECT
The Open SQL statement for reading data from database tables is:
which has a different part to play in selecting, placing, and arranging the data
11
12
The individual clauses and the ways in which they combine are all very
like any other, beginning with the SELECT keyword and ending with a period,
its division into clauses, and the ways in which they combine, make it more
You can use SELECT statements in the WHERE and HAVINGclauses. These are
called subqueries .
You can decouple the INTO clause from the SELECT statement by reading
On certain database systems, this can result in lock conflicts , even with pure
13
■ WITH
The Open SQL keyword WITH introduces the definition of common table
expressions (CTE for short) to be used in a final main query. WITH can be
used as a standalone statement (as shown here) or after the statement
OPEN CURSOR.
14
■ A closing main query SELECT mainquery_clauses, which can contain
the same clauses and additions (apart from FOR ALL ENTRIES) as a
standalone SELECT statement, and works in the same way:
● The results set of the main query is assigned to ABAP data objects
according to the INTO clause.
● As described in SELECT, a single- or multirow results set is created and,
depending on the target area specified in the INTO clause, a SELECT
loop is either opened or not.
● A SELECT loop must close with the ENDWITH statement. For WITH ...
SELECT, ENDWITH has exactly the same meaning as ENDSELECT for a
standalone SELECT loop.
● In the main query, each of the previously defined common table
expressions +cte1, +cte2, and so on, can be used as the data source
data_source.
The language element UNION can be used to combine the results sets of
multiple main queries. In this case, additional rules query_clauses apply for
specifying clauses.
A common table expression defined in the WITH statement can be used after
its definition in the subquery of another common table expression and in the
main query as the data source data_source. It cannot be used in its own
subquery or in the subqueries of preceding definitions. A common table
expression is only known within the current WITH statement.
15
The name +cte of a common table expression is valid across the full WITH
statement. The character + is omitted from the name of the substructure
only when a substructure is created as a data source for a common table
expression in the INTO clause.
The results set of a common table expression never has a client column.
Even if the client column of a client-specific data source is added explicitly in
the subquery to its SELECT list, it does not behave as such in the results set.
For this reason, in a query of the WITH statement that uses a common table
expression as a data source, the addition CLIENT SPECIFIED cannot be
specified.
■ OPEN CURSOR
➢ CLOSE CURSOR
Effect
The Open SQL statement OPEN CURSOR opens a database cursor for the
result set of the main query defined after FOR and links a cursor variable
dbcur with this database cursor. The results set of the main query can be
read with the statement FETCH.
■ The main query is specified using language element SELECT, and its
clauses and additions mainquery_clauses define the result set.
■ The language element UNION can be used to combine the result sets
of multiple queries. In this case, special rules query_clauses apply for
16
specifying clauses.
17
or database rollbacks, if these occur after the first use of the cursor in a
FETCH statement.
■ UNION
Effect
The Open SQL language element UNION joins the result sets of two queries.
The rows of the result set of the query after UNION are inserted into the
result set of the query before UNION. The syntax forms show where UNION
can be used:
1. For creating the result set of the main query of a standalone statement,
which is introduced using WITH or SELECT.
2. For creating the result set of the main query after OPEN CURSOR.
In all syntax forms, it is possible to specify the same clauses and additions
query_clauses for SELECT statements of queries before and after UNION for
18
defining result sets. A query on the right side of UNION can be enclosed in
parentheses ( ). A pair of parentheses can include multiple unions. The
queries joined with UNION are evaluated from left to right. Specific
statements can be prioritized using parentheses.
■ ORDER BY clause
● The addition ORDER BY PRIMARY KEY is not allowed.
● Columns of the union results set specified after ORDER BY must occur
with the same name in all relevant SELECT statements. The names
must be specified directly and cannot be specified with the column
selector ~ after a column name.
■ INTO clause
● If the addition CORRESPONDING or an inline declaration @DATA(...) is
used in the INTO clause, the column names of all results sets defined
in the query_clauses from left to right must match.
● The union result set is always multirow. If an assignment is made to a
non-table-like target are (meaning a SELECT statement without the
addition INTO|APPENDING ... TABLE), a loop closed using ENDSELECT
or ENDWITH is always opened.
■ Restricting the Results Set
19
● The additions UP TO and OFFSET are not currently allowed with
UNION.
Write Accesses
■ INSERT
■ UPDATE
■ MODIFY
■ DELETE
Open SQL contains a set of statements that allow you to change data in the
database. You can insert, change and delete entries in database tables.
However, you must remember that Open SQL statements do not check
They are to be used with care, and, outside the SAP transaction concept ,
discusses the difference between a database LUW and an SAP LUW, and
20
Inserting Lines into Tables
The Open SQL statement for inserting data into a database table is:
It allows you to insert one or more lines into the database table target. You
To specify the database table statically, enter the following for target:
where dbtab is the name of a database table defined in the ABAP Dictionary.
To specify the database table dynamically, enter the following for target:
where the field name contains the name of a database table defined in the
ABAP Dictionary.
You can use the CLIENT SPECIFIED addition to disable automatic client
handling.
21
Inserting a Single Line
The contents of the work area wa are written to the database table dbtab. It
is a good idea to define the work area with reference to the structure of the
database table.
You can also insert single lines using the following shortened form of the
INSERT statement:
Using FROM instead of VALUE allows you to omit the INTOclause. Shorter still
is:
INSERT dbtab.
In this case, the contents of the table work area dbtabare inserted into the
database table with the same name. You must declare this table work area
using the TABLES statement. In this case, it is not possible to specify the
name of the database table dynamically. Table work areas with the same
name as the database table (necessary before Release 4.0) should no longer
22
Inserting Several Lines
This writes all lines of the internal table itabto the database table in one
database already contains a line with the same primary key, a runtime error
occurs. You can prevent the runtime error occurring by using the addition
Whenever you want to insert more than one line into a database table, it is
more efficient to work with an internal table than to insert the lines one by
one.
Notes
an AS ABAP, the implicit database LUWs are normally not sufficient for
23
set is database-specific and undefined. Avoid this kind of parallel
access if possible.
syntax warning.
■ Once a global temporary table is filled using Open SQL, this table must
using the Open SQL statement DELETE FROM without WHERE or using
Database Hints
Syntax
[db @dbhint2
... ] ...
Effect
The addition %_HINTS can be used to specify database hints at the following
24
One or more database hints can be specified after %_HINTS as a
The name of host variables should be prefixed with the escape character @.
The addition db must be used before each database hint to specify the
database system for which the hint is intended. The following table shows
A database system can be specified more than once if there are several
database hints for this system. The possible database hints are
Performance Notes
load on both the database system and the connection between the database
25
system and the application server. This load must be kept as low as possible
evaluations required, and the database system itself, which means that a
different combination of rules and priorities can apply from case to case.
The set of rows selected should be kept as small as possible by using specific
conditions to restrict the set to those rows actually needed. Superfluous rows
data, since here the data is aggregated before the transport in the database
system.
To keep the number of database reads low, mass operations should always
26
should not be used within loops. Instead, joins, views, or subqueries can be
The same data should be not be read more than once. Saving database
tables to the local buffer in table buffering (and also saving prepared data in
Shared Objects) can produce significant time savings, since it takes much
longer for the application server to read the database system than a locally
buffered table. Database tables should always be buffered if they are read
times in different orders, the data should be sorted in the ABAP program.
Bad Example
This example uses a SELECT statement in a SELECT loop to add values from a
different database table to the work area here. An internal table is filled with
FROM spfli
27
ORDER BY carrid, connid, cityfrom, cityto
INTO @DATA(wa).
FROM scarr
INTO (@wa-carrname).
ENDSELECT.
Good Example
This example uses a join expression to fill an internal table directly. The result
The "good" example is generally far quicker than the "bad" example.
FROM spfli AS p
ON p~carrid = c~carrid
28
ORDER BY p~carrid, p~connid, p~cityfrom, p~cityto
Exceptions
Handleable Exceptions
CX_SY_OPEN_SQL_DB
29
■ Cause: An invalid database cursor was used.
CX_SY_DYNAMIC_OSQL_ERROR
■ CX_SY_DYNAMIC_OSQL_SEMANTICS
SAPSQL_SET_ILLEGAL_VALUE
30
● Cause: No table exists with the specified name.
Dictionary.
field.
LOB column.
column.
31
● Cause: An attempt was made to sort the values of a LOB column.
expression.
SAPSQL_LIKE_PATTERN_TOO_LONG
non-numeric type
database connection.
■ CX_SY_DYNAMIC_OSQL_SYNTAX
32
● Cause: The keyword AND is missing after the lower comparison value
of BETWEEN.
● Cause: The logical expression contains a text field literal that does not
expression.
33
● Cause: The logical expression has incorrect parentheses.
● Cause: The logical expression contains a text field literal that does not
● Cause: A column from the right side of a LEFT OUTER JOIN is used in a
WHERE condition.
dynamically.
The following exceptions can only occur when using the statement UPDATE:
34
■ Cause: An attempt was made to change a database field using an
SET.
■ Cause: The statement SET contains a text field literal, for which there is
CX_SY_SQL_UNSUPPORTED_FEATURE
database system.
Non-Handleable Exceptions
35
■ Cause: Invalid call of the database interface.
■ Cause: Data area passed is too short for access to the table.
36
■ Cause: Unexpected return code when accessing a table.
37