SELECT SINGLE 和 SELECT ENDSELECT的区别!

今天碰到一个小问题,当我验证SELECT执行是否成功时,使用select endselect就可以,而用select single就不可以。
AT SELECTION-SCREEN.
* DO 1 TIMES.

SELECT SINGLE NAME1
FROM T001W
INTO PLT_NAME
WHERE T001W~WERKS = S_PLT.
IF SY-SUBRC = 4.
MESSAGE E027(ZPPMRP) WITH S_PLT.
ENDIF.
* ENDDO.
*ENDSELECT.



S_PLT是屏幕screen的字段
SELECTION-SCREEN BEGIN OF BLOCK BLOCK1 .
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS : P_PLT AS CHECKBOX DEFAULT 'X'. "P_PLANT
SELECTION-SCREEN COMMENT (19) TEXT-C01.
SELECT-OPTIONS : S_PLT FOR T001W-WERKS OBLIGATORY NO-EXTENSION NO INTERVALS.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK BLOCK1.



起因,条件,使用SELECT SINGLE,当判断条件为SY-SUBRC = 4时,条件成立,执行不成功,会一直执行if条件中的代码,即MESSAGE E027(ZPPMRP) WITH S_PLT.
而使用select endselect时候,当判断条件为SY-SUBRC <> 0时,则执行成功,即此时的SY-SUBRC = 0。

备注:按照下面的解释,当
SELECT sets SY-SUBRC to 0 if at least one line was read, otherwise to 4, or possibly 8 in SELECT SINGLE FOR UPDATE.
即,当SY-SUBRC = 0时,至少有一行数据;
其他情况为SY-SUBRC = 4;
另外当SY-SUBRC =8时,只有在SELECT SINGLE FOR UPDATE的时候,这个我也解释不好。


Haha, I had this information so thought of sharing with you all.

Values of SY-SUBRC on different ABAP statements

'sy-subrc' is a return code, set by the following ABAP statements.
As a rule, if SY-SUBRC = 0, the statement was executed successfully.

