52
ABAP Training Internal Tables

Internal Tables 343411325828443

  • Upload
    dhivya

  • View
    9

  • Download
    0

Embed Size (px)

DESCRIPTION

sap internal tables

Citation preview

ABAP Training

Internal Tables

ABAP Training Internal Tables 2

Topics of the Session:

Concept of internal table Types of internal tables Declaration of internal table Commands related to internal tables Exercise

ABAP Training Internal Tables 3

Internal Tables

Database tables store long-life data Internal tables store temporary data

Table calculations on subsets of database tables Implementing complex data structures Reorganize the contents of database tables according to specific

processing needs Generate ranked lists Combine contents from more than one database table into a

single table for easier processing

ABAP Training Internal Tables 4

Internal Tables

Used as Snapshots of database tables Containers for volatile data

Exist only at runtime, (unlike database tables) Consist of any number of records Note that it is not necessary to declare the initial size of the table: sap’s

memory management allows the table to be ‘infinitely’ extensible.

ABAP Training Internal Tables 5

ABAP Internal Table Types

Choose table type (and access method) by most frequently performed operation Standard tables

Access by linear table index or key Response time proportional to table size

Sorted tables Filled in sorted order Access by linear index or sort key Response time logarithmically proportional to table size

Hashed tables Direct access (only) by table key Constant, fast response time

ABAP Training Internal Tables 6

Internal Table Types

ABAP offers standard, sorted, & hashed types of internal tables. The type of table that you should use (and hence the access method) is determined by the operations that will be performed most frequently with the table.

Standard tablesMost appropriate for general table operations. Accessed by referring to the table index (which is the quickest access method). Access time for standard table increases linearly with respect to the number of table entries. New rows are appended to the table. Individual rows are accessed by reading the table at a specified index value.

ABAP Training Internal Tables 7

Internal Table Types

Sorted tablesMost appropriate where the table is to be filled in sorted order. Sorted tables are filled using the INSERT statement. Inserted entries are sorted according to a sort sequence defined by the table key. Illegal entries are recognized as soon as you try to insert them into the table. Response time is logarithmically proportional to the number of table entries (since the system always uses a binary search). Sorted tables are particularly useful if you wish to process row by row using a LOOP.

Hashed tablesMost appropriate when access to rows is by a key. (Cannot access hashed tables via the table index)response time remains constant regardless of the number of rows in the table.

ABAP Training Internal Tables 8

Hierarchy of Internal Table Types

ABAP Training Internal Tables 9

When to Use Which Table Type

ABAP Training Internal Tables 10

Standard Tables (History)

Release 2.2 Only standard tables with header lines Table structure determined with BEGIN OF <itab> OCCURS

<n>… END OF <itab>. Release 3.0

Header lines optional Introduction of type concept

Release 4.0 Introduction of sorted & hashed table types Allows key definition & uniqueness attributes Downward compatibility of 2.2 & 3.0 tables

ABAP Training Internal Tables 11

Declaring Internal Tables

DATA: <itab> TYPE <itabkind> of <linetpye>[WITH [UNIQUE | NON-UNIQUE] <keydef>][INITIAL SIZE <n>][With header line].

<itabkind> [STANDARD] TABLE | SORTED TABLE |Hashed table | any table

<keydef> KEY <f1>…<fn> |Key table line |

Default key

ABAP Training Internal Tables 12

Tables With Header Line

Program Work Area

R/3 Database

ABAP Training Internal Tables 13

Tables Without Header Line

Program Work Area

R/3 Database

ABAP Training Internal Tables 14

Internal Tables - Header Lines

Advantages of header lines Convenient declaration

Table and header line structure declared in same statement Ease of use

Don’t have to explicitly move data into the work area structure and then append the work area structure to the table

Some statements require a table with header line Disadvantages of header lines

Performance - I/O is faster for tables without header line Cannot use in embedded structure

ABAP Training Internal Tables 15

Internal Tables - Header Lines

Default declaration is w ithout a header line To declare a table with a header line

Data: itab type standard table of <type> with header lineTypes : Begin of GS_xtype,

var1 type c,var2 type I,..

End of GS_xtype.Data book_tab type standard table of GS_xtype with header

line.

ABAP Training Internal Tables 16

Internal Table Definition Syntax

