MODIFYITABFROMWAWHERE11

作者:ABAP三叔 约 3 分钟阅读 更新日期:2025-03-02 1 年前更新 标签:ABAP, Data Engineering, SD, 开发, 数据工程, 销售分销
MODIFYITABFROMWAWHERE11 - 封面图
MODIFYITABFROMWAWHERE11 - 封面图
图片来源:微信公众号(如有侵权请联系删除)

前言

更新内表中所有行的某个字段值,比如全选标记字段,有时候很想用

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 ).
MODIFYITABFROMWAWHERE11 - 封面图
MODIFYITABFROMWAWHERE11 - 封面图

方法二

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,所以会更新到所有行。

来源说明:本文内容由「MODIFYITABFROMWAWHERE11.md」整理生成,仅用于内部技术分享与学习交流。

ABAP Cheat Sheet 补充内容

以下内容整理自 SAP 官方 ABAP 速查表(主题:WHERE Conditions),作为本文的扩展补充资料。

WHERE Conditions in ABAP Statements

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 SELECT statements to have self-contained examples. Use SELECT with 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.

⬆️ back to top

WHERE Conditions in ABAP SQL Statements

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

... a = b ...
... a EQ b ...

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.  

... a <> b ...
... a NE b ...

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.

... a < b ...
... a LT b ...

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.

... a > b ...
... a GT b ...

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.

... a <= b ...
... a LE b ...

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.

... a >= b ...
... a GE b ...

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.

... AND [NOT] ...
... OR [NOT] ...
... ( ... ) ...

Combining multiple logical expressions into one logical expression using AND or OR (inlcuding negations). To further detail out the desired condition, expressions within parentheses are possible.

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.  

... a [=|<>|>|<|...] [ALL|ANY|SOME] ( SELECT ... ) ...

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 UNION, INTERSECT, or EXCEPT. The additions ALL, ANY, and SOME can only be omitted if the subquery result set contains a single row. Otherwise, an exception occurs with a multi-row result set. One of the additions must be specified in the case of multi-row result set. For ALL, the expression is true if the comparison holds for all rows in the result set. For ANY or SOME, the expression is true if the comparison holds for at least one row. Using = or EQ with ANY or SOME is similar to using IN (SELECT ...).

"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.

... a [NOT] BETWEEN b AND c ...

The content of the left operand is (not) between the value of the two other operands. The syntax variant is like specifying ... [NOT] ( a >= b AND a <= c ) ....

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.       

... a [NOT] LIKE b [ESCAPE c] ...

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. % stands for any character string, including an empty string. _ stands for any character.
Using the ESCAPE addition, you can specify a single-character escape character of length 1 (e.g. #) in front of a wildcard character or the escaped character itself. For example, to search for the pattern 100%, you may use an expression such as the the following: ... LIKE '100#%' ESCAPE '#' ....

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. 

... a IS [NOT] INITIAL ...

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. 

... a [NOT] IN (b, c, ...)...

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.       

... a [NOT] IN ( SELECT ... ) ...

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 UNION, INTERSECT, EXCEPT.

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. 

... ( a, b, ... ) IN ( ( d, e, ... ) ( f, g, ...) ... ) ...

Checks whether each of the values of multiple operands in a parenthesized, comma-separated list on the left side of IN matches value tuples in the same place specified in parentheses on the right side of IN. Unlike the syntax option ... a [NOT] IN (b, c, ...) ..., this syntax option allows SQL expressions on the right side of IN. Note that a negation with NOT is not supported.

SELECT id FROM @itab AS tab
  WHERE ( id, animal ) IN ( ( 1, 'bear' ), ( 3, 'giraffe' ), ( 987, 'flamingo' ), ( 2, 'dog' ) )
  INTO TABLE @it. 

... ( a, b, ... ) IN ( SELECT ... ) ...

Checks whether each value of multiple operands in a parenthesized, comma-separated list on the left side of IN matches the content of a subquery result set. The result set must contain the same number of elements as specified in the parentheses on the left side of IN. As above, the position of elements in the result set is relevant. Note that multiple subqueries can be included using UNION, INTERSECT, EXCEPT, and that a negation with NOT is not supported.

SELECT id FROM @itab AS tab
  WHERE ( id, count ) IN ( SELECT key_field, num1 FROM zdemo_abap_tab1 )
  INTO TABLE @it. 

... a [NOT] IN @ranges_table ...

Checks whether the operand on the left side of IN matches (or does not match) ranges conditions specified in a ranges table. Note that the operators CP and NP are transformed into LIKE conditions (as a consequence, the conditions for CP and NP are case-sensitive).

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.

... EXISTS ( SELECT ... ) ...

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 UNION, INTERSECT, EXCEPT.

SELECT id FROM @itab AS tab
  WHERE EXISTS ( SELECT 'X' FROM zdemo_abap_tab1 WHERE key_field = tab~id )
  INTO TABLE @it.

... a IS [NOT] NULL ...

Checks whether the value of an operand is (not) the null value. Find more information in the executable example (which also includes the INDICATORS NULL STRUCTURE addition to the INTO clause) and in the ABAP Keyword Documentation.

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_where_clause) ...

Dynamic WHERE condition specified as parenthesized data objects. These data objects should contain the syntax of a logical expression. As dynamic_where_clause, a character-like data object or a standard table with character-like line type is expected. The syntax is not case-sensitive. For more information, see the Dynamic Programming cheat sheet, also with respect to potential security risks regarding dynamic specifications.

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 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.

⬆️ back to top

WHERE Conditions in ABAP Statements for Processing Internal Tables

⬆️ back to top

Executable Example

zcl_demo_abap_where_conditions

[!NOTE] - The steps to import and run the code are outlined here. - Disclaimer
来源说明:本节内容来源:SAP-samples/abap-cheat-sheets (GitHub),原作者 SAP。原 Markdown 文件:31_WHERE_Conditions.md。版权归原作者所有。