Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RTTI: Get length of a character column

Tags:

rtti

abap

My function module receives a table name and a column name at runtime.

I would like to get the length of the column: How many characters are allowed in the transparent table?

I used my favorite search engine and found RTTS.

But the examples in the documentation pass a variable to the RTTS method DESCRIBE_BY_DATA; in my case, I don't have a variable, I just have the type names in table_name and column_name.

How to get the length?

like image 587
guettli Avatar asked Feb 28 '19 13:02

guettli


2 Answers

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.
like image 181
Sandra Rossi Avatar answered Nov 11 '22 22:11

Sandra Rossi


You don't need RTTS for this. You can Do one of this

  1. Select table DD03L with TABNAME and FIELDNAME
  2. Use Function DDIF_FIELDINFO_GET
like image 33
I.B.N. Avatar answered Nov 11 '22 23:11

I.B.N.