This website is not affiliated with, sponsored by, or approved by SAP AG.

0026 - Retrieving Field Names Dynamically

Moderators: Snowy, thx4allthefish, Rich, ilya

0026 - Retrieving Field Names Dynamically

Postby Rich » Sat Mar 28, 2009 8:38 am

Retrieving Field Names Dynamically

Sometimes you may have only a table name and want to retrieve the name of each field of the corresponding table. For example, when you want to use ASSIGN COMPONENT fieldname OF TABLE table.
An ABAPer's first reaction is to read the standard ABAP basis tables DD02L, DD03L, etc

This way of reading fields is very slow. Use methods from the class CL_ABAP_TYPEDESCR instead.

Example:

Code: Select all
data: descr_struc_ref TYPE REF TO cl_abap_structdescr.
descr_struc_ref ?= cl_abap_typedescr=>describe_by_name('SFLIGHT' ).


Here is the result of descr_struct_ref after the execution of this piece of code

Code: Select all
ABSOLUTE_NAME C 200 \TYPE=SFLIGHT
TYPE_KIND C 1 u
LENGTH I 4 80
DECIMALS I 4 0
KIND C 1 S
STRUCT_KIND C 1 F
COMPONENTS h 8 Table[14x40]
HAS_INCLUDE C 1


The table COMPONENTS is filled with :

Code: Select all
LENGTH DECIMALS TYPE_KIND NAME
3 | 0 |C |MANDT
3 | 0 |C |CARRID
4 | 0 |N |CONNID
8 | 0 |D |FLDATE


etc.

You have the fields name and a lot more information about the table. This class can also handle structure, table type, etc.

Note that this method is very fast because it uses the database layer directly thanks to SYSTEM CALLs.

To have a look at the class, use transaction SE24.

(Courtesy Dany Charbonneau)
Regards

Rich

Image
Abap KC:http://www.richard-harper.me.uk/Kb
SFMDR:http://www.se37.com
Rich
 
Posts: 7112
Joined: Thu Oct 31, 2002 4:47 pm
Location: Liverpool

Return to ABAPers

Who is online

Users browsing this forum: No registered users and 1 guest





loading...


This website is not affiliated with, sponsored by, or approved by SAP AG.