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 table using a pre-existing structure from the data dictionar

Former Member
0 Likes
1,739

<Subject line changed>

I am trying to create a dynamic internal table. I have found examples that build this dynamic table using a pre-existing structure from the data dictionary. But how can I do this if all I have is the following (values are assigned during program execution, and there could be more than 3 columns) :

data:  gs_comp  TYPE abap_compdescr.

* Column 1
  gs_comp-name       = 'FIELD1'.
  gs_comp-type_kind  = 'CHAR'.
  gs_comp-length     = 4.
  gs_comp-decimals   = 0.

* Column 2
  gs_comp-name       = 'FIELD2'.
  gs_comp-type_kind  = 'QUAN'.
  gs_comp-length     = 13.
  gs_comp-decimals   = 2.

* Column 3
  gs_comp-name       = 'FIELD3'.
  gs_comp-type_kind  = 'CHAR'.
  gs_comp-length     = 10.
  gs_comp-decimals   = 3.

I was using CALL METHOD cl_alv_table_create=>create_dynamic_table but need to come up with a different idea since that method dumps after executing 36 times.

Moderator Message: Please use a more descriptive subject line in future & use the tags to format your code.

Edited by: Suhas Saha on Jan 24, 2012 9:45 AM

6 REPLIES 6
Read only

GrahamRobbo
SAP Mentor
SAP Mentor
0 Likes
1,063

Hi Mark,

