0

One Minute, One Great ABAP Tip

This is my first post for SAPLAB.org, so I’ve decided to write about something really simple yet powerful – building ABAP Dynamic Tables using RTTS ( From Release 6.40). RTTS stands for Run Time Type Services.

Creating dynamic internal tables in ABAP programs can be very useful, dynamic coding can provide fast program running and easier maintenance. From release 6.40, RTTS has provided a new approach to dynamic table programming. Here’s an example of what I mean:

REPORT Z_1MIN_1GREATTIP_RTTS.

TABLES PA0002.

* Table type
TYPES: BEGIN OF TY_PERNR_LIST,
         PERNR TYPE PERNR_D,
         BEGDA TYPE BEGDA,
         ENDDA TYPE ENDDA,
       END   OF TY_PERNR_LIST.

* Dynamic Table
DATA: LO_STRUCTDESCR   TYPE REF TO CL_ABAP_STRUCTDESCR,
      LO_ELEMDESCR  TYPE REF TO CL_ABAP_ELEMDESCR,
      LO_STRUCTDESCR_NEW TYPE REF TO CL_ABAP_STRUCTDESCR,
      LO_TABLEDESCR  TYPE REF TO CL_ABAP_TABLEDESCR,
      LO_DATA     TYPE REF TO DATA,
      LT_COMPONENT     TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE,
      LT_TOT_COMPONENT TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE,
      LA_COMPONENT     LIKE LINE OF LT_COMPONENT.

* Dynamic Selection fields
TYPES: BEGIN OF TY_FIELDCATALOG,
         FIELDNAME TYPE CHAR30,
       END   OF TY_FIELDCATALOG.
*
DATA:  LT_FIELDCATALOG TYPE STANDARD TABLE OF TY_FIELDCATALOG,
       LA_FIELDCATALOG TYPE TY_FIELDCATALOG.

* field symbols to access the dynamic table
FIELD-SYMBOLS:    TYPE ANY TABLE,
                 TYPE ANY,
                TYPE ANY.
*
* Selection Screen
SELECT-OPTIONS: SO_PERNR FOR PA0002-PERNR.

START-OF-SELECTION.

* 1- Getting Components of type
  LO_STRUCTDESCR ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( 'TY_PERNR_LIST' ).
  LT_COMPONENT  = LO_STRUCTDESCR->GET_COMPONENTS( ).
  APPEND LINES OF LT_COMPONENT TO LT_TOT_COMPONENT.

*   Element Description Last Name
  LO_ELEMDESCR ?= CL_ABAP_ELEMDESCR=>DESCRIBE_BY_NAME( 'PAD_NACHN' ).
*
*   Field name
  LA_COMPONENT-NAME = 'NACHN'.
*
*   Field type
  LA_COMPONENT-TYPE = CL_ABAP_ELEMDESCR=>GET_C(
                    P_LENGTH   = LO_ELEMDESCR->LENGTH ).
*
*   Filling the component table
  APPEND LA_COMPONENT TO LT_TOT_COMPONENT.
  CLEAR: LA_COMPONENT.

*   Element Description First Name
  LO_ELEMDESCR ?= CL_ABAP_ELEMDESCR=>DESCRIBE_BY_NAME( 'PAD_VORNA' ).
*
*   Field name
  LA_COMPONENT-NAME = 'VORNA'.
*
*   Field type
  LA_COMPONENT-TYPE = CL_ABAP_ELEMDESCR=>GET_C(
                     P_LENGTH   = LO_ELEMDESCR->LENGTH ).
*
*   Filling the component table
  APPEND LA_COMPONENT TO LT_TOT_COMPONENT.
  CLEAR: LA_COMPONENT.

*
* 3. Create a New Type
  LO_STRUCTDESCR_NEW = CL_ABAP_STRUCTDESCR=>CREATE( LT_TOT_COMPONENT ).
*
* 4. New Table type
  LO_TABLEDESCR = CL_ABAP_TABLEDESCR=>CREATE(
                  P_LINE_TYPE  = LO_STRUCTDESCR_NEW
                  P_TABLE_KIND = CL_ABAP_TABLEDESCR=>TABLEKIND_STD
                  P_UNIQUE     = ABAP_FALSE ).
*
* 5. data to handle the new table type
  CREATE DATA LO_DATA TYPE HANDLE LO_TABLEDESCR.
*
* 6. New internal table in the fieldsymbols
  ASSIGN LO_DATA->* TO .
*
*$*$*...............Dynamic Selection.............................*$*$*
* Filling up the table for the Selection fields of Select Query
  LOOP AT LT_TOT_COMPONENT INTO LA_COMPONENT.
    LA_FIELDCATALOG-FIELDNAME = LA_COMPONENT-NAME.
    APPEND LA_FIELDCATALOG TO LT_FIELDCATALOG.
    CLEAR: LA_COMPONENT, LA_FIELDCATALOG.
  ENDLOOP.
*
* Selecting data
  SELECT (LT_FIELDCATALOG)
         INTO  TABLE 
         FROM  PA0002
         UP TO 10 ROWS
         WHERE PERNR IN SO_PERNR.
*
*$*$*...............Accessing dynamic table.......................*$*$*
  LOOP AT  ASSIGNING .
    ASSIGN COMPONENT 'VORNA' OF STRUCTURE  TO .
    CONCATENATE 'Mr.'  INTO  SEPARATED BY SPACE.
  ENDLOOP.
*
*
*$*$*...............Displaying using SALV model...................*$*$*
*
  DATA: LO_ALV TYPE REF TO CL_SALV_TABLE.
