ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Access → ABAP SQL → ABAP SQL - Write Accesses
INSERT, MODIFY dbtab - subquery_clauses
Other versions:
7.31 | 7.40 | 7.54
Syntax
... { FROM source
FIELDS select_clause }
| { select_clause
FROM source }
[WHERE sql_cond]
[
GROUP BY group] [HAVING group_cond]
[
ORDER BY [UP TO n ROWS [OFFSET o]]]
[db_hints] ...
Effect
Possible clauses and additions of a
subquery after the addition
FROM of the statements
INSERT and MODIFY.
These clauses define a result set, which is used as a data source of the INSERT or INSERT statement.
With the exception of the client column, the results set of the subquery must have at least as many
columns as the primary key of the database table or the classic view filled using the INSERT
statement or modified using the MODIFY statement. It cannot, however, have more columns than the target database table or view .
The following special rules apply:
-
FROMclause
- If the database table or classic view edited using the
INSERTorMODIFYstatement is client-specific, at least one data source of the subquery must be client-specific.
- In the
FROMclause, implicit client handling cannot be switched off using the obsolete additionCLIENT SPECIFIED.
- If the subquery uses default client handling or if client handling is switched using
USING CLIENT, implicit client handling in theINSERTstatement orMODIFYstatement cannot be switched usingCLIENT SPECIFIED.
- If implicit client handling is switched in the subquery using
USING [ALL] CLIENTS [IN], implicit client handling in theINSERTstatement orMODIFYstatement must be switched usingCLIENT SPECIFIED.
-
SELECTclause
- Any
SELECTlists of the subquery specified as comma-separated lists must define one column of the results set for at least every column of the primary key of the database table or classic view in question.
If the subquery uses default client handling or switches client handling usingUSING CLIENT, no client column can be specified as the first column in theSELECTlist. In other positions, client columns are not interpreted as client columns.
If implicit client handling is switched in the subquery usingUSING [ALL] CLIENTS [IN], the first column of theSELECTlist must be a client column.
- In a
SELECTlist of the subquery specified using*, the results set produced for at least every column of the primary key of the database table or classic view in question must contain a column.
If the subquery uses default client handling or switches client handling usingUSING CLIENT, the client column of the results set is ignored.
If implicit client handling is switched in the subquery usingUSING [ALL] CLIENTS [IN], the client column of the results set is respected.
-
ORDER BYclause
- The addition
UP TO n ROWScan only be used afterORDER BY.
- The addition
OFFSETcan only be used afterUP TO n ROWS.
- An
ORDER BYclause in a subquery is not supported by all databases. This means a syntax check warning from the extended program check can occur that can be hidden using the pragma##db_feature_mode[limit_in_subselect_or_cte]. If this is detected at runtime on a database that does not support the pragma, a handleable exception of the class CX_SY_SQL_UNSUPPORTED_FEATURE is raised.
A further subquery within the subquery cannot access the table or view edited using the INSERT or MODIFY statement.
Notes
-
In an ABAP program, it is possible to use the method USE_FEATURES of the class
CL_ABAP_DBFEATURES to check whether the current database system or a database system accessed using a
secondary connection supports
ORDER BYclauses in subqueries. This requires the constant LIMIT_IN_SUBSELECT_OR_CTE of this class to be passed to the method in an internal table. - If a subquery is used, the ABAP SQL statement bypasses table buffering.
-
Implicit client handling of the subquery is not specified by client handling of the
INSERTstatement orMODIFYstatement An addition USING orCLIENT SPECIFIEDof theINSERTstatement orMODIFYstatement is ignored by the subquery. The subquery uses either the current client ID or a client specified in the subquery usingUSING. When the result of the subquery is written, however, implicit client handling of theINSERTstatement orMODIFYstatement applies by default. This can be defined using the additionsUSING CLIENTorCLIENT SPECIFIED.
- If a query is used to access a
CDS entity associated with a
CDS role and for which
CDS access control
is not disabled using the value #NOT_ALLOWED for the annotation @AccessControl.authorizationCheck or using the addition
WITH PRIVILEGED ACCESSin theFROMclause, only that data is read implicitly that matches the access condition defined in the CDS role. If data cannot be read, ABAP programs cannot distinguish whether this is due to the conditions of theSELECTstatement, the conditions of the CDS entity, or an associated CDS role. If the CDS database view database view of a CDS view is accessed, no access control takes place.
-
The use of a subquery in the statement
MODIFYand the us ofUSING CLIENTor an access in the subquery to the database table or classic view that is filled with anINSERTstatement results in the strict mode as of Release 7.53. If used,USING [ALL] CLIENTS [IN]applies strict mode from Release 7.54.
Example
Inserts columns of a results set formed by an inner join in a database table.
DELETE FROM demo_join1.
INSERT demo_join1
FROM TABLE @( VALUE #( ( a = 'a1' b = 'b1' d = 'd1' )
( a = 'a2' b = 'b2' d = 'd2' )
( a = 'a3' b = 'b3' d = 'd3' ) ) ).
DELETE FROM demo_join2.
INSERT demo_join2
FROM TABLE @( VALUE #( ( d = 'd1' e = 'e1' f = 'f1' )
( d = 'd2' e = 'e2' f = 'f2' )
( d = 'd3' e = 'e3' f = 'f3' ) ) ).
DELETE FROM demo_join3.
INSERT demo_join3 FROM ( SELECT FROM demo_join1 AS d1
INNER JOIN demo_join2 AS d2
ON d1~d = d2~d
FIELDS d1~a, d1~b, d2~e, d2~f ).
SELECT *
FROM demo_join3
INTO TABLE @DATA(result).
cl_demo_output=>display( result ).