Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Michael_Keller
Active Contributor
Some time ago ABAP surprised me with a nice feature. I saw an interesting SELECT statement during a code review. Here is the SELECT statement. For demonstration purpose I changed the statement a little bit.
SELECT SINGLE 'X'
FROM seoclass
WHERE clsname = 'CL_GUI_FRONTEND_SERVICES'
INTO @data(x).

CHECK sy-subrc = 0.

Right away I noticed the "X" behind the term "SINGLE". I hadn't seen such a combination before. Or I couldn't remember it. In any case, I didn't use this syntax yet.

Out of interest, I asked a colleague. He had the right answer: You can check if there is a row in the database table that meets the WHERE condition. With this hint I remembered that I had read about it someday. There was a blog by Horst Keller (and also the link to the SAP online help). From 7.40 SP05 this feature exists. Here is the example from the SAP Online Help.
SELECT SINGLE @abap_true
FROM scarr
WHERE carrid = @carrier
INTO @DATA(exists).

IF exists = abap_true.
cl_demo_output=>display( |Carrier { carrier } exists in SCARR| ).
ELSE.
cl_demo_output=>display( |Carrier { carrier } does not exist in SCARR| ).
ENDIF.

By using the variable "exists" it's much more clear what the statement meant. Because of the challenge to write "easy to understand" source code, I then tried the following example.
CONSTANTS entry_exists TYPE abap_bool VALUE abap_true.

SELECT SINGLE @entry_exists
FROM seoclass
WHERE clsname = 'CL_GUI_FRONTEND_SERVICES'
INTO @DATA(answer_from_db).

IF answer_from_db = entry_exists.
WRITE 'Entry exists.' COLOR COL_POSITIVE.
ELSE.
WRITE 'Entry does not exist.' COLOR COL_NEGATIVE.
ENDIF.

This is valid and works. And a little bit more understandable. However, an additional comment could be useful. According to "Clean ABAP" styleguide, there is another solution so we can omit the comment: Put the source code in a separate method with a suitable name, something like "IS_CLASS_AVAILABLE" in my demo. This could look like this (I omitted some source code parts).
[..]

CLASS-METHODS is_class_available
IMPORTING
class_name TYPE seoclsname
RETURNING
VALUE(result) TYPE abap_bool.

[..]

METHOD is_class_available.
CONSTANTS entry_exists TYPE abap_bool VALUE abap_true.

SELECT SINGLE @entry_exists
FROM seoclass
WHERE clsname = @class_name
INTO @result.

IF sy-subrc <> 0.
ENDIF.
ENDMETHOD.

[..]

IF lcl_test=>is_class_available( 'CL_GUI_FRONTEND_SERVICES' ) = abap_true.
WRITE 'Class exists.' COLOR COL_POSITIVE.
ELSE.
WRITE 'Class does not exist.' COLOR COL_NEGATIVE.
ENDIF.

Ok, with this solution I was happy. For now 😉 Perhaps how to handle sy-subrc is worth a discussion.

As a hint: If you are interested in performance discussions about checking if a row exists, you should read the comments on Horst Keller's blog.

 

Best regards and thanks for reading

Michael

 
16 Comments