Creating Internal Tables

Embed Size (px)

Citation preview

  • 7/29/2019 Creating Internal Tables

    1/16

    Creating Internal Tables

    Like other elements in theABAP type concept, you can declare internal tables as abstract data types in

    programs or in the ABAP Dictionary, and then use them to define data objects. Alternatively, you can definethem directly as data objects. When you create an internal table as a data object, you should ensure that onlythe administration entry which belongs to an internal table is declared statically. The minimum size of aninternal table is 256 bytes. This is important if an internal table occurs as a component of an aggregated dataobject, since even empty internal tables within tables can lead to high memory usage. (In the next functionalrelease, the size of the table header for an initial table will be reduced to 8 bytes). Unlike all other ABAP dataobjects, you do not have to specify the memory required for an internal table. Table rows are added to anddeleted from the table dynamically at runtime by the various statements for adding and deleting records.

    An internal table is a collection of a variable number of records. They are used for temporary storage andexist or live only during the runtime of an ABAP/4 program. Internal tables and database tableswork together. The contents of a database table can be copied to an internal table at runtime, so that the

    internal table is a snapshot of a database table and can then be used as a working copy of the data within theprogram.

    Internal tables are used as containers for volatile data in a program.

    TYPES: BEGIN OF struct1,col1 TYPE i,BEGIN OF struct2,col1 TYPE i,col2 TYPE i,

    END OF struct2,END OF struct1.

    TYPES mytype TYPE struct1-struct2-col2.

    The example shows how you can construct a nested structure type struct1 with a complexcomponent struct2 by nesting TYPES BEGIN OF ... TYPES END OF blocks, and how you can address theinner components.

    * Local types in program* referring to predefined ABAP types:

    TYPES: surname(20) TYPE c,street(30) TYPE c,zip_code(10) TYPE n,city(30) TYPE c,phone(20) TYPE n,date LIKE sy-datum.

    * Local structure in program* referring to the above types

    TYPES: BEGIN of address,name TYPE surname,code TYPE zip_code,town TYPE city,

    http://help.sap.com/saphelp_nw04/helpdata/EN/fc/eb2fb2358411d1829f0000e829fbfe/content.htmhttp://help.sap.com/saphelp_nw04/helpdata/EN/fc/eb2fb2358411d1829f0000e829fbfe/content.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld001.htmhttp://help.sap.com/saphelp_nw04/helpdata/EN/fc/eb3660358411d1829f0000e829fbfe/frameset.htmhttp://help.sap.com/saphelp_nw04/helpdata/EN/fc/eb2fb2358411d1829f0000e829fbfe/content.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld001.htm
  • 7/29/2019 Creating Internal Tables

    2/16

    str TYPE street,END OF address.

    * Local nested structure in program* referring to the above types

    TYPES: BEGIN of phone_list,adr TYPE address,tel TYPE phone,

    END OF phone_list.

    This example shows how to create complex data types from simple type definitions. After a set of simple datatypes are created with ABAP predefined types, a structured type address is defined using the data typesdefined earlier. Finally, a nested structure type, phone_list , is created, whose first component has thetype address.

    TYPES: BEGIN OF struct1,col1 TYPE i,

    BEGIN OF struct2,col1 TYPE i,col2 TYPE i,

    END OF struct2,END OF struct1.

    TYPES mytype TYPE struct1-struct2-col2.

    The example shows how you can construct a nested structure type struct1 with a complexcomponent struct2 by nesting TYPES BEGIN OF ... TYPES END OF blocks, and how you can address theinner components.

    Comparison of Transparent, Pool and Cluster tables

    By YSP, Defiance technologies

    Transparent Pool Cluster

    Contain a single table.Used to store masterdata

    They are used to hold alarge number of very smalltables(stores customizingdata or system data)

    They are used to hold datafrom a few number of largetables.(stores system data)

    It has a one-to-onerelationship with a tablein the database

    It has a many-to-onerelationship with a table inthe database

    It has a many-to-onerelationship with table inthe database

    For each transparenttable there is oneassociated table in thedatabase

    It is stored with otherpooled tables in a singletable called table pool inthe database

    Many cluster tables arestored in a single table inthe database called a tablecluster

    The database table hasthe same name, samenumber of fields and thefields have the samenames

    The database table hasdifferent name, differentnumber of fields and fieldshave different names

    The database table hasdifferent name, differentnumber of fields and fieldshave different names

    There is only a singletable

    Table pools contain moretables than table clusters

    Contains less tables thantable pools

    Single table can have

    one or more primary key

    Primary key of each table

    does not begin with samefields or fields

    Primary key of each table

    begins with same fields orfields

  • 7/29/2019 Creating Internal Tables

    3/16

    Secondary indexes canbe created

    Secondary indexes cannotbe created

    Secondary indexes cannotbe created

    They can be accessedusing open and nativeSQL

    They can be accessedusing open SQL only

    They can be accessedusing open SQL only

    USE: They are used tohold master data e.g.Table vendors or tableof customers. Exampleof transaction data isorders placed bycustomers

    USE: They reduce theamount of databaseresources needed whenmany small tables have tobe opened at the sametime

    USE: They would be usedwhen the tables haveprimary key in commonand data in these tablesare all accessessimultaneously

    An internal table is a sequence of lines with the same type.

    If you want to define a table type, follow the line type declaration with the addition OCCURS 0.

    You can also write OCCURS instead of OCCURS 0, where is any integer.

    Creating Internal Table Data Types

    To create an internal table data type, you use the TYPES statement as follows:

    Syntax

    TYPES OCCURS .

    This creates an internal table data type by using the OCCURS option of the TYPES statement. The linesof the internal table have the data type specified in . To specify the data type of the lines, you can useeither the TYPE or the LIKE parameter.

    By using the LIKE parameter to refer to an object defined in the ABAP/4 Dictionary, you can create internaltables which have the same line structure as objects stored in the Dictionary, and which reflect the structure ofdatabase tables. This is very important when reading and processing database tables

    specifies an initial number of lines. Memory is reserved for the number of lines specified as soon as thefirst line is written to an internal table data object created with type . If more lines are added to an internaltable than specified by , the reserved memory expands automatically. If there is not enough space inmemory for an internal table, it is written to a buffer or to the disk (paging area).

    TYPES VECTOR TYPE I OCCURS 10.

    This example creates an internal table data type VECTOR which has lines consisting of the elementary type Ifield.

    Creating Internal Tables by Referring to Another Table

    To create an internal table data object by referring to an existing internal table data type or data object, youuse the DATA statement as follows:

    Syntax

  • 7/29/2019 Creating Internal Tables

    4/16

    DATA [WITH HEADER LINE].

    You can use the option to refer to a table data type or table data object by using TYPE or LIKE . Thedata object is declared as an internal table with the same structure.

    If you use the WITH HEADER LINE option, the internal table is created with a table work area .

    If you want to create an internal table with a header line, the line type cannot directly be an internal table.However, it can be a structure which has internal tables as components.

    TYPES: BEGIN OF LINE,

    COLUMN1 TYPE I,

    COLUMN2 TYPE I,

    COLUMN3 TYPE I,

    END OF LINE.

    TYPES ITAB TYPE LINE OCCURS 10.

    DATA TAB1 TYPE ITAB.

    DATA TAB2 LIKE TAB1 WITH HEADER LINE.

    As shown in Creating Internal Table Data Types , this example creates a data type ITAB as an internal table.The data object TAB1 has the same structure as ITAB by referring to ITAB using the TYPE parameter of theDATA statement. The data object TAB2 has the same structure by referring to TAB1 using the LIKE

    parameter of the DATA statement. TAB2 is created with header line. Therefore, the table work area TAB2 canbe addressed in the program by using TAB2-COLUMN1, TAB2-COLUMN2, and TAB2-COLUMN3.

    Creating Internal Tables by Referring to a Structure

    To create an internal table data object by referring to an existing line structure, you use the DATA statementas follows:

    Syntax

    DATA OCCURS [WITH HEADER LINE].

    This creates an internal table by using the OCCURS option of the DATA statement. The lines of theinternal table have the data type specified in . To specify the data type, you can use either the TYPE orthe LIKE parameter.

    By using the LIKE parameter to refer to an object defined in the ABAP/4 Dictionary, you can create internaltables which have the same line structure as objects stored in the Dictionary, and which reflect the structure ofdatabase tables. This is very important when reading and processing database tables

    specifies an initial number of lines. Memory is reserved for the number of lines specified as soon as thefirst line is written to an internal table data object created with type . If more lines are added to an internaltable than specified by , the reserved memory expands automatically. If there is not enough space inmemory for an internal table, it is written to a buffer or to the disk (paging area).

    The features described above are the same as those for creating internal table data types with the TYPESstatement.

  • 7/29/2019 Creating Internal Tables

    5/16

  • 7/29/2019 Creating Internal Tables

    6/16

  • 7/29/2019 Creating Internal Tables

    7/16

    As you have already seen, internal tables (arrays or matrices in other terminology) are a set of lines with thesame type(s).

    To define an internal table, simply use the addition OCCURS when you declare a data object withthe desired line type.

    The OCCURS addition turns the data object into an internal table with the same line type as that ofthe data object. It also determines the number of lines with which the internal table is created. Unlikethe array concept of other programming languages, ABAP/4 can increase the number of lines in thetable dynamically at runtime.

    If you do not know how big your internal table will be, set the OCCURS addition to 0. If you know thatyour internal table will be smaller than 8KB, specify the number of table lines using the OCCURSparameter. This ensures that only this amount of memory area is occupied. This is particularlyimportant when you are working with nested structures. If the memory area is insufficient, further tablelines are allocated.

    Purpose of Internal Tables

    In ABAP/4, youwork mainly with tables. Tables are the essential data structures in the R/3 System. Long-life data (also known as persistent data) is stored in relational database tables.

    http://sap.mis.cmich.edu/sap-abap/abap04/sld010a.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld010a.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld010a.htm
  • 7/29/2019 Creating Internal Tables

    8/16

    Besides database tables, you can create internal tables which exist only during the runtime of yourprogram. ABAP/4 provides various operations for working with internal tables. You can, for example,search for, append, insert, or delete lines.

    The number of lines in an internal table is notfixed. Depending on requirements, the system increasesthe size of internal tables at runtime. If, for example, you want to read a database table into an internal

    table, you do not have to know the size of the database table in advance. This feature makes working withinternal tables an easy task and also supports dynamic programming.

    You can use internal tables to perform table calculations on subsets of database tables. For example, youcan read a certain part of a database table into an internal table. From the internal table, you can thencalculate totals or generate a ranked list.

    Another use for internal tables is reorganizing the contents of database tables according to the needs ofyour program. For example, you can read data relevant for creating a telephone list from one or severallarge customer tables into an internal table. During the runtime of your program, you can then access thislist directly without having to perform a time-consuming database query for each call.

    Besides using them when working with data from database tables, internal tables are an important featurein ABAP/4 for implementing very complex data structures in your program.

    Structure of Internal Tables

    In ABAP/4, you can distinguish between internal table data types, which define the structure of internaltables, and internal table data objects, which are the actual internal tables and can be filled with data. Aninternal table data type is an abstract definition of a data structure which can be used to declare dataobjects as internal tables.

    Data type

    An internal table is one of the two structured data types in ABAP/4. The other structured data type is thefield string. An internal table consists of any number of lines which all have the same data type. The datatype of the lines can be elementary or structured. This definition opens a variety of internal tablestructures which range from lines consisting of one field to lines consisting of field strings which haveinternal tables as components.

    You can define a data type as an internal table by using the TYPES statement with the OCCURSparameter. No memory is occupied when defining a data type.

    Data object

    A data object which has a data type defined as an internal table is the actual internal table youworkwith. Itoccupies memory and you can fill or read its lines.

    You create a data object as an internal table by using the DATA statement either with the OCCURSparameter or by referring to another internal table by using the TYPE or LIKE parameters.

    http://sap.mis.cmich.edu/sap-abap/abap04/sld010b.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld010b.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld010b.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld010b.htm
  • 7/29/2019 Creating Internal Tables

    9/16

    Working with Internal Tables

    In contrast with the array concepts found in other programming languages, ABAP/4 does NOT use directaccess to table entries. Instead, operations on table entries are carried out using a work area. This is atemporary holding area that contains the content of the current entry for almost ALL commands dealing withthe table.

    This work space may be either a separately defined work area or a header line that is defined whenthe table is defined.

    ABAP/4 recognizes essentially the following operations on internal tables:

    Command Effect

    APPEND Appends the contents ofthe work area at the end of the internal table

    COLLECT Inserts the cumulative contents of the work area into the internal table

    INSERT Inserts the contents of the work area into a particular table entry

    MODIFY Overwrites a particular table entry with the content of the work area

    DELETE Deletes a specified entry from the internal table

    LOOP AT Places the entries of an internal table in a work area one at a time

    READ TABLE Places a single internal table entry in a work areaSORT Sorts the internal table

    http://sap.mis.cmich.edu/sap-abap/abap04/sld011.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld011.htm
  • 7/29/2019 Creating Internal Tables

    10/16

    CLEAR Deletes the work area or the internal table

    REFRESH Deletes the internal table

    FREE Releases space in memory previously occupied by table

    Internal Tables With Header Line

    You access internal tables record by record. You must use a workarea as an interface for transferring data toand from the table.

    When you read data from an internal table, the contents of a specified table record or line overwrites thecontents of the work area. Then, you can reference the contents of the work area in your program. When youwrite data to an internal table, you must first enter the data in the work area from with the system can transferthe data to the internal table.

    http://sap.mis.cmich.edu/sap-abap/abap04/sld012.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld012.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld012.htm
  • 7/29/2019 Creating Internal Tables

    11/16

    To avoid inconsistencies, it is beneficial if the work area has the same data type as the records orlines of the internal table. A safe procedure for creating work areas which are compatible with internaltables is to use the same data type for declaring both the internal table and the work area.

    In ABAP/4, you can distinguish between two kinds of internal tables:

    Internal tables WITH header lines

    Internal tables WITHOUT header lines

    A header line is similar to a work area for a database table. A work area is used as temporarystorage for one entry of a database table. In a similar way, a header line is used to hold one line of aninternal table.

    An internal table with header line is a tuple from a work area (header line) and the bulk of the tableitself. Both are addressed using the same name, the interpretation of the name is context-sensitive.Hence it would stand for the header line in a MOVE statement, but would stand for the bulk of thetable in a SEARCH statement.

    When you create an internal table WITH a header line, a work area is created automatically with thesame data type as the rows of the internal table. The header line and the internal table have the samename. Then, the system uses this work area implicitly.

    In ABAP/4 statements for accessing internal tables, you can specify the work area to be used. For

    internal tables with header lines, you can leave out this specification. Then, the system uses thetable work area implicitly.

  • 7/29/2019 Creating Internal Tables

    12/16

    Internal Tables Without Header Line

    If a table does not have a header line, you MUST provide a work area as a separate record to holdthe content of the current entry for most commands used in processing tables.

    Internal tables WITHOUT a header line do NOT have a table work area declared which can be usedimplicitly. To access internal tables WITHOUT header lines, you must specify a work area explicitly inthe corresponding ABAP/4 statements.

    It is a matter of your preference whether you use a header line or a work area.

    Remember that, for internal tables with header lines, the internal table itself and the table work areashave the same name. If you use the name in a statement, the system interprets it as the name of thetable work area and NOT as the table itself. In some statement, however, you can enter squarebrackets after the name to address the internal table instead of the table work area asfollows: [ ].

    http://sap.mis.cmich.edu/sap-abap/abap04/sld013.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld013.htm
  • 7/29/2019 Creating Internal Tables

    13/16

    Declaring Internal Tables with Header Line

    You define an internal table with a header line using the addition WITH HEADER LINE.

    A HEADER line is a work area where the currently active, individual record in the table resides.

    http://sap.mis.cmich.edu/sap-abap/abap04/sld014.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld014.htm
  • 7/29/2019 Creating Internal Tables

    14/16

    Using Loops with an Internal Table

    You can process an internal table using the loop statement LOOP AT ... ENDLOOP. With each looppass the system places the next table entry in the work area or the header line of the internaltable .

    You can restrict the entries which are read using the WHERE addition in the same way as you dowhen using the SELECT command.

    At the beginning of each loop pass, SY-TABIX is set to the value of the current table entry. When thesystem leaves the LOOP, SY-TABIX has the same value as it had before the loop started.

    http://sap.mis.cmich.edu/sap-abap/abap04/sld020.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld020.htm
  • 7/29/2019 Creating Internal Tables

    15/16

    Reading an Entry from an Internal Table

    You use the READ TABLE statement to read a single table entry. When the entry has beenread, it is in thework area or the header line of .

    If the entry is successfully read, the value of SY-SUBRC is zero, otherwise it is not equal to zero. SY-TABIX takes the value of the table entry read.

    READ TABLE INDEX . The nth table entry is read.

    If you wish to specify individual fields as a search argument you can use the following syntax: READTABLE WITH KEY = = ... = . In this case, the first entry from is read which corresponds with the components specified in ....

    If the internal table is sorted by the search argument, you can use the BINARY SEARCH addition.The system then carries out a more performance-efficient binary search for the specified entry.

    The online documentation for the READ statement contains details of further additions.

    http://sap.mis.cmich.edu/sap-abap/abap04/sld021.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld021.htmhttp://sap.mis.cmich.edu/sap-abap/abap04/sld021.htm
  • 7/29/2019 Creating Internal Tables

    16/16

    Information about Internal Tables

    You can get information about an internal table using the DESCRIBE TABLE statement:

    The LINES addition returns the number of entries currently in the table.

    The OCCURS addition returns the number of OCCURS in the table definition.

    You can display information about any data object using the DESCRIBE FIELD statement (see the onlinedocumentation for the DESCRIBE statement).