ABAP Training Internal Tables 17

Standard Table Declaration

TYPES: begin of LineType,F1,f2,

End of LineType.DATA: itab TYPE STANDARD TABLE OF

LineType [WITH DEFAULT KEY]Initial size 100With header line.

ABAP Training Internal Tables 18

Sorted Table Declaration

TYPES: begin of LineType,F1,f2,

End of LineType.DATA: itab TYPE SORTED TABLE OF

LineType WITH {NON-UNIQUE|UNIQUE} KEY f1

With header line.

ABAP Training Internal Tables 19

Hashed Table Declaration

TYPES: begin of LineType,F1,f2,

End of LineType.DATA: itab TYPE HASHED TABLE OF

LineType WITH UNIQUE KEY f1Initial size 100With header line.

ABAP Training Internal Tables 20

Declaring Internal Tables

with reference to existing internal or database table typeDATA <itab> type <tabtypedef>[WITH HEADER LINE].

with reference to an existing line structureDATA <itab> like <tabtypedef>

[WITH HEADER LINE].

ABAP Training Internal Tables 21

Declaring Tables With a Header Line

Data: begin of itab occurs 1,Field1(8),Field2 type i, …End of itab.

Types: begin of itab_type,Field1(8),Field2 type c, …End of itab_type.

Data: itab type standard table of itab_typeWith header line.

ABAP Training Internal Tables 22

Declaring Tables Without a Header Line

Types: begin of itab_type,Field1(8),Field2 type c, …End of itab_type.

Data: itab1 type standard table of itab_type.

Data: itab2 like itab1.

ABAP Training Internal Tables 23

Do Not Declare Internal Table This Way

Don’tData: begin of INT_tab occurs 0,

EMP_code type i,Name(20),Join_date type d,

End of INT_tab.

ABAP Training Internal Tables 24

Accessing Internal Tables

•work area acts as interface for transferring data•read from table

•contents of table line overwrite work area•program refers to work area

•write to table•first enter data in the work area then transfer to table

work area

table

ABAP Training Internal Tables 25

Generic Key Operations

ABAP Training Internal Tables 26

Clear, Refresh and Free Commands

Clear Deletes the content of the header area of internal table Does not delete the content of the internal table

Refresh Deletes the content of the internal table Does not delete the content of the header area

Free Frees the memory space allocated to the internal table

ABAP Training Internal Tables 27

Data Transfer & Internal Tables

Filling line by line Append, collect, insert

Copying the contents of another table Append, insert, move

Reading line by line Loop, read, search

Determining the attributes of an internal table Describe

ABAP Training Internal Tables 28

Append

Appends new line to itab <wa> TO specifies the source

area <wa> INITIAL LINE TO appends line

with each field set to default values according to field type

To be used only with STANDARD table

Data: itab type standard table of spfli ,itab_wa like spfli.

Select * into itab_waFrom spfli.

Append itab_wa to itab.endselect.

APPEND [ <wa> TO | INITIAL LINE TO] <itab>

ABAP Training Internal Tables 29

COLLECT [ <wa> INTO] <itab>

•used to fill an internal table which has unique table key (or standard key for standard table if no key defined):

–standard key is combination of non-numeric key fields•if table entry exists with the same table key values as the work area <wa> or the header line of the table

–COLLECT adds the contents of the numeric fields of the work area to contents of the numeric fields in the existing entry

•else –COLLECT is the same as APPEND

Collect

ABAP Training Internal Tables 30

Collect

Often used to generate summary information

data: itab type standard table of sflight,wa like sflight.

select * into wa from sflight.collect wa into itab.

endselect.loop at itab into wa

write wa-carrid, wa-connid, wa_fldate, wa-seatsocc.endloop.

standard key fieldsΣsflight-seatsocc

ABAP Training Internal Tables 31

Collect

header line internal table

abc 1 6

def 5 4

abc 1 7

abc 1 6

abc 1 6def 5 4

abc 1 13def 5 4

database

‘abc’ ‘1’ 6

‘def’ ‘5’ 4

‘abc’ ‘1’ 7

insert

accumulate

ABAP Training Internal Tables 32

Insert

New line is inserted before the line which has index <idx> If the table consists of <idx>-1 lines the new line is inserted at the

end of the table If INSERT is used without the INDEX addition it can be used only in

