Application Development 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: 

Dynamically convert types for a dynamic select statement?

Hello experts.
I got a very strange topic that I am having trouble with.

Here is the problem: I need to do a dynamic select on multiple tables based on field matnr. Inside i_data I got user provided articles with type mara-matnr. Some of the tables, however got different types for matnr and I am getting the exception cx_sy_dynamic_osql_semantics.

For that reason I need to manually convert the type before every select statement. Inside i_domname I got the correct data type for the given entry.
Is it somehow possible to loop over i_tab every time and dynamically use the CONV # statement to convert to the corresponding i_domname type? Alternatively we can use result_tab and do the opposite - there the type is correct, I would just need the value from i_tab to fill it, but again I get a problem with the types.

To conclude - I am trying to convert the type of my internal table ( has only one field - matnr ) based on import parameter i_domname.

Is this possible?
Sorry if unclear.
Thank you.

DATA result_data TYPE REF TO data.
    DATA(type) = CAST cl_abap_datadescr(
    cl_abap_typedescr=>describe_by_name( i_domname ) ).

    CREATE DATA result_data TYPE HANDLE type.

    TYPES: BEGIN OF test_st,
             matnr LIKE result_data,
           END OF test_st,
           test_tt TYPE TABLE OF test_st  WITH DEFAULT KEY.

    DATA result_tab TYPE test_tt.


    LOOP AT i_data INTO DATA(conv).

      APPEND INITIAL LINE TO result_tab ASSIGNING FIELD-SYMBOL(<fs2>).
      <fs2>-matnr = REF #( conv-matnr ).

    ENDLOOP.

SELECT COUNT( * ) INTO @r_count FROM (i_tabname) FOR ALL ENTRIES IN @i_data "Or alternatively use result_tab WHERE matnr = @i_data-matnr.

.... And so on, more selects
1 REPLY 1

Sandra_Rossi
Active Contributor
0 Kudos

I don't really understand what you mean, but maybe you want to create variables at runtime, and for this you have to use Run Time Type Services (RTTS), methods GET of classes cl_abap_elemdescr, etc., CREATE DATA ... TYPE HANDLE ..., ASSIGN dref->* TO ... (dereferencement)

There are many questions/answers in the forum.