7

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?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
guettli
  • 25,042
  • 81
  • 346
  • 663

2 Answers2

8

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.
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • 1
    Just as a side note as per [documentation](https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abenmethod_call_guidl.htm), you could ommit `CALL METHOD`, surrounding the parameters with brackets like `cl_abap_typedescr=>describe_by_name( EXPORTING ... RECEIVING ... EXCEPTIONS ... ).` – andrecito Feb 28 '19 at 14:25
  • 1
    There are constants somwhere for the type kinds; I'd recommend to use those, and rather extract a boolean method `is_character_type` for readability. – Florian Mar 04 '19 at 23:57
  • @Florian, thanks, code changed with constants for better readability ; I prefer not proposing a method here to simplify the answer, but you are right that there should be one in the real life. – Sandra Rossi Mar 05 '19 at 09:50
  • plus 1 for using lo_typedescr->length / cl_abap_char_utilities=>charsize. Shame the devs didnt think to offer a property for this. – phil soady Jul 21 '20 at 11:25
1

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
I.B.N.
  • 992
  • 4
  • 15