Skip to Content
author's profile photo Former Member
Former Member

Runtime structure creation

Hello ABAP experts,

I would like to create a structure. This structure should have the field names that I select from a table value.

For example,

Let's say my select statement returns the value 'VALUE1', my structure definition should be

begin of stru1 ,

'VALUE1'(20) type c,

belnr type belnr,

matnr type matnr,

end of stru1.

Is this possible? If so, can you please provide me a sample code?

Thank you,

Ram Kannan

Moderator message - FAQ. Unmarked as question & removed points allocated.

Message was edited by: Suhas Saha

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • Posted on Jan 21, 2014 at 06:57 PM

    TYPES: BEGIN OF ty_struc,

    value1(20) TYPE c,

    belnr TYPE belnr,

    matnr TYPE matnr,

    END OF stru1.

    Here you have the structure definition - or table definition.

    DATA ls_struc TYPE ty_struc.

    Gives you the structure at run time. If you mean creation via RTT, you should do:

    DATA lr_data TYPE REF TO data.

    CREATE DATA lr_data TYPE ty_struc.

    And for the usage of the structure you should derefence it:

    FIELD-SYMBOLS <fs_struc> TYPE any.

    ASSIGN lr_data->* TO <fs_struc>.



    Best,



    Sander


    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 21, 2014 at 07:20 PM

    DATA: lt_fieldcat TYPE lvc_t_fcat.

    1) Fill your fields into LT_FIELDCAT.

    2) Then create a dynamic internal table using that field catalogue.

    DATA: lt_dyn_table TYPE REF TO data,

    ls_structure TYPE REF TO data.

    FIELD-SYMBOLS : <fs_dyn_table> TYPE ANY TABLE,

    <fs_structure> TYPE any.

    CALL METHOD cl_alv_table_create=>create_dynamic_table

    EXPORTING

    it_fieldcatalog = lt_fieldcat

    IMPORTING

    ep_table = lt_dyn_table

    EXCEPTIONS

    generate_subpool_dir_full = 1

    OTHERS = 2.

    ASSIGN lt_table->* TO <fs_table>.

    3) Then create the structure dynamically.

    CREATE DATA ls_structure LIKE LINE OF <fs_table>.

    ASSIGN ls_structure->* TO <fs_structure>.

    Add a comment
    10|10000 characters needed characters exceeded

    • My point exactly. So Ram, you could also consider the solution provided below.

      REPORT z_example_rtt_comp_create.

      DATA gt_component TYPE cl_abap_structdescr=>component_table.

      *----------------------------------------------------------------------*

      * CLASS lcl_comp DEFINITION

      *----------------------------------------------------------------------*

      *

      *----------------------------------------------------------------------*

      CLASS lcl_comp DEFINITION.

      PUBLIC SECTION.

      CLASS-METHODS add_field

      IMPORTING

      iv_field TYPE fieldname

      iv_rollname TYPE any.

      CLASS-METHODS get_component

      RETURNING

      value(rt_component) TYPE cl_abap_structdescr=>component_table.

      PRIVATE SECTION.

      CLASS-DATA mt_component TYPE cl_abap_structdescr=>component_table.

      ENDCLASS. "lcl_comp DEFINITION

      *----------------------------------------------------------------------*

      * CLASS lcl_comp IMPLEMENTATION

      *----------------------------------------------------------------------*

      *

      *----------------------------------------------------------------------*

      CLASS lcl_comp IMPLEMENTATION.

      METHOD add_field.

      DATA ls_component TYPE cl_abap_structdescr=>component.

      ls_component-name = iv_field.

      ls_component-type ?= cl_abap_datadescr=>describe_by_name( iv_rollname ).

      INSERT ls_component INTO TABLE mt_component.

      ENDMETHOD. "add_field

      METHOD get_component.

      rt_component[] = mt_component[].

      ENDMETHOD. "get_component

      ENDCLASS. "lcl_comp IMPLEMENTATION

      START-OF-SELECTION.

      lcl_comp=>add_field(

      iv_field = 'VALUE1'

      iv_rollname = 'CHAR20' ). "#EC NOTEXT

      lcl_comp=>add_field(

      iv_field = 'BELNR'

      iv_rollname = 'BELNR' ). "#EC NOTEXT

      lcl_comp=>add_field(

      iv_field = 'MATNR'

      iv_rollname = 'MATNR' ). "#EC NOTEXT

      * INSERT ANY ADDITIONAL VALUES YOU LIKE

      gt_component = lcl_comp=>get_component( ).

      DATA lo_struct_type TYPE REF TO cl_abap_structdescr.

      DATA lo_table_type TYPE REF TO cl_abap_tabledescr.

      DATA lx_root TYPE REF TO cx_root.

      TRY.

      * RTT structure type handler

      lo_struct_type ?= cl_abap_structdescr=>create(

      p_components = gt_component ).

      * RTT Table type handler - based on structured type)

      lo_table_type = cl_abap_tabledescr=>create(

      p_line_type = lo_struct_type ).

      CATCH cx_sy_struct_attributes INTO lx_root.

      sy-msgv1 = lx_root->if_message~get_text( ).

      MESSAGE e398(00) WITH sy-msgv1.

      EXIT.

      ENDTRY.

      DATA lr_table TYPE REF TO data.

      DATA lr_struct TYPE REF TO data.

      FIELD-SYMBOLS <ft> TYPE ANY TABLE.

      FIELD-SYMBOLS <fs> TYPE ANY.

      * Creation and dereferencing of the table

      CREATE DATA lr_table TYPE HANDLE lo_table_type.

      ASSIGN lr_table->* TO <ft>.

      * Creation and dereferencing of the structure

      CREATE DATA lr_struct TYPE HANDLE lo_struct_type.

      ASSIGN lr_struct->* TO <fs>.

  • author's profile photo Former Member
    Former Member
    Posted on Jan 21, 2014 at 06:42 PM

    Hi ,

    Can you please elaborate little more?

    select statement suppose returns multiple valuees in internal table like 'Val1','Val2' and 'val3'.

    Then u need structure like,

    begin of stru1 ,

    'VAL1'(20) type c,

    val2(20) type c,

    val3(20) type c,

    end of stru1.

    Am i getting right?

    BR

    Sumeet

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 21, 2014 at 06:53 PM

    Hello,

    For you requirement, you will need to create a dynamic internal table based in your fields received in your SELECT statement and use a field symbol structure to assign for future use.

    You can use this content as reference: Dynamic Internal table - ABAP Development - SCN Wiki.

    Focus in the FORM routine CREATE_DYNAMIC_ITAB, last 2 lines.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 22, 2014 at 02:37 AM

    Thank you everyone who answered the question...

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.