2

I am trying to validate if a record in a structure is in a cluster table. The code that I thought of using is the following:

REPORT zzz.

DATA: BEGIN OF gs_zfi,
        number TYPE bseg-belnr,
      END OF gs_zfi.

START-OF-SELECTION.
  SELECT SINGLE @abap_true
    FROM bseg
    INTO @DATA(lv_belnr_exists)
    WHERE belnr = @gs_zfi-number. "number field has the same data element as the belnr(belnr_d)

  IF lv_belnr_exists IS INITIAL.
    MESSAGE a011(zfivald).
  ENDIF.

However, I am not allowed to use abap_true in a cluster table. Is there any similar way to check whether a record in the structure, i.e. the field number, exist in the belnr field of the table bseg?

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
Frank
  • 153
  • 3
  • 17
  • `BSEG` is not a cluster table. At least not in S4-HANA. There is no syntax error on a HANA system. – Jagger Feb 01 '22 at 12:41
  • @Jagger There are no cluster tables on HANA DB, because as soon as you switch to the HANA database backend (which does not necessarily require to upgrade the business logic to S/4), all cluster tables get transformed into regular tables. But BSEG is a cluster table on non-HANA systems. And not all SAP systems in the world are migrated to HANA databases yet. – Philipp Feb 02 '22 at 11:48

3 Answers3

3

To my knowledge, the most efficient and concise way to check if a certain row exists in a database table is SELECT SINGLE COUNT( * ). You don't even need a temporary variable, because you can just check sy-subrc or sy-dbcnt:

SELECT SINGLE COUNT( * )
   FROM bseg
   WHERE belnr = @gs_zfi-number.
IF sy-subrc = 0.
   " Record exists
ELSE.
   " Record does not exist
ENDIF.
Philipp
  • 67,764
  • 9
  • 118
  • 153
2

As mentioned in the comment there is no syntax error on an SAP HANA system. What you can do is following.

REPORT zzz.

DATA: BEGIN OF gs_zfi,
        number TYPE bseg-belnr,
      END OF gs_zfi.

START-OF-SELECTION.
  SELECT SINGLE belnr
    FROM bseg
    INTO @DATA(lv_belnr)
    WHERE belnr = @gs_zfi-number.

  DATA(lv_belnr_exists) = xsdbool( sy-subrc = 0 ).

  IF lv_belnr_exists IS INITIAL.
    MESSAGE a011(zfivald).
  ENDIF.

Alternatively

REPORT zzz.

DATA: BEGIN OF gs_zfi,
        number TYPE bseg-belnr,
      END OF gs_zfi.

START-OF-SELECTION.
  SELECT belnr FROM bseg
    INTO @DATA(lv_belnr)
    WHERE belnr = @gs_zfi-number.
    DATA(lv_belnr_exists) = abap_true.
    EXIT.
  ENDSELECT.

  IF lv_belnr_exists IS INITIAL.
    MESSAGE a011(zfivald).
  ENDIF.
Jagger
  • 10,350
  • 9
  • 51
  • 93
-1

You could add field BUKRS (company code) into where clause, to make your selection run faster

key fields of BSEG table:

u.n
  • 52
  • 4