Free SAP Training & Tutorial A Free SAP Functional And Technical Tutorial And Training For ABAP, SAP QUERY, ALV, SMARTFORMS, SD, PP, MM, QM, FICO
27.05.2010 | Author: | Posted in ABAP 

Today let’s learn how to create a dynamic internal table using ABAP, in this tutorial I’ll use CREATE_DYNAMIC_TABLE method to generate the internal table.


REPORT Z_CREATE_DYNAMIC_INTERNAL_TABLE.

"A sample Code to generate Dynamic Internal table
"http://www.freesaptutorial.com

DATA: TY_DTRF TYPE REF TO DATA,
      ALVF_CAT TYPE TABLE OF LVC_S_FCAT,
      LS_ALV_CAT LIKE LINE OF ALVF_CAT.

TYPES TABNAME LIKE DCOBJDEF-NAME .
PARAMETER: P_TBNAME TYPE TABNAME.

DATA: BEGIN OF ITAB OCCURS 0.
    INCLUDE STRUCTURE DNTAB.
DATA: END OF ITAB.

DATA: new_line TYPE REF TO data.
FIELD-SYMBOLS: <fs_data> TYPE REF TO data,
       <fs_1> TYPE ANY TABLE,
       <fs_2>,
       <fs_3>.

FIELD-SYMBOLS : <F_FS> TYPE TABLE,
<F_FS1> TYPE TABLE,
<F_FS2> TYPE ANY,
<F_FS3>.

REFRESH ITAB.

"Using NAMETAB_GET to get the tablename structure
CALL FUNCTION 'NAMETAB_GET'
  EXPORTING
  LANGU          = SY-LANGU
  TABNAME        =  P_TBNAME
  TABLES
  NAMETAB        = ITAB
  EXCEPTIONS
  NO_TEXTS_FOUND = 1.

LOOP AT ITAB .
  LS_ALV_CAT-FIELDNAME = ITAB-FIELDNAME.
  LS_ALV_CAT-REF_TABLE =  P_TBNAME.
  LS_ALV_CAT-REF_FIELD = ITAB-FIELDNAME.
  APPEND LS_ALV_CAT TO ALVF_CAT.
ENDLOOP.

*Using function CREATE_DYNAMIC_TABLE to create the internal table
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING IT_FIELDCATALOG = ALVF_CAT
IMPORTING EP_TABLE = TY_DTRF .
ASSIGN TY_DTRF->* TO <F_FS>.

"Select for the transparent table into the internal table
SELECT * FROM (P_TBNAME) INTO CORRESPONDING FIELDS OF TABLE <F_FS>.

  "Now here's how we get the value of the dynamic internal table
  "By using Assign Component [column index] of STRUCTURE
  "The first dynamic internal table is COMPONENT 1

LOOP AT <F_FS> ASSIGNING <F_FS2>.

  "COMPONENT 1 is the first column in Dynamic Internal table

   ASSIGN COMPONENT 1 OF STRUCTURE <F_FS2> TO <F_FS3>.
   WRITE: / <F_FS3>.

   ASSIGN COMPONENT 2 OF STRUCTURE <F_FS2> TO <F_FS3>.
   WRITE: <F_FS3>.

   ASSIGN COMPONENT 3 OF STRUCTURE <F_FS2> TO <F_FS3>.
   WRITE: <F_FS3>.

   ASSIGN COMPONENT 4 OF STRUCTURE <F_FS2> TO <F_FS3>.
   WRITE: <F_FS3>.

   ASSIGN COMPONENT 5 OF STRUCTURE <F_FS2> TO <F_FS3>.
   WRITE: <F_FS3>.

ENDLOOP.

Save and activate the code.

Type in any transparent table name and click execute.

internaltable

Here’s the Dynamic internal table result.

internaltable2

Popularity: 12% [?]

 


 
 

Related Tutorial " ABAP" 

  •  
     
    Recent Tutorials
    Create Table Maintenance Display In SAP Create table display to maintain tables values using SM30
    Create Table Maintenance Generator In SAP Maintaining SAP Transparent table values using maintenance generator
    Table Relationship In SAP Learn How To Find Tables Relationships in SAP
    Convert Number To words Using ABAP Find out how to convert numbers into words Using ABAP
    How To Maintain Long Text Object in SAP Maintain long text object in SAP
    How To Know What TCODE Used In ABAP Program Find TCODE For your ABAP Program Report
    How does CRM Work? Learn how CRM system works to run your business
    Why SAP CRM is the Best Choice? The reasons why CRM application is the best choice
    Difference Between CRM System and CRM Strategy What's the difference between CRM system and its strategy
    CRM for a Better Customer Service Learn how CRM could make your customer service much better
    Costs of Implementing CRM Software Learn the how much cost you need to prepare when implementing CRM
     
    Popular Tutorials
  • (none)
  •  
    Categories
    ABAP   Basis & Configuration  
    ALV SAP BW (Business Warehouse)
    BAPI   SAP Interview Questions
    SAP Tables   SAP Overview
    Transaction Codes FI SAP Query
      MM SAP Script  
      PP SAP Tutorial FI Module
      QM   MM Module
      SD   PM Module
    SAP Functional MM   PP Module
      SD   QM Module
    White Papers     SD Module
    Uncategorized   SmartForms  
      User-Exits  
     
     

     

    Home | About | Contribute | Privacy

    Copyright 2011 freesaptutorial.com.

    Free SAP Tutorial is not associated with SAP AG in Germany or in any other country.

    SAP is a registered trade mark of SAP AG.