ASSIGN sets SY-SUBRC to 0 if the field symbol assignment was possible, otherwise to 4.
AUTHORITY-CHECK sets SY-SUBRC to 0 if the user has the required authorization, otherwise to 4, 8, 12, 16, 24, 28, 32, or 36 depending on the cause of the authorization failure.
CALL DIALOG with USING sets SY-SUBRC to 0 if the processing is successful, otherwise to a value other than 0.
CALL FUNCTION sets SY-SUBRC in accordance with the defined exception handling.
CALL METHOD sets SY-SUBRC in accordance with the defined exception handling.
CALL SELECTION-SCREEN sets SY-SUBRC to 0 if the user chooses Enter or Execute, and 4 if the user chooses Cancel.
CALL TRANSACTION with USING sets SY-SUBRC to 0 if the processing is successful, otherwise to a value other than 0.
CATCH SYSTEM-EXCEPTIONS sets SY-SUBRC after the ENDCATCH statement if a system exception occurs. The value is set in the program.
COMMIT WORK sets SY-SUBRC to 0. COMMIT WORK AND WAIT sets SY-SUBRC to 0 if the update is successful, otherwise to a value other than 0.
COMMUNICATION INIT DESTINATION ... RETURNCODE sets SY-SUBRC as specified.
CONCATENATE sets SY-SUBRC to 0 if the result fits into the target variable, otherwise to 4.
CREATE OBJECT sets SY-SUBRC if the exceptions of the instance constructor are handled in the program.
CREATE OBJECT in OLE2 sets SY-SUBRC to 0 if an external object could be created, otherwise to 1, 2, or 3, depending on the cause.
DELETE sets SY-SUBRC to 0 if the operation is successful, otherwise to 4 or another value other than 0, depending on the cause.
DEMAND ... MESSAGES INTO sets SY-SUBRC to 0 if the message table is empty, otherwise to a value other than 0.
DESCRIBE LIST sets SY-SUBRC to 0 if the line or list exists, otherwise to 4 or 8.
EXEC SQL - ENDEXEC sets SY-SUBRC to 0 in nearly all cases. It does, however, set SYSUBRC to 4 if no entry is read in a FETCH statement.
FETCH sets SY-SUBRC to 0 if at least one line was read, otherwise to 4.
GENERATE SUBROUTINE POOL sets SY-SUBRC to 0 if the generation was successful, otherwise to 8.
GET CURSOR sets SY-SUBRC to 0 if the cursor is correctly positioned, otherwise to 4.
GET PARAMETER sets SY-SUBRC to 0 if a corresponding value exists in SAP memory, otherwise to 4.
IMPORT sets SY-SUBRC to 0 if the import is successful, otherwise to 4.
INSERT sets SY-SUBRC to 0 if the operation is successful, otherwise to 4.
LOAD REPORT sets SY-SUBRC to 0 if the operation is successful, otherwise to 4 or 8 depending on the cause of the error.
LOOP sets SY-SUBRC to 0 if there is at least one pass through the extract. Otherwise, it is set to a value other than 0.
LOOP AT sets SY-SUBRC to 0 if there is at least one loop pass through the internal table, otherwise to 4.
MODIFY sets SY-SUBRC to 0 if the operation is successful, otherwise to 4.
MODIFY LINE sets SY-SUBRC to 0 if a line in the list was changed, otherwise it sets it to a value other than 0.
MODIFY sets SY-SUBRC to 0 if the operation is successful, otherwise to 4.
OLE2 Automation, executed successfully, otherwise 1, 2, 3, or 4, depending on the cause of the error.
OPEN DATASET sets SY-SUBRC to 0 if the file could be opened, otherwise to 8.
Open SQL statements set SY-SUBRC to 0 if the operation is successful, otherwise to a value other than 0.
OVERLAY sets SY-SUBRC to 0 if at least one character is overlaid, otherwise to 4.
READ DATASET sets SY-SUBRC to 0 if the read operation was successful, otherwise to 4 or 8, depending on the cause of the error.
READ LINE sets SY-SUBRC to 0 if a list line exists, otherwise to a value other than 0.
READ TABLE sets SY-SUBRC to 0 if table lines are found, otherwise to 2, 4, or 8, depending on the context and cause of the error.
REPLACE sets SY-SUBRC to 0 if the search string was replaced, otherwise to a value other than 0.
SCROLL sets SY-SUBRC to 0 if the scrolling within the list was successful, otherwise to 4 or 8, depending on the cause.
SEARCH sets SY-SUBRC to 0 if the search string was found, otherwise to 4.
SELECT sets SY-SUBRC to 0 if at least one line was read, otherwise to 4, or possibly 8 in SELECT SINGLE FOR UPDATE.
SET COUNTRY sets SY-SUBRC if the country code exists in table T005X, otherwise to 4.
SET BIT sets SY-SUBRC to 0 if the bit could be set, otherwise to a value other than 0.
SET TITLEBAR sets SY-SUBRC to 0 if the title exists, otherwise to 4.
SHIFT ... UP TO sets SY-SUBRC to 0 if the position could be found within the string, otherwise to 4.
SPLIT sets SY-SUBRC to 0 if the sizes of the target fields are adequate, otherwise to 4.
UPDATE sets SY-SUBRC to 0 if the operation is successful, otherwise to 4.
WRITE ... TO sets SY-SUBRC to 0 if the assignment is successful, otherwise to 4.
ABAP中的SELECT语句用于从数据库中选择数据。SELECT SINGLE语句用于选择一条数据,如果根据查询条件可以得到多条数据,则必须使用ENDSELECT来结束语句。然而,应尽量避免使用SELECT SINGLE,因为在ABAP的语法检查中会有警告。可以使用其他方法来代替,例如使用SELECT ... INTO TABLE来选择数据并存储在内表中。\[1\] 在ABAP中,还可以使用SELECT语句结合CASE语句来进行条件选择。例如,可以使用SELECT ... SUM ... CASE ... END来计算某个字段的总和,并根据条件进行加减操作。\[2\] 在使用SELECT语句时,需要注意以下几点: 1. 在使用前必须判断内表参数是否非空,否则SELECT语句相当于没有WHERE条件限制,将取出表中所有数据。 2. 搜索出的数据必须包含该表的全部主键,否则可能无法精确匹配。 3. 使用内表作为条件时,内表不应太大,因为每行数据都会增加一个WHERE条件,太多的条件会影响效率。\[3\] 总之,ABAP中的SELECT语句是用于从数据库中选择数据的重要语句,可以根据需要使用不同的语法结构来实现不同的查询需求。 #### 引用[.reference_title] - *1* [ABAP数据库操作系列之操作语句讲解Select](https://blog.csdn.net/qq_53645728/article/details/129233177)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [ABAP SELECT SUM函数用法](https://blog.csdn.net/qq_40223617/article/details/126561381)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [SAP-ABAP-SELECT语法SQL语法详解](https://blog.csdn.net/Qunending/article/details/126656678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值