This cheat sheet focuses on WHERE conditions and explores various syntax options in ABAP statements that include WHERE for data filtering. This is relevant, for example, when retrieving data from a data source using ABAP SQL or when processing internal tables with ABAP statements. For all details and syntax options, refer to the ABAP Keyword Documentation. Several aspects and code snippets in this cheat sheet are also available in other cheat sheets.
Note
- Most examples in the cheat sheet use internal tables as data sources for ABAP SQL
SELECTstatements to have self-contained examples. UseSELECTwith internal tables as data sources only when SQL functionalities like joins exceed ABAP statements. For more details, refer to the Internal Tables cheat sheet. - Some examples also use artifacts from the ABAP cheat sheet repository. To check out these examples, ensure you have imported the ABAP cheat sheet repository into your system.
- Using logical expressions, you can limit the number of rows in the result set or those to be modified in a data source.
- A row is included in the result set or modified in the data source only if the logical expression is true.
- Certain restrictions apply to logical expressions. For example, columns of types like
stringandrawstringcannot be used. - Regarding data retrieval, columns in the logical expressions do not need to be part of the result set.
- You can combine multiple logical expressions using
ANDorOR. - Combining logical expressions in parentheses allows the refinement of conditions. Find more information here.
- Conditions can be specified in ABAP SQL statements in these contexts:
- Queries with
SELECT:WHEREandHAVINGclauses - Modifying operations:
WHEREclause inUPDATEandDELETEstatements
- Queries with
The following table provides an overview of SQL conditions. Note that the code snippets are from the executable example further down.
| Syntax Variant | Notes | Code Snippet |
|
|
The content of two operands is equal. |
SELECT id FROM @itab AS tab
WHERE animal = 'bear'
INTO TABLE @it.
SELECT id FROM @itab AS tab
WHERE animal EQ 'bear'
INTO TABLE @it. |
|
|
The content of two operands is not equal. |
SELECT id FROM @itab AS tab
WHERE animal <> 'bear'
INTO TABLE @it.
SELECT id FROM @itab AS tab
WHERE animal NE 'bear'
INTO TABLE @it. |
|
|
The content of the left operand is less than the content of the right operand. |
SELECT id FROM @itab AS tab
WHERE count < 15
INTO TABLE @it.
SELECT id FROM @itab AS tab
WHERE count LT 15
INTO TABLE @it. |
|
|
The content of the left operand is greater than the content of the right operand. |
SELECT id FROM @itab AS tab
WHERE count > 15
INTO TABLE @it.
SELECT id FROM @itab AS tab
WHERE count GT 15
INTO TABLE @it. |
|
|
The content of the left operand is less than or equal to the content of the right operand. |
SELECT id FROM @itab AS tab
WHERE count <= 15
INTO TABLE @it.
SELECT id FROM @itab AS tab
WHERE count LE 15
INTO TABLE @it. |
|
|
The content of the left operand is greater than or equal to the content of the right operand. |
SELECT id FROM @itab AS tab
WHERE count >= 15
INTO TABLE @it.
SELECT id FROM @itab AS tab
WHERE count GE 15
INTO TABLE @it. |
|
|
Combining multiple logical expressions into one logical expression using |
SELECT id FROM @itab AS tab
WHERE animal = 'bear' AND count = 5
INTO TABLE @it.
SELECT id FROM @itab AS tab
WHERE animal = 'kangaroo' OR count = 4
INTO TABLE @it.
SELECT id FROM @itab AS tab
WHERE ( animal = 'bear' AND count = 5 )
AND ( animal = 'lion' AND count = 20 )
INTO TABLE @it.
SELECT id FROM @itab AS tab
WHERE ( animal = 'bear' AND count = 5 )
OR ( animal = 'lion' AND count = 20 )
INTO TABLE @it.
SELECT id FROM @itab AS tab
WHERE count > 10
OR NOT ( animal = 'kangaroo' AND count = 8 )
INTO TABLE @it. |
|
|
The content of a single operand is compared with the result set of a scalar subquery using a comparison operator. Multiple subqueries can be included with |
"The following example assumes there is a single-row result set of the subquery.
SELECT id FROM @itab AS tab
WHERE count = ( SELECT key_field FROM zdemo_abap_tab1 WHERE num1 = 40 )
INTO TABLE @it.
"ALL addition
SELECT id FROM @itab AS tab
WHERE count > ALL ( SELECT key_field FROM zdemo_abap_tab1 )
INTO TABLE @it.
"ANY addition
SELECT id FROM @itab AS tab
WHERE count = ANY ( SELECT key_field FROM zdemo_abap_tab1 WHERE num1 <= 40 )
INTO TABLE @it.
"SOME addition (yields the same result as the previous statement)
SELECT id FROM @itab AS tab
WHERE count = SOME ( SELECT key_field FROM zdemo_abap_tab1 WHERE num1 <= 40 )
INTO TABLE @it. |
|
|
The content of the left operand is (not) between the value of the two other operands. The syntax variant is like specifying |
SELECT id FROM @itab AS tab
WHERE count BETWEEN 1 AND 10
INTO TABLE @it.
"Negation with NOT
SELECT id FROM @itab AS tab
WHERE count NOT BETWEEN 1 AND 10
INTO TABLE @it. |
|
|
Checks whether the content of the left operand matches (or does not match) a specified pattern. The pattern can be specified by using wildcard characters. |
SELECT animal FROM @itab AS tab
WHERE animal LIKE '%ee%'
OR animal LIKE '_e%'
INTO TABLE @DATA(animals).
"Negation with NOT
SELECT animal FROM @itab AS tab
WHERE animal NOT LIKE '_e%'
INTO TABLE @animals.
"ESCAPE addition
"The following example matches any character sequence followed
"by the % character.
SELECT animal FROM @itab AS tab
WHERE animal LIKE '%#%' ESCAPE '#'
INTO TABLE @animals. |
|
|
Checks whether the content of an operand is (not) the initial value of its built-in DDIC type. |
SELECT id FROM @itab AS tab
WHERE count IS INITIAL
INTO TABLE @it.
"Negation with NOT
SELECT id FROM @itab AS tab
WHERE count IS NOT INITIAL
INTO TABLE @it. |
|
|
Checks whether the content of the left operand is (not) contained in a set of a parenthesized, comma-separated list of values. You can also specify just one operand in the parentheses. |
SELECT id FROM @itab AS tab
WHERE animal IN ( 'elephant', 'gorilla', 'dog', 'snake' )
INTO TABLE @it.
"Negation with NOT
SELECT id FROM @itab AS tab
WHERE animal NOT IN ( 'chimpanzee', 'dog', 'snake' )
INTO TABLE @it.
"Blanks after the first parentheses and before the second are not mandatory.
"This also applies to blanks within the parentheses. However, choose either
"to use the blanks or not.
SELECT id FROM @itab AS tab
WHERE animal IN ('elephant','gorilla','dog','snake')
INTO TABLE @it. |
|
|
Checks whether the content of the left operand is (not) contained in the result set of a scalar subquery. Note that multiple subqueries can be included using |
SELECT id FROM @itab AS tab
WHERE count IN ( SELECT key_field FROM zdemo_abap_tab1 WHERE num1 <= 40 )
INTO TABLE @it.
"Negation with NOT
SELECT id FROM @itab AS tab
WHERE count NOT IN ( SELECT key_field FROM zdemo_abap_tab1 WHERE num1 <= 40 )
INTO TABLE @it. |
|
|
Checks whether each of the values of multiple operands in a parenthesized, comma-separated list on the left side of |
SELECT id FROM @itab AS tab
WHERE ( id, animal ) IN ( ( 1, 'bear' ), ( 3, 'giraffe' ), ( 987, 'flamingo' ), ( 2, 'dog' ) )
INTO TABLE @it. |
|
|
Checks whether each value of multiple operands in a parenthesized, comma-separated list on the left side of |
SELECT id FROM @itab AS tab
WHERE ( id, count ) IN ( SELECT key_field, num1 FROM zdemo_abap_tab1 )
INTO TABLE @it. |
|
|
Checks whether the operand on the left side of |
DATA rangestab TYPE RANGE OF i.
"Value range between 1 and 10
rangestab = VALUE #( ( sign = 'I' option = 'BT' low = 1 high = 10 ) ).
SELECT id FROM @itab AS tab
WHERE count IN @rangestab
INTO TABLE @it.
"Value range: Lower than 5 + greater than or equal to 25
rangestab = VALUE #( ( sign = 'I' option = 'LT' low = 5 )
( sign = 'I' option = 'GE' low = 25 ) ).
SELECT id FROM @itab AS tab
WHERE count IN @rangestab
INTO TABLE @it. |
|
|
Checks the result of a subquery. The comparison is true if the result set contains at least one row. Note that data source fields specified in the subquery are not relevant. You may also just use a single literal representing a column. Note that multiple subqueries can be included using |
SELECT id FROM @itab AS tab
WHERE EXISTS ( SELECT 'X' FROM zdemo_abap_tab1 WHERE key_field = tab~id )
INTO TABLE @it. |
|
|
Checks whether the value of an operand is (not) the null value. Find more information in the executable example (which also includes the |
SELECT tab2~key_field, tab1~char2
FROM zdemo_abap_tab2 AS tab2
LEFT OUTER JOIN zdemo_abap_tab1 AS tab1 ON tab1~char1 = tab2~char1
WHERE tab1~char1 IS NULL
INTO TABLE @DATA(joined_tab).
"Negation IS NOT NULL
SELECT tab2~key_field, tab1~char2
FROM zdemo_abap_tab2 AS tab2
LEFT OUTER JOIN zdemo_abap_tab1 AS tab1 ON tab1~char1 = tab2~char1
WHERE tab1~char1 IS NOT NULL
INTO TABLE @joined_tab. |
|
|
Dynamic |
DATA(dynamic_where_clause) = `count > 15`.
SELECT id FROM @itab AS tab
WHERE (dynamic_where_clause)
INTO TABLE @it.
DATA(dyn_where_cl_as_tab) = VALUE string_table( ( `animal = 'kangaroo'` )
( `OR` )
( `count = 4` ) ).
SELECT id FROM @itab AS tab
WHERE (dyn_where_cl_as_tab)
INTO TABLE @it. |
Note
- Some subqueries in the syntax variants must be scalar subqueries. This means that the subquery returns a single-column result set. The
SELECTlist of the subquery must only contain a single element. - See this topic in the ABAP Keyword Documentation what can be specified as operands on the left and right side.
- The comparisons are done on the database, so there is no type conversions in ABAP beforehand. Note that platform-dependent conversion behavior may be applied. For SAP HANA Platform-related conversion rules, see this topic.
- See here a list of comparable types in the ABAP Dictionary.
- The following ABAP statements and constructor operators, used for processing internal tables, allow
WHEREconditions to be specified:LOOP ATREAD TABLEDELETE- Table filtering using the
FILTERconstructor operator FORloops in the context of table comprehensions and reductions (using, e.g., theVALUE- having the exact semantics asLOOP AT; theNEWoperator is also possible - andREDUCEoperators)
- The variety of syntax options differs from that available for ABAP SQL statements.
- The following syntax options are supported for
WHEREconditions for these:LOOP AT,READ TABLE(searches for the first line matching theWHEREcondition in the internal table),DELETE, andFORloops.- Comparison expressions
- Comparison operators as above (
=,EQ,<>,NE,>,GT,<,LT,>=,GE,<=,LE) - Comparison operators for character-like data types
CO,CN,CA,NA,CS,NS,CP,NP(see the String Processing cheat sheet) - Comparison operators for byte-like data types, e.g.
BYTE-COand others; and bit patterns [NOT] BETWEEN ... AND ...[NOT] IN ranges_table
- Comparison operators as above (
- Predicate expression
IS [NOT] INITIALIS [NOT] BOUNDIS [NOT] INSTANCE OF
- Dynamic
WHEREconditions: Any character-like data object or standard table with character-like line type containing logical expressions can be specified within a pair of parentheses. - Logical expressions can be combined with
AND,OR,EQUIV, also using parenthesized expressions.
- Comparison expressions
FILTERconstructor operator- To use the
FILTERoperator, certain prerequisites must be met:- The source table needs at least one sorted or hash key.
- If the table lacks such a primary table key, a secondary table key must be available.
- Only table key columns can be compared with single values in the
WHEREcondition. The filtering can also be based on a filter table. - The operator has restricted syntax options:
- For source tables with hash keys: Only
=is supported for key component comparisons. - For source tables with sorted keys:
=,EQ,<>,NE,<,LT,>,GT,<=,LE,>=,GEare supported (at least the initial part of the key must be specified). IS [NOT] INITIALis supported.- Multiple comparisons can only be combined using
AND.
- For source tables with hash keys: Only
- To use the
zcl_demo_abap_where_conditions
Note
- The steps to import and run the code are outlined here.
- Disclaimer