a LOOP…ENDLOOP by adding the new line before the current line NB: insertion by index is not recommended for SORTED

table nor permissible for HASHED table

INSERT [ <wa> INTO | INITIAL LINE INTO] <itab> [INDEX <idx>].

ABAP Training Internal Tables 33

System Fields Used in Table Processing

SY-TABIX Holds the index of the current line in the table

SY-SUBRC Return code for operation

SY-SUBRC = 0 Operation was sucessful

SY-SUBRC <> 0 Operation failed

SY-DBCNT Number of lines that were affected by the operation How many lines have already been processed

ABAP Training Internal Tables 34

Copying Contents of an Internal Table

To append part or all of <itab1> to <itab2>APPEND LINES OF <itab1> [FROM <n1>] [TO <n2>] TO <itab2>.

To insert part or all of <itab1> into <itab2>INSERT LINES OF <itab1> [FROM <n1>] [TO <n2>] INTO <itab2> [INDEX <idx>].

To copy the entire contents of <itab1> into itab2>MOVE <itab1> TO <itab2>.

NB: again, inserting by index w ill create problem w ith SORTED table and not permitted w ith HASHED table

ABAP Training Internal Tables 35

•Reads <itab> line by line•INTO <wa> specifies the target area for tables without header line •FROM & TO specify begin and end index values (for STANDARD and SORTED table only)

data: itab type standard table of spfli,wa like itab.

select * into table itab from spfli.loop at itab into wa.

write: / wa-carrid, wa-connid.endloop.

LOOP AT <itab> [INTO <wa>] [FROM <n1>] [TO <n2>] [WHERE <condition>].

Loop

ABAP Training Internal Tables 36

Branching Out From the Loop

Exit: Control goes to the end of the loop.

Continue: Control goes to the beginning of the loop.

ABAP Training Internal Tables 37

•Reads the line with index <idx> from table <itab>

•faster than accessing the table with the key value•if <idx> < 0 a runtime error occurs•INDEX valid only for STANDARD and SORTED table

•INTO <wa> specifies the target area for tables without header line

data: itab type standard table of spfli,wa like itab.

select * into table itab from spfli.

read table itab into wa index 7.write: / wa-carrid, wa-connid.

READ TABLE <itab> [INTO <wa>] INDEX <idx>.

READ - by INDEX

ABAP Training Internal Tables 38

•1st variation: Reads the line with key values matching those in wa2• 2nd variation: Reads the line with explicit key values specified in <key>

•system reads the first entry of itab which matches the key value

data: begin of rectype,f1 type I,f2 type I,

end of rectype.data: tab type SORTED TABLE of rectype with unique key f1 with header line.Wa-f1 = 1.read table tab from wa.

READ TABLE <itab> FROM <wa2> [INTO <wa>]READ TABLE <itab> WITH TABLE KEY <key> [INTO <wa>]

READ - by TABLE KEY

ABAP Training Internal Tables 39

•Reads the line with key <key> from table <itab>

•system reads the first entry of itab which matches the key value

•INTO <wa> specifies the target area for tables without header line

data: itab type standard table of spfli,wa like itab.

select * into table itab from spfli.read table itab into wa with key carrid = ‘LH’ connid = ‘0400’.

write: / wa-carrid, wa-connid.endloop.

READ TABLE <itab> [INTO <wa>] WITH KEY <key> [BINARY SEARCH].

READ - by KEY

ABAP Training Internal Tables 40

READ - Variations

Defining a sequence of key fieldsWITH KEY <k1> = <f1> … <kn> = <fn>

Defining the entire line as keyWITH KEY = <value>

BINARY SEARCH (useful only in conjuction with STANDARD table and HASHED table) Table must be sorted in the order specified in the key fields prior to

the use of the READ statement with BINARY SEARCH

ABAP Training Internal Tables 41

Describe

Allows you to find out how many lines are contained in your internal table or how large you have defined the OCCURS parameter when you declare <itab>

DESCRIBE TABLE <itab> [LINES <lin>] [OCCURS <occ>].

ABAP Training Internal Tables 42

Changing the Contents of an Internal Table

Changing lines Modify Write to

Not recommended. Does not recognise the structure of a table line. Overwrites section of table line even across fields from start pos for length len

