SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow

Check Existence using ABAP SELECT

This ABAP tutorial shows how to check existence of data in SAP tables using ABAP SELECT statements In other words, ABAP programmers will find methods to check existency in ABAP using Select command. Of course, for internal tables ABAP developers can use SELECT SINGLE with TRANSPORTING NO FIELDS. Unfortunately if the SAP developer is dealing with database tables to check if a data exists or not requires other ways for solution.

In order to check existency of a data stored in database table in SAP system using ABAP SELECT statement is as follows.

In this existency check method, ABAP Select command is executed with Single and only mandt field is selected which is found on every table and it is stored into sy-mandt system variable.
Since mandt exists on every SAP table and sy-mandt is always available as a system variable, ABAP programmer does not have to define dummy local variables.
Besides sy-mandt system structure field value will not be changed with this Select Single statement since the data table mandt field values are same as sy-mandt value.

SELECT SINGLE mandt INTO sy-mandt FROM vbak WHERE vbeln = '0120000002'.
IF sy-subrc = 0.
 WRITE:/ 'Exits'.
ELSE.
 WRITE:/ 'Do Not Exit'.
ENDIF.
Code

Although above code to check data existence in an ABAP database table is my favorite, the extended check for ABAP program will display following as an error in SLIN screen.

Do not change system fields.
Can be hidden using pragma ##WRITE_OK. Message Code UNR 0237
Code

The error message shows also how to supress this extended check error. ABAP programmer can add the pragma ##WRITE_OK to exclude this code statement from error list.

SELECT SINGLE mandt INTO sy-mandt FROM vbak WHERE vbeln = '0120000002' ##WRITE_OK.
IF sy-subrc = 0.
 WRITE:/ 'Exits'.
ELSE.
 WRITE:/ 'Do Not Exit'.
ENDIF.
Code

An other method is using the UP TO 1 ROWS. But ABAP developer has to use SELECT and ENDSELECT in order to compile and execute this existency check successfully.

Unfortunately this is not a preferred way for existency check in ABAP especially when compared with above existency check sample.

SELECT mandt INTO sy-mandt FROM vbak UP TO 1 ROWS WHERE vbeln = '0120000002'.
ENDSELECT.
IF sy-subrc = 0.
 WRITE:/ 'Exits'.
ELSE.
 WRITE:/ 'Do Not Exit'.
ENDIF.
Code

Let's mention an other method where a workarea definition is required. This method is also not a good way to check if a row exists in table or not. Because the SELECT command transfers data from SAP database table into ABAP workarea structure which is not necessary.

TABLES vbak.
SELECT SINGLE * FROM vbak WHERE vbeln = '0120000002'.
* OR
DATA ls_vbak TYPE vbak.
SELECT SINGLE * FROM vbak INTO ls_vbak WHERE vbeln = '0120000002'.

IF sy-subrc = 0.
 WRITE:/ 'Exits'.
ELSE.
 WRITE:/ 'Do Not Exit'.
ENDIF.
Code

Of course if your SAP system is 7.40 with the enhancements provided with 7.40 ABAP programmer can declare and create a local variable on the fly instead of creating a local structure as follows.

SELECT SINGLE mandt FROM vbak INTO @DATA(lv_mandt) WHERE vbeln = '0120000002'.
IF sy-subrc = 0.
 WRITE:/ 'Exits'.
ELSE.
 WRITE:/ 'Do Not Exit'.
ENDIF.
Code

Again if ABAP developer apply the same enhancement of 7.40 for workarea creation, following ABAP code will be build to check existency of a data in SAP table.

SELECT SINGLE * FROM vbak INTO @DATA(ls_vbak) WHERE vbeln = '0120000002'.
IF sy-subrc = 0.
 WRITE:/ 'Exits'.
ELSE.
 WRITE:/ 'Do Not Exit'.
ENDIF.
Code


SAP HANA and ABAP

Install SAP Free
CRM Companies List
Web Based CRM Software


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.