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

Create Dynamic Structure at Runtime via ALV-Methods!

Former Member
0 Likes
1,322

Hi Experts,

i try to create at the ABAP Runtime a new Structure.

FOR EXAMPLE: I have a internal Table "database" and i dont know their Structure or Typ.

MY Question: How can i get the structuretype for this internal Table "database" ??

I thought that it is possible with ALV-Methods, but i dont find the right way.

First Step: I must get the structure of this internal Table.

Second Step: I must create a workarea/ line of this internal Table, that i can work row for row with the table.

Have someone an code example for me, because iám very confused about this Problem.

With kind regards.

Ersin Tosun

1 ACCEPTED SOLUTION
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
877

Hello,

For this specific requirement, SAP has provided RTTI class.

Below is a code snippet for your ready reference. In this example we're trying to get the structure of the dynamic table <ITAB> whose structure is not defined till run-time.

TYPE-POOLS: abap.

PARAMETERS: p_table TYPE tabname.

DATA: dref TYPE REF TO data.

FIELD-SYMBOLS <itab> TYPE STANDARD TABLE.

CREATE DATA dref TYPE STANDARD TABLE OF (p_table).
ASSIGN dref->* TO <itab>.

DATA: go_tab_descr TYPE REF TO cl_abap_tabledescr,
      go_struc_descr TYPE REF TO cl_abap_structdescr,
      wa_comp TYPE abap_compdescr.

go_tab_descr ?= cl_abap_tabledescr=>describe_by_data( <itab> ).

CHECK sy-subrc = 0.

go_struc_descr ?= go_tab_descr->get_table_line_type( ).

LOOP AT go_struc_descr->components INTO wa_comp.
  WRITE: / wa_comp-name.
ENDLOOP.

I must create a workarea/ line of this internal Table, that i can work row for row with the table.

Sorry i missed the Step 2:

FIELD-SYMBOLS: <itab> TYPE STANDARD TABLE,
               <wa> TYPE ANY,
               <val> TYPE ANY.

LOOP AT <itab> ASSIGNING <wa>.
  LOOP AT go_struc_descr->components INTO wa_comp.
*   To access the components of the structure dynamically
    ASSIGN COMPONENT wa_comp-name OF STRUCTURE <wa> TO <val>.
  ENDLOOP.
ENDLOOP.

BR,

Suhas

Edited by: Suhas Saha on Nov 18, 2010 7:26 PM

5 REPLIES 5
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
878

Hello,

For this specific requirement, SAP has provided RTTI class.

Below is a code snippet for your ready reference. In this example we're trying to get the structure of the dynamic table <ITAB> whose structure is not defined till run-time.

TYPE-POOLS: abap.

PARAMETERS: p_table TYPE tabname.

DATA: dref TYPE REF TO data.

FIELD-SYMBOLS <itab> TYPE STANDARD TABLE.

CREATE DATA dref TYPE STANDARD TABLE OF (p_table).
ASSIGN dref->* TO <itab>.

DATA: go_tab_descr TYPE REF TO cl_abap_tabledescr,
      go_struc_descr TYPE REF TO cl_abap_structdescr,
      wa_comp TYPE abap_compdescr.

go_tab_descr ?= cl_abap_tabledescr=>describe_by_data( <itab> ).

CHECK sy-subrc = 0.

go_struc_descr ?= go_tab_descr->get_table_line_type( ).

LOOP AT go_struc_descr->components INTO wa_comp.
  WRITE: / wa_comp-name.
ENDLOOP.

I must create a workarea/ line of this internal Table, that i can work row for row with the table.

Sorry i missed the Step 2:

FIELD-SYMBOLS: <itab> TYPE STANDARD TABLE,
               <wa> TYPE ANY,
               <val> TYPE ANY.

LOOP AT <itab> ASSIGNING <wa>.
  LOOP AT go_struc_descr->components INTO wa_comp.
*   To access the components of the structure dynamically
    ASSIGN COMPONENT wa_comp-name OF STRUCTURE <wa> TO <val>.
  ENDLOOP.
ENDLOOP.

BR,

Suhas

Edited by: Suhas Saha on Nov 18, 2010 7:26 PM

Read only

Former Member
0 Likes
877

Hi suhas,

many thanks for your excellent help.

When he want make "CREATE DATA dref TYPE STANDARD TABLE OF (datatab)." than it comes a Error-Message.

Error-Message: Runtime-error UC_OBJECTS_NOT_CHARLIKE !!!!

For again Information, my datatab is an internal table and i dont know what kind of structuretype it is.

But why comes this Error-Message??

Read only

Former Member
0 Likes
877

Hi,

Please review the below code..


DATA: dy_table      TYPE REF TO data,
        dy_line       TYPE REF TO data,.

FIELD-SYMBOLS : <fs_table>    TYPE STANDARD TABLE,
                              <fs_workarea> TYPE ANY,

    V_table will hold the structure name.

    CREATE DATA dy_table TYPE TABLE OF (v_table).
*--Build the Table Details (Fields) at runtime for the Condition Table
    ASSIGN dy_table->* TO <fs_table>.

    CREATE DATA dy_line LIKE LINE OF <fs_table>.
    ASSIGN dy_line->* TO <fs_workarea>.

<fs_table> will be your dynamic internal table.
<fs_workarea> will be your dynamic work area.

There is a limitation in using the OOPS methods for creating the dynamic internal.

Regards,

SRinivas

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
877

Hello,

Error-Message: Runtime-error UC_OBJECTS_NOT_CHARLIKE

Read the F1 documentation for CREATE DATA & you'll understand why you're getting this error message.

my datatab is an internal table and i dont know what kind of structuretype it is.

Anyway you should've used this piece of code snippet to get the structure of your dynamic internal table

DATA: go_tab_descr TYPE REF TO cl_abap_tabledescr,
      go_struc_descr TYPE REF TO cl_abap_structdescr,
      wa_comp TYPE abap_compdescr.

FIELD-SYMBOLS:
<wa> TYPE ANY, "FS to store Work Area value
<val> TYPE ANY.  "FS to store field value
 
go_tab_descr ?= cl_abap_tabledescr=>describe_by_data( <itab> ).
 
CHECK sy-subrc = 0.
 
"Get the structure of your internal table
go_struc_descr ?= go_tab_descr->get_table_line_type( ).
 
LOOP AT <itab> ASSIGNING <wa>.
  LOOP AT go_struc_descr->components INTO wa_comp.
*   To access the components of the structure dynamically
    ASSIGN COMPONENT wa_comp-name OF STRUCTURE <wa> TO <val>.
  ENDLOOP.
ENDLOOP.

Read only

Former Member
0 Likes
877

Hi Suhas,

first many thanks for your corrections and Helps this is also for all other experts in SDN.

I have copy your coding but there is another Problem with Casting!!

go_tab_descr ?= cl_abap_tabledescr=>describe_by_data( datatab ).

On this line, the Compiler got me a Error Message: MOVE_CAST_ERROR.

Why i got problems with casting??

Is the reason that i use a old Release( 4.7) from SAP??

But in my Libary i can find the Class and Methods, which you have send me as a coding.