Deleting lines Selected lines in a loop Using the index Adjacent duplicate lines

ABAP Training Internal Tables 43

•Replaces a line in <itab> with contents of <wa>•INDEX option specifies the target line of <itab>

•can be used only with standard and sorted table•without index, the target line is determined by search key

•TRANSPORTING moves only the named fields from <wa> to <itab>

data: itab type standard table of spfli,wa like itab.

select * into table itab from spfli.wa-fldate = ‘19982201’.wa-price = 1912.50.modify itab from wa index 5 transporting fldate price.

MODIFY [TABLE] <itab> [FROM <wa>] [INDEX <idx>] [TRANSPORTING <f1>…<fn>] [WHERE <cond>].

Modify

ABAP Training Internal Tables 44

•Deletes line from itab•can be used only in a loop

data: itab type standard table of spfli,wa like itab.

select * into table itab from spfli.loop at itab into wa.

if wa-carrid = ‘LH’.delete itab.

endloop.

DELETE <itab>.

Delete

ABAP Training Internal Tables 45

•deletes line from itab with index <idx>•after deleting the line the index of the following lines is decremented by one•not allowed with hashed table

data: itab type standard table of spfli.

select * into table itab from spfli.delete itab index 5.

DELETE <itab> INDEX <idx>.

Delete

ABAP Training Internal Tables 46

•deletes all adjacent duplicate lines from itab •without COMPARING

•uses table key to determine duplicates

•with COMPARING•uses the contents of the specified fields in <comp> to determine duplicates

data: itab type standard table of spfli.

select * into table itab from spfli.

delete adjacent duplicates from itab comparing carrid.

DELETE ADJACENT DUPLICATES FROM <itab> [COMPARING <comp>].

Delete

ABAP Training Internal Tables 47

•deletes all lines from itab where index is between n1 and n2•without FROM

•starts from first line•without TO

•deletes to end of table•without WHERE

•unconditional deletion of lines between n1 and n2

data: itab type standard table of spfli.

select * into table itab from spfli.

delete itab from 2 to 12 where carrid = ‘LH’.

DELETE <itab> [FROM <n1>] [TO <n2>] [WHERE <condition>].

Delete

ABAP Training Internal Tables 48

•without BY•uses table key to sort

•<order>•ASCENDING or DESCENDING

•as TEXT•char fields sorted alphabetically rather than by internal binary representation

data: itab type standard table of spfli.

select * into table itab from spfli.

sort itab by carrid connid descending.

SORT <itab> [<order>] [AS TEXT] [BY <f1> [<order>] [AS TEXT] ... [BY <f1> [<order>] [AS TEXT] .

Sort

ABAP Training Internal Tables 49

•Uses only with standard table•builds itab sorted by <f>

•descending sort order •it <itab> is less then 100 lines use APPEND …SORTED BY

•otherwise use SORT after table is populated

•table can contain only the number of lines specified in the OCCURS clause

data: itab type standard table of spfli ,itab_wa like spfli.

select * into itab_wa from spfli.append itab_wa to itab sorted by cityfrom.endselect.

APPEND [<wa> TO] <itab> SORTED BY <f>.

Append…sorted By

ABAP Training Internal Tables 50

Do’s & Don’t

Do not use nested loop Do not select data from

database table within a loop Do not use binary search

without sorting

Use read within a loop Select before the loop and

then read the data or use ‘for all entries’ addition with the select statement

Check whether itab is initial before using ‘for all entries’

ABAP Training Internal Tables 51

Summary of Internal Table Operations

Standard Table Sorted Table Hashed Table

INSERTCOLLECTMODIFYDELETEREADLOOP

INSERTAPPENDMODIFYDELETEREADLOOPSORT

n

No Index Operations

works like appendinserts in correct positionworks like append

see above

sort sequence may be broken

ABAP Training Internal Tables 52

Exercise

1. SELECT DATA FROM VBAP WHERE VBELN LIES BETWEEN 0 AND 100 AND STORE THE DATA IN AN INTERNAL TABLE (ITAB1).

2. MOVE THE DATA FROM ITAB1 TO ITAB2 WHERE VBELN LIES BETWEEN 20 AND 40.

3. DISPLAY THE DAT FROM BOTH THE ITABS.

TIME ALLOCATED: 1 ½ HRS.