Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Fetch fields from table or structure in ABAP SAP
If you need to identify the fields and number of fields in a structure, then you should use runtime type services. Using runtime type services makes more sense in this case as if we have some data in our environment, then it's not ideal to call database for fetching the same.
Using Runtime Type Services
Runtime type services provide a way to analyze the structure of data objects at runtime. Here's how to get field information from a structure ?
DATA(structure) = VALUE <your_structure>( ). DATA(Descriptor) = CAST cl_abap_structdescr( cl_abap_datadescr=>describe_by_data( structure ) ). DATA(Fields) = LINES( Descriptor->components ).
This will give you the count of the components of the table or structure. The cl_abap_structdescr class provides detailed information about structure components.
Alternative Database Query Method
You can also try another option if you do not want to use runtime type services. The following query can get you the field count from the data dictionary ?
SELECT COUNT(*) INTO @DATA(count) FROM dd03l WHERE tabname = '<STRUCTURE_NAME>' AND as4local = 'A'.
The DD03L table stores the field information of SAP tables and structures. The condition as4local = 'A' ensures you get only active fields.
Complete Example
DATA: ls_structure TYPE sflight. " Method 1: Runtime Type Services DATA(lo_descriptor) = CAST cl_abap_structdescr( cl_abap_datadescr=>describe_by_data( ls_structure ) ). DATA(lv_field_count) = LINES( lo_descriptor->components ). WRITE: / 'Number of fields using RTS:', lv_field_count. " Method 2: Database Query SELECT COUNT(*) INTO @DATA(lv_db_count) FROM dd03l WHERE tabname = 'SFLIGHT' AND as4local = 'A'. WRITE: / 'Number of fields from DD03L:', lv_db_count.
Conclusion
Runtime type services are the preferred method for analyzing structure fields when data is already available in memory, while database queries against DD03L are useful for getting field information directly from the data dictionary.
