RTTI: Get length of a character column

For retrieving the type of a given DDIC type only known at runtime, use the method DESCRIBE_BY_NAME. The RTTI length is always returned as a number of bytes.

Example to get the type of the column CARRID of table SFLIGHT (I know it's a column of 3 characters) :

cl_abap_typedescr=>describe_by_name(
EXPORTING
  p_name         = 'SFLIGHT-CARRID'
RECEIVING
  p_descr_ref    = DATA(lo_typedescr)
EXCEPTIONS
  type_not_found = 1 ).

" you should handle the error if SY-SUBRC <> 0

" Because it's SFLIGHT-CARRID, I expect 6 BYTES
ASSERT lo_typedescr->length = 6. " 3 characters * 2 bytes (Unicode)

" Length in CHARACTERS
CASE lo_typedescr->type_kind.
  WHEN lo_typedescr->typekind_char
    OR lo_typedescr->typekind_num
    OR lo_typedescr->typekind_date
    OR lo_typedescr->typekind_time
    OR lo_typedescr->typekind_string.
  DATA(no_of_characters) = lo_typedescr->length / cl_abap_char_utilities=>charsize.
  ASSERT no_of_characters = 3.
ENDCASE.

Tags:

Abap

Rtti