look at [Runtime Type Services|http://wiki.sdn.sap.com/wiki/display/ABAP/RuntimeTypeServices+(RTTS)].

Cheers

Graham Robbo

Read only

0 Likes
1,063

Thanks, I have been staring at some of those examples, links from that page, but most of them say to use:

cl_abap_structdescr=>describe_by_name( p_tabnam )

But in my case, I don't know p_tabnam. I just have the attributes: Name, Type_kind, Length, and Decimals for each column. How would I assign those values to the attributes and append to something declared as TYPE abap_component_tab ?

Read only

0 Likes
1,063

You can pass a table of components to the CREATE method of the CL_ABAP_STRUCTDESCR class.

Cheers

Graham Robbo

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,063

I was using CALL METHOD cl_alv_table_create=>create_dynamic_table but need to come up with a different idea since that method dumps after executing 36 times.

This is because cl_alv_table_create=>create_dynamic_table( ) uses GENERATE SUBROUTINE POOL technique to generate the dynamic internal table & in the current internal session maximum of 36 dynamic subroutine pools can be generated. Hence the dump!

As Graham has mentioned dynamic internal tables can easily created using the RTTC classes. And they are easy to use & maintain!

But in my case, I don't know p_tabnam. I just have the attributes: Name, Type_kind, Length, and Decimals for each column.

Check the class CL_ABAP_ELEMDESCR & the corres. methods GET_<data_type>( ) methods. You'll have to use these methods to populate COMPONENT_TABLE param.

BR,

Suhas

PS: If you are on Release 702, you should use CL_ABAP_STRUCTDESCR=>GET( ) method instead of CREATE( ).

Read only

Former Member
0 Likes
1,063

Thank you for your help. I have accomplished what I was trying to do. Feel free to try out my sample code below. I was also helped by code found at the bottom of the following thread:


*&---------------------------------------------------------------------*
*&      Form  dyn_test
*&---------------------------------------------------------------------*
*    My goal here is to create an internal table called <dyn_tab>
*    I tested the code below and it works good.
*----------------------------------------------------------------------*
form dyn_test.

** v_tab will just be used to put some entries in <dyn_tab>.
  types: begin of v_typ,
           FIELD1(20)  TYPE c,
           FIELD2      TYPE d,
           FIELD3(10)  TYPE p DECIMALS 2,
         end of v_typ.
  data:  v_wa          TYPE v_typ,
         v_tab         TYPE STANDARD TABLE OF v_typ.

  data: w_tab          TYPE STANDARD TABLE OF abap_compdescr,
        w_tab_wa       TYPE abap_compdescr,
        w_typ          TYPE REF TO cl_abap_elemdescr,
        lt_tot_comp    TYPE cl_abap_structdescr=>component_table,
        lt_comp        TYPE cl_abap_structdescr=>component_table,
        la_comp        LIKE LINE OF lt_comp,
        lo_new_type    TYPE REF TO cl_abap_structdescr,
        lo_table_type  TYPE REF TO cl_abap_tabledescr,
        w_tref         TYPE REF TO data,
        w_dy_line      TYPE REF TO data.

  FIELD-SYMBOLS: <dyn_tab>  TYPE STANDARD TABLE,
                 <dyn_wa>,
                 <dyn_field>.

* Add entries to w_fields_tab
  clear w_tab_wa.
  w_tab_wa-name      = 'FIELD1'.
  w_tab_wa-type_kind = 'C'.         "Char field
  w_tab_wa-length    = '20'.
  append w_tab_wa to w_tab.

  clear w_tab_wa.
  w_tab_wa-name      = 'FIELD2'.
  w_tab_wa-type_kind = 'D'.         "Date field
  append w_tab_wa to w_tab.

  clear w_tab_wa.
  w_tab_wa-name      = 'FIELD3'.
  w_tab_wa-type_kind = 'P'.
  w_tab_wa-length    = '10'.
  w_tab_wa-decimals  = '2'.         "Numeric field with decimals
  append w_tab_wa to w_tab.


  LOOP AT w_tab INTO w_tab_wa.

    CASE w_tab_wa-type_kind.
      WHEN 'STRING'.  w_typ = cl_abap_elemdescr=>get_string( ).
      WHEN 'XSTRING'. w_typ = cl_abap_elemdescr=>get_xstring( ).
      WHEN 'I'.       w_typ = cl_abap_elemdescr=>get_i( ).
      WHEN 'F'.       w_typ = cl_abap_elemdescr=>get_f( ).
      WHEN 'D'.       w_typ = cl_abap_elemdescr=>get_d( ).
      WHEN 'T'.       w_typ = cl_abap_elemdescr=>get_t(  ).
      WHEN 'C'.       w_typ = cl_abap_elemdescr=>get_c( p_length = w_tab_wa-length ).
      WHEN 'N'.       w_typ = cl_abap_elemdescr=>get_n( p_length = w_tab_wa-length ).
      WHEN 'X'.       w_typ = cl_abap_elemdescr=>get_x( p_length = w_tab_wa-length ).
      WHEN 'P'.       w_typ = cl_abap_elemdescr=>get_p( p_length = w_tab_wa-length p_decimals = w_tab_wa-decimals ).
    ENDCASE.

* { begin test
*  WRITE: / 'absolute_name:', w_typ->absolute_name.
*  WRITE: / 'type_kind    :', w_typ->type_kind.
*  WRITE: / 'length       :', w_typ->length.
*  WRITE: / 'decimals     :', w_typ->decimals.
* } end test

    clear la_comp.
    la_comp-type = w_typ.               "Field type
    la_comp-name = w_tab_wa-name.       "Field name   ex: FIELD1
    APPEND la_comp TO lt_tot_comp.      "Add entry to component table

  ENDLOOP.

* Create new type from component table
  lo_new_type = cl_abap_structdescr=>create( lt_tot_comp ).

* Create new table type
  lo_table_type = cl_abap_tabledescr=>create( lo_new_type ).

* Create dynamic internal table and assign to Field Symbol
  CREATE DATA w_tref TYPE HANDLE lo_table_type.
  ASSIGN w_tref->* TO <dyn_tab>.


*** Put a breakpoint on the next statement here, then take a look
*** at the structure of <dyn_tab> in the debugger.


* Create dynamic work area and assign to Field Symbol
  CREATE DATA w_dy_line LIKE LINE OF <dyn_tab>.
  ASSIGN w_dy_line->* to <dyn_wa>.

* Now put some data in the dynamic table so we can see the results
  v_wa-FIELD1 = 'Hello World'.
  v_wa-FIELD2 = sy-datum.
  v_wa-FIELD3 = '1234.56'.
  do 5 times.
    append v_wa to v_tab.
  enddo.
  <dyn_tab>[] = v_tab[].

* Write results
  LOOP AT <dyn_tab> 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.

  WRITE: / 'Finished.', /.

endform.                                                    "dyn_test

Read only

0 Likes
1,063

Good stuff Mark, and thanks for posting your sample code so others can see it. You could also add it to the [Wiki area on RTTC|http://wiki.sdn.sap.com/wiki/display/Snippets/ConceptofRunTimeType+Creation] to enhance that information as well?

Cheers

Graham Robbo