前言
更新内表中所有行的某个字段值,比如全选标记字段,有时候很想用
WHERE 1 = 1 ,ABAP中报错该如何处理?
下面介绍两种方式
方法一
REPORT ztest.TYPES: BEGIN OF ty_item, id TYPE i, sel TYPE abap_bool, END OF ty_item.DATA: lt_data TYPE STANDARD TABLE OF ty_item, ls_data TYPE ty_item.
DO 10 TIMES. INSERT VALUE #( id = sy-index ) INTO lt_data INDEX sy-index.ENDDO.ls_data-sel = abap_true.MODIFY lt_data FROM ls_data TRANSPORTING sel WHERE id IS NOT INITIAL.cl_demo_output=>display( lt_data ).方法二
REPORT ztest.TYPES: BEGIN OF ty_item, id TYPE i, sel TYPE abap_bool, END OF ty_item.DATA: lt_data TYPE STANDARD TABLE OF ty_item, ls_data TYPE ty_item.
DO 10 TIMES. INSERT VALUE #( id = sy-index ) INTO lt_data INDEX sy-index.ENDDO.ls_data-sel = abap_true.MODIFY lt_data FROM ls_data TRANSPORTING sel WHERE ('').cl_demo_output=>display( lt_data ).注意看第13行的不同点,展示 结果跟方法一是一样的.
解释: where 后面括号内括起来的是一个动态的where条件,当为空的时候,相当于不加限制即1 = 1,所以会更新到所有行。
以下内容整理自 SAP 官方 ABAP 速查表(主题:WHERE Conditions),作为本文的扩展补充资料。
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 SQLSELECTstatements 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.
string and rawstring cannot be used.AND or OR.SELECT: WHERE and HAVING clausesWHERE clause in UPDATE and DELETE statementsThe 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. |
|
|
|
The content of two operands is not equal. |
|
|
|
The content of the left operand is less than the content of the right operand. |
|
|
|
The content of the left operand is greater than the content of the right operand. |
|
|
|
The content of the left operand is less than or equal to the content of the right operand. |
|
|
|
The content of the left operand is greater than or equal to the content of the right operand. |
|
|
|
Combining multiple logical expressions into one logical expression using |
|
|
|
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 content of the left operand is (not) between the value of the two other operands. The syntax variant is like specifying |
|
|
|
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. |
|
|
|
Checks whether the content of an operand is (not) the initial value of its built-in DDIC type. |
|
|
|
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. |
|
|
|
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 |
|
|
|
Checks whether each of the values of multiple operands in a parenthesized, comma-separated list on the left side of |
|
|
|
Checks whether each value of multiple operands in a parenthesized, comma-separated list on the left side of |
|
|
|
Checks whether the operand on the left side of |
|
|
|
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 |
|
|
|
Checks whether the value of an operand is (not) the null value. Find more information in the executable example (which also includes the |
|
|
|
Dynamic |
|
[!NOTE] - Some subqueries in the syntax variants must be scalar subqueries. This means that the subquery returns a single-column result set. The SELECT list 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.
WHERE conditions to be specified:LOOP ATREAD TABLEDELETE FILTER constructor operatorFOR loops in the context of table comprehensions and reductions (using, e.g., the VALUE - having the exact semantics as LOOP AT; the NEW operator is also possible - and REDUCE operators)WHERE conditions for these: LOOP AT, READ TABLE (searches for the first line matching the WHERE condition in the internal table), DELETE, and FOR loops. =, EQ, <>, NE, >, GT, <, LT, >=, GE, <=, LE)CO, CN, CA, NA, CS, NS, CP, NP (see the String Processing cheat sheet)BYTE-CO and others; and bit patterns[NOT] BETWEEN ... AND ...[NOT] IN ranges_tableIS [NOT] INITIALIS [NOT] BOUNDIS [NOT] INSTANCE OFWHERE conditions: Any character-like data object or standard table with character-like line type containing logical expressions can be specified within a pair of parentheses. AND, OR, EQUIV, also using parenthesized expressions.FILTER constructor operatorFILTER operator, certain prerequisites must be met:WHERE condition. The filtering can also be based on a filter table.= is supported for key component comparisons.=, EQ, <>, NE, <, LT, >, GT, <=, LE, >=, GE are supported (at least the initial part of the key must be specified).IS [NOT] INITIAL is supported.AND.zcl_demo_abap_where_conditions
[!NOTE] - The steps to import and run the code are outlined here. - Disclaimer
31_WHERE_Conditions.md。版权归原作者所有。