RTTI: Get length of a character column
Asked Answered
S

2

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?

Son answered 28/2, 2019 at 13:10 Comment(0)
S
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
Stowell answered 20/3, 2019 at 12:32 Comment(1)
I like "select table DD03L". It is simple.Son
C
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.
Calchas answered 28/2, 2019 at 13:20 Comment(4)
Just as a side note as per documentation, you could ommit CALL METHOD, surrounding the parameters with brackets like cl_abap_typedescr=>describe_by_name( EXPORTING ... RECEIVING ... EXCEPTIONS ... ).Koh
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.Cece
@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.Calchas
plus 1 for using lo_typedescr->length / cl_abap_char_utilities=>charsize. Shame the devs didnt think to offer a property for this.Pareto
S
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
Stowell answered 20/3, 2019 at 12:32 Comment(1)
I like "select table DD03L". It is simple.Son

© 2022 - 2024 — McMap. All rights reserved.