Determining the Attributes of Data Objects 

You may sometimes need to find out the attributes of a data object at runtime that were not statically available. For example, you may need to find out the type of a generic interface parameter in a subroutine. To do this, you would use the statement:

DESCRIBE FIELD <f> [LENGTH <l>] [TYPE <t> [COMPONENTS <n>]]
                   [OUTPUT-LENGTH <o>] [DECIMALS <d>]
                   [EDIT MASK <m>] [HELP-ID <h>].

The attributes of the data object <f> specified by the parameters of the statement are written to the variables following the parameters. You can use any number of the additions in the same statement.

DESCRIBE FIELD DISTANCE BETWEEN <f1> AND <f 2> INTO <d>.

This statement returns the distance between the data objects <f1> and <f 2>.

The next sections describe the additions in detail:

Determining the Field Length

To find out the field length of a data objet, use the LENGTH addition:

DESCRIBE FIELD <f> LENGTH <l>.

The statement writes the length of the field <f> into the variable <l>.

DATA: text(8) TYPE c,
      len TYPE i.

DESCRIBE FIELD text LENGTH len.

Field LEN contains the value 8.

Determining the Data Type

To find out the data type of a data objet, use the TYPE addition:

DESCRIBE FIELD <f> TYPE <t>.

The statement writes the type of the field <f> into the variable <t>.

Apart from the predefined ABAP type of the field (C, D, F, I, N, P, T, X), the statement can return the following values:

These values are internal type identifiers. Data objects with the internal types s and b may have been declared using references to corresponding elementary types in the ABAP Dictionary. Data objects with type h are aggregated. Data objects with internal table r are reference variables in ABAP Objects.

If <f> is a structure, the statement also returns the value C. To find out more about a structure, you must use the extra addition COMPONENTS:

DESCRIBE FIELD <f> TYPE <t> COMPONENTS <n>.

Instead of C, the statement writes the following internal type identifiers into the variable <t>:

The number of direct components of the structure is written into the variable <n> as an integer.

TABLES spfli.
DATA: numtext(8) TYPE n,
      typ(1) TYPE c.

DESCRIBE FIELD numtext TYPE typ.
WRITE typ.

DESCRIBE FIELD spfli-fltime TYPE typ.
WRITE typ.

This example produces the following output:

N T

The field TYP contains first the value N, then T.

TYPES: surname(20)  TYPE c,
       street(30)   TYPE c,
       zip_code(10) TYPE n,
       city(30)     TYPE c,
       phone(20)    TYPE n,
       date         LIKE sy-datum.

TYPES: BEGIN OF address,
         NAME TYPE surname,
         CODE TYPE zip_code,
         TOWN TYPE city,
         STR  TYPE street,
       END OF address.

TYPES: BEGIN OF phone_list,
         adr TYPE address,
         tel TYPE phone,
       END OF phone_list.

DATA pl TYPE phone-list.

DATA: typ(1) TYPE c,
      n TYPE i.

DESCRIBE FIELD pl TYPE typ COMPONENTS n.

Here, the field TYP contains the value 'u' and N contains the value 2 because PL is a structure with two direct components (ADR and TEL) and without internal tables.

Determining the Output Length

To find out the field length of a data obje c t, use the OUTPUT-LENGTH addition:

DESCRIBE FIELD <f> OUTPUT-LENGTH <o>.

The statement writes the output length of the field <f> into the variable <o>. The output length of a field is the number of characters occupied by the field contents on a list after a WRITE statement.

DATA: float TYPE f,
      out TYPE i,
      len TYPE i.

DESCRIBE FIELD float LENGTH len OUTPUT-LENGTH out.

This example results in field LEN containing the value 8 and the field OUT containing the value 22.

Determining the Decimal Places

To find out the number of decimal places occupied by a data objet, use the DECIMALS addition:

DESCRIBE FIELD <f> DECIMALS <d>.

The statement writes the number of decimal places in field <f> into the variable <d>.

DATA: pack TYPE p DECIMALS 2, dec(1) TYPE c.

DESCRIBE FIELD pack DECIMALS dec.

This example results in field DEC containing the value 2.

Determining the Conversion Routine

To find out any conversion routine defined for the field in the ABAP Dictionary, use the EDIT MASK addition:

DESCRIBE FIELD <f> EDIT MASK <m>.

If <f> was declared with reference to an ABAP Dictionary data type and there is a conversion routine <conv> for the field, the statement writes it into the variable <m> in the form '==<conv>'. You can specify a conversion routine for the domain of a data element in the ABAP Dictionary. Each conversion routine is linked to two function modules that allow the ABAP Dictionary representation of a value to be converted into the program representation and vice versa. The variable <m> can be used as an edit mask in a WRITE statement.

DATA: fltime TYPE s_fltime,
      m(7) TYPE c.

DESCRIBE FIELD fltime EDIT MASK m.

fltime = sy-uzeit.

WRITE: / fltime,
       / sy-uzeit,
       / sy-uzeit USING EDIT MASK m.

The domain S_DURA of data element S_FLTIME in the ABAP Dictionary is linked to the conversion routine SDURA. The field M contains the value ==SDURA. This produces the following output list:

604:55
10:04:55
604:55

The conversion routine SDURA converts hours into minutes. Internally, field FLTIME is handled in the converted form. Field SY-UZEIT is displayed in the list in the converted form.

Determining the Help Text ID

You can find out the identifier of the help text (F1 help) defined for a field in the ABAP Dictionary using the HELP-ID addition:

DESCRIBE FIELD <f> HELP-ID <h>.

If the field <f> is defined with reference to a data type from the ABAP Dictionary, the statement writes the help text ID into the variable <h>. You can use the ID in a suitable function module to display the help text.

DATA: company TYPE s_carr_id,
      h(20) TYPE c,
      tlink TYPE TABLE OF tline.

DESCRIBE FIELD company HELP-ID h.

CALL FUNCTION 'HELP_OBJECT_SHOW'
     EXPORTING
          dokclass                      = 'DE'
          doklangu                      = sy-langu
          dokname                       = h
     TABLES
          links                         = tlink
     EXCEPTIONS
          object_not_found              = 1
          sapscript_error               = 2
          others                        = 3.

IF sy-subrc <> 0.
  ...
ENDIF.

In this program, the field H receives the name of the data element S_CARR_ID. The function module HELP_OBJECT_SHOW displays the documentation for the data element in a dialog box.

Determining the Distance Between Two Fields

To find out the distance between two fields in memory, use the following statement:

DESCRIBE FIELD DISTANCE BETWEEN <f1> AND <f 2> INTO <d>.

The statement returns the number of bytes between data objects <f1> and <f 2> into the variable <d>. The length of the first field in memory is always included. Any alignment is taken into account.

DATA: BEGIN OF test,
col1(3) TYPE C,
col2(2) TYPE C,
col3 TYPE i,
END OF test,
dist TYPE i.

DESCRIBE DISTANCE BETWEEN test-col3 AND test-col1 INTO dist.

The field DIST has the value 8. These are the lengths 3 and 2 of the components COL1 and COL2, and an alignment gap of 3 characters between COL2 and COL3.