*
  TRY.
      CL_SALV_TABLE=>FACTORY(
        EXPORTING
          LIST_DISPLAY = ABAP_FALSE
        IMPORTING
          R_SALV_TABLE = LO_ALV
        CHANGING
          T_TABLE      =  ).
    CATCH CX_SALV_MSG .
  ENDTRY.
*
  LO_ALV->DISPLAY( ).

This is a good and quick way to create a dynamic ALV report in just a few lines of code. Please be sure to read up next on “1 minute – 1 Great Tip”: ECC6 Enhancement Framework in 100 lines.

About the author

Helder Goncalves (hfgoncalves) has been working with ABAP development for more than 9 years, covering all major modules, releases and technologies. Click here to view Helder’s full profile.

Author : webmaster

Author's Website | Articles from

Working as an Information Systems Consultant for over 10 years now, I've found that working with SAP brought me added value on how to deal and manage IT and Information Systems Projects. More than a hobby, this blog is aimed to propagate SAP knowledge exchange and help other SAP technology or functional Consultants finding any piece of useful information. Please participate with your comments and opinions, it will help enrich the SAP community. Thanks!


Related Posts





Community Feeds

  • SAP BPC Training | SAPBPC Placement | SAP Business Planning Consolidation Process Training | SAP BPC

    NewyorkSys is one of the leading top IT Training and Consulting Company in US, with a good placement Track Record. We have certified trainers. We will provide Online Training, Fast Track online training, with job assistance Courses: SAP Technologies, Dataware housing Tools, Oracle Technologies, SAS, JAVA, .NET, BA… Etc.
    ATTEND FREE DEMO CLASS
    NewyorkSys Inc
    USA: +1- 718- 313-0499
    USA: +1-718-305-1757
    E-mail ID: training@NewyorkSys.com
    Visit: http://www.NewyorkSys.com

  • Expertise technique SAP, ABAP

    Expert Technique SAP, ABAP

  • SAP Training and Certification Courses

    Be a part of SAP training and online sap certification to boost up your skills and career. SAP training helps to improve your business methodologies which are beneficial to generate business around the world. JKT SAP Academy is an authorize education partner of sap and provide sap training for sap abap, sap erp, sap basis, sap fi,sap co, sap mm, sap sd, sap pp and other functional modules.

  • SAP All Modules Training & Placement By Newyorksys.com

    NewyorkSys is one of the leading top IT Training and Consulting Company in US, with a good placement track record. We have certified trainers. We will provide Online Training, Fast Track online training, with job assistance Courses : SAP Technologies,Dataware housing Tools , Oralce Technologies,SAS,JAVA,.NET,BA .. etc. ATTEND FREE DEMO CLASS NewyorkSys Inc USA:+1- 718 – 313-0499
    USA:+1-718-305-1757
    E-mail ID: training@NewyorkSys.com
    Visit: http://www.NewyorkSys.com

  • SAP HR/HCM Training | SAP HR/HCM Placement | SAP Human Resource / Human Capital Management Training,

    SAP HR/HCM Online Course. NewyorkSys is one of the leading top IT Training and Placement Company in US, with a Good placement track record. We have Certified Trainers. We will provide Online Training, Fast Track Online Training, with Job Assistance. SAP Technologies, Data Warehousing Tools, Oralce Technologies, SAS, JAVA, .NET, BA . ATTEND FREE DEMO CLASS. NewyorkSys Inc. – USA: +1- 718-313-0499;+1-718-305-1757 E: training@newyorksys.com

  • NEWYORKSYS: SAP FICO Training | SAP FICO Placement | sap finance controlling training | SAP FICO Onl

    SAP FICO Online Course. NewyorkSys is one of the leading top IT Training and Placement Company in US, with a Good placement track record. We have Certified Trainers. We will provide Online Training, Fast Track Online Training, with Job Assistance. SAP Technologies, Data Warehousing Tools, Oralce Technologies, SAS, JAVA, .NET, BA . ATTEND FREE DEMO CLASS. NewyorkSys Inc. – USA: +1- 718-313-0499;+1-718-305-1757 E: training@newyorksys.com

  • NEWYORKSYS: SAP FICO Training | SAP FICO Placement | sap finance controlling training | SAP FICO Onl

    SAP FICO Online Course. NewyorkSys is one of the leading top IT Training and Placement Company in US, with a Good placement track record. We have Certified Trainers. We will provide Online Training, Fast Track Online Training, with Job Assistance. SAP Technologies, Data Warehousing Tools, Oralce Technologies, SAS, JAVA, .NET, BA . ATTEND FREE DEMO CLASS. NewyorkSys Inc. – USA: +1- 718-313-0499;+1-718-305-1757 E: training@newyorksys.com

  • Sap Training

    Sappractices offers customized SAP training based on changing demands of SAP industry.Our SAP Certified experts are real time working professionals having multiple domain implementation experience.They have worked on almost all the verticals and add on technologies.They have an experience in handling the critical issues.Our Training model not only focus on key technologies,it also brings the knowledge feeding on other related technologies,based on the job market.

    sappractices@gmail.com
    http://www.sappractices.com

  • SAP Training Manuals

    Teach yourself SAP with our SAP Training Course Manuals. This is the best low cost solution to learning SAP. Our training course is easy to follow, up-to-date, well formatted, uses plain language and is illustrated in colour. We supply all our documents in Microsoft Word format so you can easily customise the manuals for your project.

  • SAP SD Certification Material

    It is a website that you can use to build you SAP SD skills and prepare for SAP Certification. SAP sample questions, books, tips&tricks are provided. Good luck with certification!

Submit More
;;