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

Dynamic structure creation

Hi All,

I want to create dynamic internal table , containing fields of variable length.

I have to give field length at run-time and program should create an inetrnal table accordingly.

E.g.

Runtime parameter length given :

10 , 5, 9 , 12, 40 ....

It should create internal table like:

itab:

field1( 10 )

field2( 5 )

field3( 9 )

field4( 12 )

field5( 40 )

Can we give type of fields also at runime ?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Posted on Jul 25, 2008 at 09:31 AM

    Hi

    Welcome to SDN

    go through the link

    http://www.sap-img.com/ab030.htm

    Regards

    Pavan

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 25, 2008 at 09:47 AM

    u can use this code

      type-pools : abap. 
      field-symbols: <dyn_table> type standard table, 
                   <dyn_wa>, 
                   <dyn_field>. 
      data: dy_table type ref to data, 
          dy_line  type ref to data, 
          xfc type lvc_s_fcat, 
          ifc type lvc_t_fcat. 
      selection-screen begin of block b1 with frame. 
    parameters: p_table(30) type c default 'T001'. 
    selection-screen end of block b1. 
      start-of-selection. 
        perform get_structure. 
      perform create_dynamic_itab.      **********Creates a dyanamic internal table********** 
      perform get_data. 
      perform write_out. 
      form get_structure. 
      data : idetails type abap_compdescr_tab, 
           xdetails type abap_compdescr. 
      data : ref_table_des type ref to cl_abap_structdescr. 
    * Get the structure of the table. 
      ref_table_des ?=  
          cl_abap_typedescr=>describe_by_name( p_table ). 
      idetails[] = ref_table_des->components[]. 
        loop at idetails into xdetails. 
        clear xfc. 
        xfc-fieldname = xdetails-name . 
        xfc-datatype = xdetails-type_kind. 
        xfc-inttype = xdetails-type_kind. 
        xfc-intlen = xdetails-length. 
        xfc-decimals = xdetails-decimals. 
        append xfc to ifc. 
      endloop. 
      endform. 
      form create_dynamic_itab. 
    * Create dynamic internal table and assign to FS 
      call method cl_alv_table_create=>create_dynamic_table 
                   exporting 
                      it_fieldcatalog = ifc 
                   importing 
                      ep_table        = dy_table. 
        assign dy_table->* to <dyn_table>. 
    * Create dynamic work area and assign to FS 
      create data dy_line like line of <dyn_table>. 
      assign dy_line->* to <dyn_wa>. 
      endform. 
        
      form get_data. 
    * Select Data from table. 
      select * into table <dyn_table> 
                 from (p_table). 
      endform. 
       Write out data from table. 
      loop at <dyn_table> into <dyn_wa>. 
        do. 
          assign component  sy-index   
             of structure <dyn_wa> to <dyn_field>. 
          if sy-subrc <> 0. 
            exit. 
          endif. 
          if sy-index = 1. 
            write:/ <dyn_field>. 
          else. 
            write: <dyn_field>. 
          endif. 
        enddo. 
      endloop. 
    

    Amitava

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 25, 2008 at 10:03 AM

    Hi Pratiksha ,

    Refer this blog :

    https://blogs.sap.com/?p=40600

    saptechnical.com/Tutorials/ABAP/ABAPMainPage.htm

    U have good examples regarding Dynamic structure in the following link

    http://www.sap-img.com/ab030.htm

    Regards,

    Sreekar.Kadiri

    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.