03 Abap SQL
03 Abap SQL
03 Abap SQL
ABAP SQL
- ABAP SQL
o Introduction
o Excursion: Database Tables and Views
o Reading Data Using SELECT
� Basic Syntax
� Using SELECT for Multiple Purposes
� Clause Variations and Additions in SELECT Statements
� More Clauses
� Operands and Expressions in ABAP SQL Statements
� SQL operands
� SQL Expressions
o Elementary Expressions
o SQL Functions
o More SQL Expressions
o Window Expressions
� SQL Conditions
� Selecting Data by Evaluating the Content of Other Tables
� Combining Data of Multiple Database Tables
� Common Table Expressions (CTE)
o Changing Data in Database Tables
� Using INSERT
� Using UPDATE
� Using MODIFY
� Using DELETE
o More Information
o Executable Example
Introduction
- ABAP SQL is a subset of SQL
which is the standardized language for accessing databases.
- The main ABAP SQL keywords to read and change data are the
following:
Keyword Purpose
| `SELECT` | Reads data from database tables
|
| `INSERT` | Adds rows to database tables
|
| `UPDATE` | Changes the content of rows of database tables
|
| `MODIFY` | Inserts rows into database tables or changes the content of existing
rows |
| `DELETE` | Deletes rows from database tables
|
- ABAP SQL statements use the ABAP SQL interface. This interface transforms all ABAP SQL
statements that access the standard database of an AS ABAP to platform-dependent SQL and
forwards the results to the database system.
- Generally bear in mind the performance notes when using
ABAP SQL. The considerations there are not relevant for this cheat sheet since
the focus is on syntax options.
💡 Note<br>
- There are further clauses available of which some are dealt with
further down. In general, the recommendation is to hit F1 for the keywords and additions to get
all the details in the ABAP Keyword Documentation.
- Especially in older ABAP programs, you will see other forms of the
SELECT syntax that you should no longer use. Strict syntax check modes might enforce the use
of specific ABAP SQL syntax. For example, the INTO clause should be placed after the other
clauses. Furthermore, host variables or host expressions are required for data objects and
expressions, i. e. they must be preceded by @ or @( ... ). Further information: Release-
Dependent Syntax Check Modes (F1 docu for standard ABAP).
- Regarding host variables as in SELECT ... INTO @target. and since they are used in most
examples below: A host variable is a data object that is
o declared in the ABAP program
o prefixed with the @ character and
o specified in an operand position of an ABAP SQL statement.
See more information here.
- The SELECT list, i. e. the fields that are specified, can also be specified following the SELECT
keyword before the FROM clause - without FIELDS. The following two SELECT statements are
basically the same but differently arranged. The code snippets in the cheat sheet randomly use
one syntax or the other.
``` abap
SELECT FROM dbtab
FIELDS comp1, comp2, comp3
...
SELECT comp1, comp2, comp3
FROM dbtab
...
```
- Regarding the target into which data is read: Instead of using a
variable that is (extra) declared beforehand, you can also make use
of inline
declarations,
for example ... INTO TABLE @DATA(itab)., to comfortably
create an appropriate variable in place. Note that in case of
internal tables as targets, the resulting table is a standard table
and has an empty key which might have an impact when further
processing the internal table entries. Find more information in the
ABAP cheat sheet Internal Tables. In newer ABAP releases, the declaration operator FINAL can
be used to declare immutable variables.
💡 Note<br>
- Although its use is optional, a WHERE clause should be specified to further restrict the read result.
- Regarding the addition CORRESPONDING FIELDS OF in the INTO
clause: As mentioned, only the content of columns for which there are identically named
components in the target are assigned. However, if you want to read data into an existing data
object and particular fields are specified in the SELECT list and if the addition is not specified,
you might stumble on undesired results. The target data object must contain enough
components and the content of the columns are assigned to the components of the target from
left to right in the order specified after SELECT. The content of surplus components of the target
is not changed. Plus, pay attention to assignment rules. Basic rule: Without CORRESPONDING ...,
column names do not play a role but only the position. With CORRESPONDING ..., the position
of the columns does not play a role but only the name.
- Find more information regarding the addition INTO here.
Reading multiple rows into an internal table.
SELECT FROM dbtab
FIELDS * "All fields
WHERE ...
INTO TABLE @itab. "itab has an appropriate row type
- A SELECT loop can be opened if the assignment is made to a structure and the addition SINGLE is
not used.
- If the row is found, the system field sy-subrc is set to 0.
- The loop must be closed using ENDSELECT.
- To terminate the loop completely, you can use the statement EXIT.
- Note: As covered further down, when using the addition PACKAGE SIZE and storing the result in
a table, a loop is opened, too.
SELECT FROM dbtab
FIELDS *
WHERE ...
INTO @struc.
IF sy-subrc = 0.
... "For example, making changes on data and adding the row to an internal
table.
ENDIF.
ENDSELECT.
IF exists = abap_true.
...
ENDIF.
Removing rows that occur more than once in a multirow result set using the DISTINCT addition.
- Cannot be used with the addition SINGLE.
- See more information here here.
SELECT DISTINCT comp1
FROM dbtab
WHERE ...
INTO TABLE @itab.
o Defining alias names for individual columns of the result set with AS.
o Make sure that you use an alias name only once here. In the statement, the alias name
can only be used after an ORDER BY clause.
o As shown further down, in some cases (e. g. when using SQL expressions) the
specification of an alias name is required. Setting an alias name for the data source is
also possible (SELECT FROM dbtab AS alias_name ...). See the section on joins
further down.
💡 Note<br>
You have plenty of options regarding the specification of the columns in the SELECT list, among them,
the outlined direct specification of the column name. SQL expressions can be specified, too. See more
details here and in the sections on SQL expressions further down.
"All fields
SELECT * FROM dbtab
WHERE ...
INTO ...
"Comma-separated list
SELECT col1, col2, col3
FROM dbtab
WHERE ...
INTO ...
Reading data from an internal table as data source using SELECT. Note that an alias name must be
specified for the internal table used as data source.
Find more information here.
SELECT *
FROM @itab1 AS tab
WHERE ...
INTO TABLE @DATA(itab2).
Limiting the number of returned table rows using the optional addition UP TO n ROWS.
"A maximum of five rows are to be returned
"If the INTO clause is the last clause, the UP TO clause must be positioned after it.
SELECT * FROM dbtab
WHERE ...
INTO TABLE @DATA(itab_upto)
UP TO 5 ROWS.
Returning only the table rows after a row with a specified count from the result set using the optional
addition OFFSET n. You can only use the addition, if an ORDER BY clause is specified.
"In the example, data of all flights are retrieved, except for the 2 flights
"with the shortest flight time.
SELECT *
FROM ztest_abap_flsch
WHERE carrid = 'LH'
ORDER BY fltime ASCENDING
INTO TABLE @DATA(itab)
OFFSET 2.
Reading into packages of a specified number of rows when reading into internal tables. The addition
PACKAGE SIZE n can be specified after INTO TABLE and APPENDING TABLE. A SELECT loop ist opened.
After PACKAGE SIZE, the number of rows is specified (which can be a host variable, host expression or a
literal of type i) denoting the number of rows to be inserted in the target object per iteration.
SELECT FROM dbtab
FIELDS comp1, comp2, comp3
WHERE ...
INTO TABLE @DATA(itab_pack) PACKAGE SIZE n.
...
ENDSELECT.
Specifying an anonymous data object as target object using the addition NEW. Only to be used after INTO
and not APPENDING.
"Here, the target object is an anonymous data object declared inline.
SELECT FROM dbtab
FIELDS comp1, comp2, comp3
WHERE ...
INTO TABLE NEW @DATA(dref).
More Clauses
GROUP BY
clause: Combining groups of table rows in the result set. You
might also use SQL expressions
here. Multiple clause elements are separated by a comma. Find more
information on SQL expressions further down.
Note that the GROUP BY clause requires all columns that are
directly specified in the SELECT list or specified there as an
argument of an SQL expression to be specified. An exception to this is
aggregate
functions
in aggregate
expressions
(except grouping
functions)
as shown in the following example.
In the example below, the database table rows that have the same content in column comp1 are
combined. The lowest and highest values in column comp2 are determined for each of these groups and
placed into the combined row.
SELECT FROM dbtab
FIELDS comp1, MIN( comp2 ) AS min, MAX( comp2 ) AS max
WHERE ...
GROUP BY comp1
INTO ...
HAVING
clause: Limiting the number of table rows in groups in the
result by setting conditions on these rows. The rows for which a
logical expression is true are inserted in the target variable. Note
that HAVING can only be used together with GROUP BY.
SELECT FROM dbtab
FIELDS comp1, MIN( comp2 ) AS min, MAX( comp3 ) AS max
WHERE ...
GROUP BY comp1
HAVING comp1 LIKE '%XYZ%' AND SUM( comp4 ) > 100
INTO ...
ORDER BY
clause: Sorting the result set by specified columns.
The following example shows the ordering of the result set based on the
content of the primary key of the data
source.
You can also order by any columns and by explicitly specifying the sort
order. There are more ordering options, for example, by using SQL
expressions.
SELECT FROM dbtab
FIELDS comp1, comp2, comp3
WHERE ...
ORDER BY PRIMARY KEY
"comp2 ASCENDING
"comp2 DESCENDING
INTO ...
💡 Note<br>
- Not specifying ORDER BY means that the order of entries in the result set is undefined.
- If ORDER BY and GROUP BY clauses are used, all columns specified after ORDER BY must also be
specified after GROUP BY.
- If aggregate functions are specified after SELECT, all columns that are specified after ORDER BY
and that do not have an alias name for an aggregate function must also be specified after SELECT
and after the GROUP BY clause which is required in this case, too.
WHERE clause: Restricts the number of rows that are included in the result set using logical expressions.
See further information on them in the following sections.
SELECT FROM dbtab
FIELDS comp1, comp2, comp3
WHERE comp1 = 'abc'
AND comp2 < 123
INTO ...
SQL operands
- Are elementary operands in an ABAP SQL statement
- Can be database table or view columns, a
literal,
host
variables
(i. e. global or local data objects escaped using @:
@dobj) or host
expressions
(@( ... ))
o Regarding literals: They are not prefixed with the escape
character @. The literals can be
typed
(using the type name and content within a pair of backquotes:
<code>char\`abc\`</code>) with built-in ABAP Dictionary
types
or untyped. Typed literals are preferable for the following
reasons: Using untyped literals means extra cost in terms of
performance since they must be converted by the compiler. Plus,
their use can result in errors at runtime whereas typed literals
guarantee type compatibility at once. For more information on typed literals, refer to the
ABAP Keyword Documentation and the Typed Literals in ABAP SQL section of the Data
Types and Data Objects cheat sheet.
o Regarding host expressions: Structures and internal tables are
possible as host expressions for statements modifying the
content of database tables as shown further down.
- See more information
here.
Example demonstrating possible operands:
DATA upto TYPE i VALUE 3.
"The following clause shows all options having the same effect
UP TO 3 ROWS. "Untyped numeric literal
"UP TO int4`3` ROWS. "Typed numeric literal
"UP TO @upto ROWS. "Host variable
"UP TO @( 10 - 7 ) ROWS. "Host expression
SQL Expressions
- Expressions in an ABAP SQL statement that are passed to the database
system for evaluation.
- For example, SQL expressions can be specified as columns in the
SELECT list as demonstrated in most of the following examples.
Find information on more possible positions and general information
on SQL expressions
here
and the subtopics there.
Elementary Expressions
- An elementary expression represents one of the four mentioned
operands above: A value from the database (the column name) or
values from an ABAP program passed to the database (literal, host
variable or host expression).
- As an example, see the SELECT list in the example above.
- See more information
here.
SQL Functions
- You can use built-in functions in ABAP SQL.
- Result: Value with the associated dictionary type.
- Arguments of the functions: Cover one or more SQL expressions.
- See more information
here.
Example: Numeric functions
SELECT SINGLE
carrname,
"Result: Largest integer value not greater than the specified value
"Result: 1
floor( decfloat34`1.333` ) AS floor,
FROM zdemo_abap_carr
WHERE carrid = 'AA'
INTO @DATA(numeric_functions).
"First letter of a word -> uppercase, all other letters -> lowercase;
"note that a space and other special characters means a new word.
"Result: Http://Www.Lufthansa.Com
initcap( url ) AS initcap,
"Extracts a string with the length specified starting from the right
"Result: hansa
right( carrname, 5 ) AS right,
"All trailing characters that match the character of the 2nd argument
"are removed; trailing blanks are removed, too
"Result: Lufthans
rtrim( carrname, 'a' ) AS rtrim,
FROM zdemo_abap_carr
WHERE carrid = 'LH'
INTO @DATA(string_functions).
<p align="right"><a href="#top">⬆� back to top</a></p>
Example: Special functions
SELECT SINGLE
carrid,
"Conversion functions
"When used: Special conversions that cannot be handled in a general
"CAST expression
FROM zdemo_abap_carr
INTO @DATA(special_functions).
"Minimum value
MIN( fltime ) AS min,
FROM zdemo_abap_flsch
WHERE carrid = 'LH'
GROUP BY carrid
INTO TABLE @DATA(agg_exp).
- Cast
expressions
to convert the value of operands to a dedicated dictionary type.
Note that there are special conversion
rules.
- String
expressions
using the operator && to concatenate character strings.
- Case
distinctions
to carry out either a simple (comparison of the values of a
dedicated operand) or complex (searched case; evaluation of multiple
logical expressions) case distinction.
The following example demonstrates the expressions mentioned above:
SELECT SINGLE
carrid,
"Arithmethic expressions
"operators + - *
"Note that / is not allowed in integer expressions as the one below
( 1 + 2 ) * 3 AS calc,
"Case distinction
"Simple case distinction
"The expression compares the values of an operand with other
"operands. Result: The first operand after THEN for which the
"comparison is true. If no matches are found, the result specified
"after ELSE is selected.
CASE currcode
WHEN 'EUR' THEN 'A'
WHEN 'USD' THEN 'B'
ELSE 'C'
END AS case_simple,
FROM zdemo_abap_carr
WHERE carrid = 'AA'
INTO @DATA(more_sql_expr).
Window Expressions
How window expressions work:
- Define a subset of the result set (i. e. the
"window")
of a database query that implements ABAP SQL
- Apply a window
function -
which evaluates the rows of the window and which can, for example,
be an aggregate
function
like
AVG
to determine the average value - to the result set
- I. e. a window is constructed by the rows of the result set for
which all the window functions have the same result; a value is then
determined for the rows of a window
Setup of a statement with window expressions:
- Window function, e. g. an aggregate function like AVG,
followed by OVER( ... ) (the content in the parentheses
defines the "window")
- The content in the parentheses can contain the following additions:
o Optional PARTITION BY: Defines the windows using a
comma-separated list of SQL
expressions;
the window function is calculated for the rows of this window;
note that if the addition is not specified, the window comprises
all rows of the result set
o Optional ORDER BY: Introduces both an order (you can
use ASCENDING and DESCENDING) and a frame
(as outlined below) within the current window, which further
restricts the rows for which the window function is calculated
o A window frame, which stands for a subset of rows inside a
window, can optionally be defined if ORDER BY is
specified; there are 3 options to define the starting and ending
frame boundaries (see the example)
See more information on window expressions and the syntax
here.
Examples:
"Example 1: A simple window is constructed in the OVER clause;
"window functions - here aggregate functions - are applied
SELECT carrid, currency,
SUM( paymentsum ) OVER( PARTITION BY carrid ) AS sum,
AVG( price AS DEC( 14,2 ) ) OVER( PARTITION BY carrid ) AS avg,
MAX( price ) OVER( PARTITION BY carrid ) AS max
FROM zdemo_abap_fli
ORDER BY carrid
INTO TABLE @DATA(win).
"Example 2:
SELECT carrid, currency, fldate,
"Sorts the rows by some columns and counts the number of rows from
"the first row of the window to the current row.
COUNT( * ) OVER( ORDER BY currency, fldate
ROWS BETWEEN
"UNBOUNDED PRECEDING: frame starts at the first row of the window
UNBOUNDED PRECEDING
"CURRENT ROW: determines starting or ending at the current row;
here, it ends
AND CURRENT ROW ) AS count1,
"Sorts the rows by some columns and counts the number of rows from
"the current row to the last row of the window.
"The result is reverse numbering.
COUNT( * ) OVER( ORDER BY currency, fldate
ROWS BETWEEN CURRENT ROW
UNBOUND FOLLOWING:
"Determines the ending frame boundary, this addition specifies the
last row of the window
AND UNBOUNDED FOLLOWING ) AS count_reverse,
"Sorts the rows by some columns and calculates the rolling averages
"of a subset of rows from column price. The subset consists of the
"current row plus one preceding and one following row. Another use
"case as below example that uses prices would be that, for example,
"you can calculate the 3-day-average temperature for every day from
"a list of temperature data.
AVG( price AS DEC( 14,2 ) ) OVER( ORDER BY currency, fldate
ROWS BETWEEN
"n PRECEDING: for both start and end of frame; frame to start/end n rows above
the current row
1 PRECEDING
"n FOLLOWING: for both start and end of frame; frame to start/end n rows
beneath the current row
AND 1 FOLLOWING ) AS avg
FROM zdemo_abap_fli
INTO TABLE @DATA(result).
SQL Conditions
You can formulate conditions in ABAP SQL statements, i. e. logical
expressions,
especially in the WHERE clause to restrict the result. Note
that without a WHERE clause, all rows are respected for the
operation.
See below a selection of the operators that are possible when specifying
conditions. For more information, see the subtopics of the SQL
Conditions
topic.
Operator Meaning
=, EQ The content of two operands is equal.
<>, NE The content of two operands is not equal.
<, LT The content of one operand is less than the content of the other operand.
>, GT The content of one operand is greater than the content of the other operand.
<=, LE The content of one operand is less than or equal to the content of the other operand.
>=, GE The content of one operand is greater than or equal to the content of the other
operand.
... [NOT] The value of an operand is (not) between the value of the two other operands.
BETWEEN ...
AND ...
... [NOT] The content of an operand matches (does not match) a specified pattern. The pattern
LIKE ... can be specified by using wildcard characters. % stands for any character string,
including an empty string. _ stands for any character.
... IS [NOT] The value of an operand is (not) the initial value of its built-in dictionary type.
INITIAL ...
... EXISTS Checks the result set of a subquery. The expression is true if the result set contains at
... least one row. See more information here.
... [NOT] IN Checks whether the operands on the left side match a value from a set of values
... specified in parentheses. On the left side, a single operand or an operand list are
possible. On the right side, a comma-separated lists or subqueries can be specified. It is
also possible to specify a ranges table to evaluate ranges conditions.
... IS [NOT] Checks whether the value of an operand is (not) the null value. Find more information
NULL ... in the code snippet and in the ABAP Keyword Documentation.
💡 Note<br>
You can combine multiple logical expressions into one
logical expression using AND or OR. To further
detail out the desired condition, expressions within parentheses are
possible.
Examples:
"---- SQL conditions demonstrated with the WHERE clause ----
"Note:
"- For most of the self-contained examples, an internal table is used as the
" data source of SELECT statements to work with simple data.
"- For some examples that are covered, such as subqueries, demo database tables
" from the cheat sheet repository are used in addition.
"- Dynamic specifications are also possible. They are not covered here. See
" the Dynamic Programming cheat sheet.
"---- Types and internal table to work with in the examples ----
"Note: You cannot use type string columns in WHERE conditions.
TYPES: BEGIN OF demo_struc,
id TYPE i,
name TYPE c LENGTH 15,
"name TYPE string,
END OF demo_struc.
DATA itab TYPE SORTED TABLE OF demo_struc WITH UNIQUE KEY id.
"Populating internal table with data to work with in the examples
itab = VALUE #( ( id = 1 name = 'bear' )
( id = 2 name = 'camel' )
( id = 3 name = 'rabbit' )
( id = 4 name = 'zebra' )
( id = 5 name = 'dog' )
( id = 6 name = 'deer' )
( id = 7 name = 'squirrel' )
( id = 8 name = 'cheetah' )
( id = 9 name = 'elephant' )
( id = 10 name = 'donkey' )
( id = 11 name = 'fish' )
( id = 12 name = 'sheep' ) ).
"---- =, <>, >, >= (as a selection of possible comparison operators) ----
SELECT id FROM @itab AS tab WHERE name = 'bear' INTO TABLE @DATA(it). "1
SELECT id FROM @itab AS tab WHERE name <> 'bear' INTO TABLE @it. "2-12
SELECT id FROM @itab AS tab WHERE id > 10 INTO TABLE @it. "11,12
SELECT id FROM @itab AS tab WHERE id >= 10 INTO TABLE @it. "10,11,12
"In the following example, the resulting table is initial. One of the expressions
"in parentheses is false (AND is used between the expressions in parentheses).
"In contrast, the example below returns an entry because of using OR.
SELECT id FROM @itab AS tab
WHERE ( id = 1 AND name = 'bear' )
AND ( id = 20 AND name = 'camel' )
INTO TABLE @it.
"Deleting the entry because it is not relevant for the further examples.
DELETE itab INDEX 13.
"Negation NOT IN; note to use host variables/expressions for local/global data
objects
DATA(animal) = 'sheep'.
SELECT id FROM @itab AS tab
WHERE name NOT IN ( 'fish', @animal )
INTO TABLE @it. "1-10
"In the following example, the subquery reads data from a demo database table.
"For a representative result, the table is cleared, and then filled with 'suitable'
"data sets.
DELETE FROM zdemo_abap_tab1.
MODIFY zdemo_abap_tab1 FROM TABLE @( VALUE #( ( key_field = 11 num1 = 11 )
( key_field = 12 num1 = 12 )
( key_field = 13 num1 = 13 )
( key_field = 14 num1 = 14 ) ) ).
SELECT id FROM @itab AS tab WHERE id IN @rangestab INTO TABLE @it. "1,2,3,10,11,12
"You cannot use logical operators such as CP (conforms to pattern) in the WHERE
clause.
"In a ranges table, they are possible.
"Note:
"- Regarding CP: * (any character sequence), + (any character), # (escape character)
"- An equivalent example above uses the LIKE addition.
DATA rt TYPE RANGE OF demo_struc-name.
rt = VALUE #( ( sign = 'I' option = 'CP' low = '*ee*' ) "ee in a string
( sign = 'I' option = 'CP' low = '+o*' ) ). "o in second position
SELECT name FROM @itab AS tab
WHERE name IN @rt
INTO TABLE @names. "dog,deer,cheetah,donkey,sheep
"The following example visualizes the null values. The INDICATORS addition of the
"INTO clause is used to specify indicators such as the null indicator. In the
"example, an appropriate target table is defined to also store information about
"which columns of the result set contain the null value and which do not.
"For more information on the syntax, refer to the ABAP Keyword Documentation.
TYPES: BEGIN OF st4null,
BEGIN OF s2,
key_field TYPE zdemo_abap_tab2-key_field,
char2 TYPE zdemo_abap_tab1-char2,
END OF s2,
BEGIN OF nulls,
key_field TYPE c LENGTH 1,
char2 TYPE c LENGTH 1,
END OF nulls,
END OF st4null.
DATA joined_tab_w_null_ind TYPE TABLE OF st4null WITH EMPTY KEY.
ENDIF.
💡 Note<br>
There are more join variants available. See the ABAP
Keyword Documentation on
joins
for more information.
Merging the result sets of multiple queries into a single result set using the set operator UNION. In this
case, the rows of the result set of the query after UNION are inserted into the result set of the query in
front of UNION.
SELECT FROM dbtab1
FIELDS ...
WHERE ...
UNION
SELECT FROM dbtab2
FIELDS ...
WHERE ...
INTO ...
💡 Note<br>
- Each CTE must be used at least once, either in another CTE or in the
main query. The main query must access at least one CTE.
- The result set of a CTE never has a client
column.
- See more information in this topic
and further options and additions when using CTEs in the subtopics.
Example: The result sets of both common table expressions
+connections and +sum_seats are merged in the
subquery of the CTE +result in a join expression. An explicit
name list assigns names to the resulting columns. These names are used
in the main query to sort the results. For each flight connection of the
selected airline, the total number of occupied seats is stored in the
internal table.
WITH
+connections AS (
SELECT zdemo_abap_flsch~carrid, carrname, connid, cityfrom, cityto
FROM zdemo_abap_flsch
INNER JOIN zdemo_abap_carr
ON zdemo_abap_carr~carrid = zdemo_abap_flsch~carrid
WHERE zdemo_abap_flsch~carrid BETWEEN 'AA' AND 'JL' ),
+sum_seats AS (
SELECT carrid, connid, SUM( seatsocc ) AS sum_seats
FROM zdemo_abap_fli
WHERE carrid BETWEEN 'AA' AND 'JL'
GROUP BY carrid, connid ),
+result( name, connection, departure, arrival, occupied ) AS (
SELECT carrname, c~connid, cityfrom, cityto, sum_seats
FROM +connections AS c
INNER JOIN +sum_seats AS s
ON c~carrid = s~carrid AND c~connid = s~connid )
SELECT *
FROM +result
ORDER BY name, connection
INTO TABLE @DATA(result).
"Line is created inline using the VALUE operator as part of a host expression
INSERT dbtab FROM @( VALUE #( comp1 = ... comp2 = ... ) ).
"Inserting lines from a table declared inline using the VALUE operator
"as part of a host expression
INSERT dbtab FROM TABLE @( VALUE #( ( comp1 = ... comp2 = ... )
( comp1 = ... comp2 = ... ) ) ).
"ACCEPTING DUPLICATE KEYS addition: To avoid the runtime error mentioned above,
"all lines that would produce duplicate entries in the database table
"regarding the keys are discarded and sy-subrc is set to 4.
INSERT dbtab FROM TABLE @itab ACCEPTING DUPLICATE KEYS.
Using UPDATE
- Changes the content of one or more rows of a database table specified.
- Similar to INSERT, sy-subrc and sy-dbcnt are set.
"Changing content by overwriting entire rows based on a structure
UPDATE dbtab FROM @row.
UPDATE dbtab FROM @( VALUE #( comp1 = ... comp2 = ... ) ). "Using a host expression
ind_tab = VALUE #(
( comp1 = ... comp2 = ... comp_ind-comp2 = abap_true )
( comp1 = ... comp2 = ... comp_ind-comp2 = abap_true ) ).
Using MODIFY
- Inserts one or more rows into a database table specified or overwrites existing ones.
- As above, sy-subrc and sy-dbcnt are set.
"Inserting a single row into a database table or changing an existing row
MODIFY dbtab FROM @row.
Using DELETE
- Deletes one or more rows from a database table specified.
- As above, sy-subrc and sy-dbcnt are set.
"Variant DELETE FROM ...: Either all rows are deleted or restricted
- This topic serves as the entry point for topics about ABAP SQL in the ABAP Keyword
Documentation. For the full details, check the subtopics there, especially topics not covered in
this cheat sheet.
- There are RAP-specific variants of ABAP SQL statements that use the MAPPING FROM ENTITY
addition. Find more information here.
Executable Example
zcl_demo_abap_sql
💡 Note<br>
- The executable example covers the following topics, among others:
o Reading from database tables using SELECT
o Various additions to SELECT statements
o Changing data in database tables using INSERT, UPDATE, MODIFY and DELETE
o Excursions: Operands and expressions in ABAP SQL statements
- The steps to import and run the code are outlined here.
- Disclaimer