Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Testing for datatypes

Former Member
0 Likes
1,044

Hi,

I have the requirement to dynamically build a line_declaration for an internal table which will be filled with data from one or several combined DB tables. Ultimately, the user will, in a selection screen, choose which fields they want to include.

The datatype of a field in my internal table has to match the DB table of course, else I would have to convert the values coming from the DB tables and I want to stick with the block-wise filling of the internal table using INTO CORRESPONDING FIELDS. So I have to declare the datatype of the individual fields making up one record in my internal table distinguishing between three types of fields:

- DATE-type fields - for those I have to declare a D field

- NUM-type fields - for those I have to declare a type like >>p DECIMALS 2<<

- everything else can be filled into a STRING-type field.

Is there a way in ABAP to test a DB-table-field that I know the name of for its datatype? I have not found anything on that in my ABAP reference nor online.

Thanks a lot!

Best regards,

Sapperdapper

9 REPLIES 9
Read only

dirk_wittenberg
Contributor
0 Likes
1,008

Hi Friedrich,

did you have a look at class CL_ABAP_TYPEDESCR and its various subclasses?

With these classes you can get information about the data type of a variable / field or check the compatibility of 2 objects.

Kind regards,

Dirk Wittenberg

Read only

Former Member
0 Likes
1,008

Hi,

Sample code : To know the structure technical details(i.e) For Database Tables also.

REPORT  ZCL_ABAP_STRUCTDESCR.

PARAMETERS P_TABNAM type TABNAME OBLIGATORY.

Data:  r_descr type REF TO cl_abap_structdescr,

         wa_comp TYPE abap_compdescr.

START-OF-SELECTION.

**         ?=   Widening Casting

r_descr ?= cl_abap_typedescr=>describe_by_name( P_TABNAM ).

Loop at r_descr->components into wa_comp.

   write:/ wa_comp-name, wa_comp-type_kind, wa_comp-length,

            wa_comp-decimals.

EndLoop.

Kind Regards,

Balaji

Read only

0 Likes
1,008

Hi,

thanks for the help!

I wonder - if for example the user of my report enters a total of, say, 40 fields (from whatever number of tables) to work on, will it be less resource-intensive to use coding like this to test the datatype of the fields and use one of the three datatypes STRING, DATE and >p DECIMALS 2< - or just to adapt the datatype of the fields (in the internal table) to the datatype in the corresponding DB table using LIKE?

I guess the second alternative would be faster, in which case I would not start going down the road I've started in this thread - but I am not the expert, so I'd like to hear your opinion.

Thanks a lot!

Best regards,

Sapperdapper

Read only

0 Likes
1,008

Hi,

Welcome.

Of course you can go-head with the second alternative,which will be fast too.

Kind regards,

Prasanna Balaji K

Read only

0 Likes
1,008

Hi Prasanna,

okay, if that is the faster alternative, then I'll go for that - but I cannot go ahead with it yet because, honestly, I don't know how to do it and I haven't yet found any help text that I can follow.

What I need to do in that case is combine two inputs from the selection screen:

=> Example: The user fills out one field with 'KNA1' and another one with 'KUNNR' in the selection screen - then I have to concatenate those two using CONCATENATE into the string 'KNA1-KUNNR' - I can do that - and then I have to use that text to refer to the database object KNA1-KUNNR so I can use its datatype.

From various sources, I assume I have to use FIELD-SYMBOLS in one way or other, but for all I tried I have not yet figured out how to use it.

Can you explain me how to do that?

Thanks a lot!

Best regards,

Sapperdapper

Read only

0 Likes
1,008

Hi,

if I understand you right you want to code somtething like

DATA my_var LIKE <input_from_user>.

where this <input_from_user> would be KNA1-KUNNR ?

This won't work. For the declaration of dynamic variables you have to refer to

DATA my_var TYPE REF TO DATA.

CREATE DATA my_var TYPE HANDLE my_type_handle .

where my_type_handle has to be created using classes like CL_ABAP_STRUCTDESCR with method CREATE . The parameter P_COMPONENTS for this method is a table with all fields to be created for the structure.

Filling this parameter table will be some work to be done but I don't see another chance.

And don't be afraid of the performance, the classes of the RTTS ( Run Time Type Services ) are quite fast.

Regards,

Dirk

Read only

0 Likes
1,008

Hi Dirk,

close: I don't want to use the user input as a data object that I can refer to, but rather I want to use the DB object that the user input names - it is a prerequisite that the user enters only the names of existing fields. In this example, there is a DB field KUNNR in table KNA1 and I want to use the datatype of that field.

What I want to do is to create an internal table that I can populate with values from DB tables, which is why the fields of my internal table have to match the DB fields.

My structure could look like this:

TYPES: BEGIN OF line01_type,

              KUNNR LIKE KNA1-KUNNR,

              ... ,

              END OF line01_type.

(both KUNNR and KNA1 being variables from my selection screen, I just concatenated them to get KNA1-KUNNR).

Of course, there are other possibilities - in this example, the field KUNNR for my internal table just has to be a C-type field with a LENGTH of 10 or a STRING field - but just referring to the DB object seems to me to be the easiest way. That way, fields which are numeric in the DB (for example DMBTR fields) will automatically be numeric in my internal table and DATE-type fields (ERDAT or so) will automatically be of datatype DATE - and the populating will work smoothly.

I imagine that would be easier than using a class to find out about a field's datatype and so on - but again, I am not the expert. Maybe things just don't work that simple.

Best regards,

Sapperdapper

Read only

0 Likes
1,008

Hi,

Kindly follow the below steps for your scenario,

Step 1: From the selection screen,get the fields of the database table which you want to fetch from the table.

Step 2: Dynamically create the internal table structure using the following link http://help.sap.com/abapdocu_702/en/abencreate_data_via_rttc_abexa.htm with the help of the below code which i mentioned above.

FYI

  • DESCRIBE TABLE & DESCRIBE FIELD syntax became obsolete, so we have one option with good performance means RTTI & RTTC Concept using OO ABAP.

  • RTTI : RunTime Type Identification - To find the any variable attributes like data type,length       .                                                   etc in run-time.

        RTTC : RunTime Type Creation - To create the data type (or) structure dynamically.

If you are still facing any issues, let me know dear.

Kind Regards,

Prasanna Balaji K

Read only

0 Likes
1,008

Hi Prasanna,

many thanks! I will try.

It still seems a tad more complicated than I thought it should be, but I worked with a scripting language, that was easy. I thought I could just take the words - in fact the variables 'KNA1' and 'KUNNR'  that the user puts in the selection screen are just words without connection to anything - and then use something like LIKE REF TO or so to just define the datatype of my fields with reference to the DB object that these variables refer to.

Well, I guess I will just have to learn some of the inner workings of ABAP. I will follow the links you provided and read the help and see where it takes me.

Thanks for now!

Best regards,

Sapperdapper