Quantcast
Channel: SCN : Document List - ABAP Development
Viewing all articles
Browse latest Browse all 935

Dynamic Internal Tables

$
0
0

You want to create an internal table, however you don’t have the required specifications viz., the number of columns and the data type of the fields at design time. This is where we need to know about Dynamic Internal tables. Although you might see some knowledge documents about Dynamic tables in SCN, what I have done here is combining different approaches to create the internal tables dynamically. You will see two approaches to create the dynamic internal tables.

 

Dynamic Internal tables

 

  • Tables with variable number of rows & columns which can be defined during runtime only.
  • Some of the attributes which can be set dynamically are

      •  Column position

      •  Column width

      •  Field name of internal table field

      •  Data type of the fields

  • Benefits:

      •  Extremely useful when the number of columns/fields are not known at design time.

      •  Avoids redundancy

 

  

    Pre-requisites:

 

  • Field Symbols

      •  Field symbols are placeholders or symbolic names for other fields.

      •  They do not physically reserve space for a field, but point to its 

               contents.

 

              FIELD-SYMBOLS: <gt_table1> TYPE STANDARD TABLE

 

  • Data references

           •  Data references are pointers to data objects.

      •  Used to create data objects dynamically.

 

              DATA: dref1 TYPE REF TO data,                  

                     ASSIGN dref1->* TO <fs1>.

 

  

    Example scenario:

    

    To illustrate the usage of Dynamic Internal tables, I have given below a requirement which I came across

    in my project.

 

  • Select configuration values from various tables (see fig. below) and send it to the partner. The number of tables is not defined at design time. The initial requirement suggested a count of more than 50 tables.   

 

Table1.jpg

  • The output layout was fixed as given below

          Output1.jpg

  • Config/Code Values extract:

        Config1.png

 

  • Logic used:
              
           Data declaration

          •  p_struc            TYPE REF TO cl_abap_structdescr

          •  p_struc_table   TYPE REF TO cl_abap_tabledescr

          •  ls_comp          TYPE              abap_componentdescr

          •  lt_comp           TYPE              cl_abap_structdescr=>component_table

          •  lv_integer         TYPE              I

 

    Code Snippet            

          •  ls_comp-name = wa_object-tab1field [ Table Field ]

          •  lv_integer = '40‘

          •  ls_comp-type = cl_abap_elemdescr=>get_c( lv_integer ) 

          •  APPEND ls_comp TO lt_comp

          •  p_struc = cl_abap_structdescr=>create( p_components =  lt_comp ) [ Create Structure ]

                •  p_struc_table = cl_abap_tabledescr=>create( p_struc ) [ Create Table ]

                •  CREATE DATA gt_table TYPE HANDLE p_struc_table. [ Create a data reference ]

                •  ASSIGN gt_table->* TO <gt_table1>. [ Assign the contents of the data object ]

              

 

    Now, I'll describe the other method to create dynamic internal tables.

  • Dynamic table can also be created using the ALV control lvc_s_fcat
  • Some of the fields contained in this structure are shown in the following figure

        alv1.jpg

 

  • However, this method has a limitation of 36 dynamic internal tables. Otherwise, it gives a short 

       dump error GENERATE_SUBPOOL_DIR_FULL  

   

    Usage of ALV Control:

 

        Data declaration

            g_container              TYPE scrfname VALUE ‘Container Control on Screen‘

            g_grid                      TYPE REF TO cl_gui_alv_grid

            g_custom_container TYPE REF TO cl_gui_custom_container

            i_alv_cat                  TYPE TABLE OF lvc_s_fcat

            catalog                    TYPE lvc_t_fcat

            table_name              TYPE dd02l-tabname

            new_table                TYPE ref to data

            layout                      TYPE lvc_s_layo

  

       

   FIELD-SYMBOLS: <fs_table> TYPE STANDARD TABLE

 

 

    Populate the container

          CREATE OBJECT g_custom_container EXPORTING container_name =  g_container

                CREATE OBJECT g_grid EXPORTING i_parent = g_custom_container

   

          Field catalog

               CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

                        EXPORTING

                                i_structure_name         = table_name

                        CHANGING

                                ct_fieldcat                   = catalog

                        EXCEPTIONS

                                inconsistent_interface  = 1

                                program_error             = 2

                                OTHERS                    = 3.

                        IF sy-subrc <> 0.

                                MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

                                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

                        ENDIF.

 

    Create Dynamic table & assign to Field Symbol

               CALL METHOD cl_alv_table_create=>create_dynamic_table

                        EXPORTING

                              it_fieldcatalog                     = i_alv_cat

                        IMPORTING

                             ep_table                             = new_table

                       EXCEPTIONS

                             generate_subpool_dir_full     = 1

                             OTHERS                            = 2.

 

               ASSIGN new_table->* TO <fs_table> .

 

         Fetch data

                SELECT * FROM table_name INTO CORRESPONDING FIELDS OF TABLE <fs_table>

 

           ALV Display

                     CALL METHOD g_grid->set_table_for_first_display

                        EXPORTING

                               i_structure_name   = table_name

                               is_layout               = layout

                        CHANGING

                               it_outtab                = <fs_table>

                               it_fieldcatalog         = i_alv_cat.

 

 

 

  

     Thanks,

    Akshay Salunke

igate logo.jpg


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>