You are on page 1of 89

AB1013- Internal Tables and Open SQL v1.

India SAP CoE, Slide 1

AB1013- Internal Tables and Open SQL v1.0


1 2

PrepareMe TellMe
3

ShowMe
LetMe HelpMe

5
India SAP CoE, Slide 2

AB1013- Internal Tables and Open SQL v1.0


1 2

PrepareMe TellMe
3

ShowMe
LetMe HelpMe

5
India SAP CoE, Slide 3

Introduction
Internal Table: Provide a means of taking data from a fixed structure and storing it in working memory in ABAP Is a dynamic sequential dataset in which all records have the same structure and a key Part of the ABAP type concept Access individual records in an internal table using either the index or the key Various operations like Read, Write, Modify, Copy, Sort can be performed.

India SAP CoE, Slide 4

Introduction
Open SQL ABAP code is portable between database systems . To access the database in an ABAP program open SQL is used. Open SQL is subset and variation of ANSI SQL. ABAP interpreter passes all open SQL statements to the database interface part of the work process there they are converted to the SQL native to the database used.

India SAP CoE, Slide 5

Purpose Of Internal Table


Internal Table serves the following purpose: internal table is a collection of a variable number of records used for temporary storage and exist or live only during the runtime of an ABAP/4 program Internal tables and database tables work 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 the program.

India SAP CoE, Slide 6

Characteristic Of Internal Table


Dynamic memory management

Temporary storage of data on Application server


Data only available during the program execution

Structured data types in ABAP


Data used for manipulation within the program

India SAP CoE, Slide 7

AB1013- Internal Tables and Open SQL v1.0


1 2

PrepareMe TellMe
3

ShowMe
LetMe HelpMe

5
India SAP CoE, Slide 8

Structure Of Internal Tables


Data type Data type of an internal table is defined by its line type , Key and Table Type Line type: The line type of an internal table can by any ABAP data type Key: Key is used to define Lines in the table . Keys can be either be user defined or Unique Table Type: The table type defines how ABAP accesses individual table entries. There are three kinds of internal table types: Standard Table Sorted Table Hashed Table

India SAP CoE, Slide 9

Structure Of Internal Tables


Data Object Created using the data type of an internal table or directly as an internal table Always fully typed with line type, key and table type. Since internal tables are dynamic data objects that can have any number of lines of a particular type. The number of lines in an internal table is only restricted by the concrete capacity limits of the system installation.

India SAP CoE, Slide 10

Types of Internal Table And Their Usage


Standard Internal Tables They have a linear index Can access them using either the index or the key. If you use the key, the response time is in linear relationship to the number of table entries. This is the quickest way to access table entries. To fill a standard table, append lines using the (APPEND) statement. You should read, modify and delete lines by referring to the index. Response time for accessing a standard table is in linear relation to the number of table entries. This table type is particularly appropriate if you want to address individual table entries using the index.
India SAP CoE, Slide 11

Types of Internal Table And Their Usage


Sorted Internal Tables Sorted tables have a linear key, and, like standard tables. The key of a sorted table can be either unique, or nonunique. Since the system automatically uses a binary search. Sorted tables are appropriate for partially sequential processing in a LOOP. Response Time: The response time for access using the key is in logarithmic relation to the number of table entries. Use: This table type is particularly suitable if you want the table to be sorted while you are still adding entries to it.

India SAP CoE, Slide 12

Types of Internal Table And Their Usage


Hashed Internal Tables Hashes tables have no internal linear index. You can only access hashed tables by specifying the key. The key of a hashed table must be unique, and you must specify UNIQUE in the table definition. Response Time : The response time is constant, regardless of the number of table entries, since the search uses a hash algorithm. Use: This table type is particularly suitable if you want mainly to use key access for table entries.

India SAP CoE, Slide 13

Types Of Internal Table And Their Usage


Index Tables Index table is only used to specify the type of generic parameters in a FORM or FUNCTION. That means that you can't create a table of type INDEX. Internal tables are not DB tables. Standard and Sorted tables in combined are basically called as Index tables and nothing else. Here is the hierarchy .

India SAP CoE, Slide 14

Types Of Internal Table And Their Usage

Internal Tables with header line You use a work area as an interface for transferring data to and from the table. When you read data from an internal table, the contents of a specified line overwrites the contents of the work area. Then, you can reference the contents of the work area in your program. When you write data to an internal table, you must first enter the data in the work area and then can transfer the data to the internal table.

India SAP CoE, Slide 15

Types Of Internal Table And Their Usage


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 hold the 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 used implicitly. To access internal tables WITHOUT header lines, you must specify a work area explicitly in the corresponding ABAP/4 statements.

India SAP CoE, Slide 16

AB1013- Internal Tables and Open SQL v1.0


1 2

PrepareMe TellMe
3

ShowMe
LetMe HelpMe

5
India SAP CoE, Slide 17

Internal Table Creation-Methods


Internal table creation methods:

Creating internal tables using data type.


Creating Internal Tables by Referring to Another Table.

Creating Internal Tables by Referring to a Structure

India SAP CoE, Slide 18

How To Process Internal Tables


Assigning Internal Tables Like other data objects, you can use internal tables as operands in a MOVE statement MOVE <itab1> TO <itab2> <itab2> = <itab1> These statements assign the entire contents of table <itab1> to table <itab2>, including the data in any nested internal tables. The original contents of the target table are overwritten. In case of internal tables with header lines, header line and the body of the table have the same name. If you want to address the body of the table in an assignment, you must place two brackets ([ ]) after the table name.

India SAP CoE, Slide 19

How To Process Internal Tables


Sorting Internal Tables Standard or hashed table can be sorted (by its key) as follows: SORT <itab> [ASCENDING|DESCENDING] [AS TEXT] [STABLE]. The statement sorts the internal table <itab> in ascending order by its key. The sort order depends on the sequence of the standard key fields in the internal table. You can specify the direction of the sort using the additions ASCENDING and DESCENDING. The default is ascending. Caution: If an internal table is statically recognizable as a sorted table, the SORT statement causes a syntax error.
India SAP CoE, Slide 20

How To Process Internal Tables


Initializing Internal Tables CLEAR <itab>. This statement restores an internal table to the state it was in immediately after you declared it. This means that the table contains no lines. Memory already occupied by the memory up until you cleared it remains allocated to the table. REFRESH <itab>. To ensure that the table itself has been initialized, you can use this statement. This always applies to the body of the table. FREE <itab>. You can use FREE to initialize an internal table and release its memory space.
India SAP CoE, Slide 21

How To Process Internal Tables


Comparing Internal Tables Like other data objects, you can use internal tables as operands in logical expressions. .... <itab1> <operator> <itab2> ... For <operator>, all operators listed in the table in Comparisons Between Data Types can be used (EQ, =, NE, <>, ><, GE, >=, LE, <=, GT, >, LT, <). If you are using internal tables with header lines, remember that the header line and the body of the table have the same name. If you want to address the body of the table in a comparison, you must place two brackets ([ ]) after the table name.
India SAP CoE, Slide 22

How To Process Internal Tables


MODIFY - Change an internal table MODIFY itab [FROM wa] [INDEX idx]. Changes an entry in the internal table itab . If you specify FROM wa , the line is replaced by the explicitly specified work area wa . If the FROM specification is omitted, the line is replaced by the header line from itab . The return code value is set as follows: When specifying the insertion point with INDEX idx : SY-SUBRC = 0 The change was executed. SY_SUBRC = 4 The index specification was too big. The change was not executed because the table had fewer than idx entries.
India SAP CoE, Slide 23

Working With Internal Tables


ABAP/4 recognizes essentially the following operations on internal tables: APPEND Appends the contents of the 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

India SAP CoE, Slide 24

Working With Internal Tables Continued


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 area SORT Sorts the internal table CLEAR Deletes the work area or the internal table REFRESH Deletes the internal table FREE Releases space in memory previously occupied by table

India SAP CoE, Slide 25

Working With Internal Tables


Filling Internal Tables Additions: APPEND [wa TO|INITIAL LINE TO] itab: Appends a new line to the end of the internal table itab. APPEND LINES OF itab1 [FROM idx1] [TO idx2] TO itab2: Appends the internal table itab1 or a block of lines from itab1 to the end of the internal table itab2. APPEND [wa TO] itab SORTED BY f. Inserts the new entry into table and re-sorts the table by the sub-field f in descending order. This only makes sense if the table was sorted beforehand.

India SAP CoE, Slide 26

Working With Internal Tables


Using Loops with an Internal Table You can process an internal table using the loop statement LOOP AT ... ENDLOOP. With each loop pass the system places the next table entry in the work area <wa> or the header line of the internal table <itab>. You can restrict the entries which are read using the WHERE addition in the same way as you do when using the SELECT command. At the beginning of each loop pass, SY-TABIX is set to the value of the current table entry.

India SAP CoE, Slide 27

Working With Internal Tables


Reading an Entry from an Internal Table Additions: INTO wa: If the system finds an appropriate entry, it uses the specified work area wa as the output area. COMPARING f1 f2 ... : If the system find an entry, the system compares the subfields f1, f2, ... with the corresponding fields of the work area . TRANSPORTING f1 f2 ... If the system finds an entry, it transfers only specified fields f1 f2 ... the other subfields remain unchanged

India SAP CoE, Slide 28

Reading Entry from Internal Table: (Continued)


Specifying the Search Key: (Linear Search) To use the table key of <itab> as a search key, enter <key> as follows: READ TABLE <itab> FROM <wa> <result>. READ TABLE <itab> WITH TABLE KEY <k1> = <f 1> ... <k n> = <f n> <result>. Binary Search in Standard Tables If you read entries from standard tables using a key other than the default key, you can use a binary search instead of the normal linear search. To do this, include the addition BINARY SEARCH in the corresponding READ statements. READ TABLE <itab> WITH KEY <k1> = <f1>... <kn> = <fn> <result> BINARY SEARCH.

India SAP CoE, Slide 29

Working With Internal Tables


Deleting an Internal Table If you are using an internal table without a header line, CLEAR <itab> deletes the body of the table. If your internal table has a header line, CLEAR <itab> deletes the header line. If you only want to address the body of an internal table with a header line, you use the form <itab>[]. REFRESH <itab> deletes the body of the table.

India SAP CoE, Slide 30

Working With Internal Tables


Changing an Internal Table Addition: MODIFY itab [FROM wa] [INDEX idx] [TRANSPORTING f1 ... fn]: Changes a single entry in the internal table itab, specifying the key explicitly or implicitly.

MODIFY TABLE itab [FROM wa] [TRANSPORTING f1 ... fn]: Generic change to a single entry in the internal table itab with key. MODIFY itab [FROM wa] TRANSPORTING f1 ... fn WHERE cond: Changes several entries in the internal table itab where WHERE condition matches.
India SAP CoE, Slide 31

Working With Internal Tables


Information about Internal Tables (DESCRIBE) You can get information about an internal table using the DESCRIBE TABLE statement: Places the number of filled lines of the table itab into the field n. The value returned to n has type I. The OCCURS addition Places the value of the OCCURS or INITIALSIZE parameter from the definition of itab into the variable n. The value returned ton has the type I.

India SAP CoE, Slide 32

Working With Internal Tables


Fields Of Internal Tables SY-TABIX Current line of an internal table. SY-TABIX is set by the statements below, but only for index tables. The field is either not set or is set to 0 for hashed tables. APPEND sets SY-TABIX to the index of the last line of the table, that is, it contains the overall number of entries in the table. LOOP AT sets SY-TABIX to the index of the current line at the beginning of each loop lass. At the end of the loop, SY-TABIX is reset to the value that it had before entering the loop. READ TABLE sets SY-TABIX to the index of the table line read. If you use a binary search, and the system does not find a line, SY-TABIX contains the total number of lines.
India SAP CoE, Slide 33

Working With Internal Tables


Fields Of Internal Tables SY-TFILL After the statements DESCRIBE TABLE, LOOP AT, and READ TABLE, SY-TFILL contains the number of lines in the relevant internal table.

SY-TLENG After the statements DESCRIBE TABLE, LOOP AT, and READ TABLE, SY-TLENG contains the length of the lines in the relevant internal table.

India SAP CoE, Slide 34

Control Breaks
A LOOP which processes an internal table, you can use special control structures for control break processing. All these structures begin with AT and end with ENDAT. The sequence of statements which lies between them is then executed if a control break occurs.

You can use these key words for control break processing with internal tables only if a loop is actively processing an internal table and reference is to the innermost currently active loop.
At the start of a new control level (i.e. immediately after AT), the following occurs in the output area of the current LOOP statement:
India SAP CoE, Slide 35

Control BreaksContinued
All character type fields (on the right) are filled with "*" after the current control level key. Between AT and ENDAT, you can use SUM to insert the appropriate control totals in the numeric fields of the LOOP output area (on the right) after the current control level key. Summing is supported both at the beginning of a control level ( AT FIRST, AT NEW f) and also the end of a control level ( AT END OF f, AT LAST). At the end of the control level processing (i.e. after ENDAT),The old contents of the LOOP output area are restored.

India SAP CoE, Slide 36

Control BreaksContinued
When calculating totals, you must ensure that the totals are inserted into the same sub-fields of the LOOP output area as those where the single values otherwise occur. If there is an overflow, processing terminates with a runtime error. If an internal table is processed only in a restricted form (using the additions FROM, TO and/or WHERE with the LOOP statement), you should not use the control structures for control level processing because the interaction of a restricted LOOP with the AT statement is currently not properly defined.

India SAP CoE, Slide 37

Overview-Open SQL
Overview of Open SQL Set of ABAP statements that perform operations on the central database in the R/3 System. The results of the operations and any error messages are independent of the database system in use. ABAP programs that only use Open SQL statements will work in any R/3 System, regardless of the database system in use. In Open SQL statements, views are handled in exactly the same way as database tables.

India SAP CoE, Slide 38

Open SQL Commands

India SAP CoE, Slide 39

Return Codes
SY-SUBRC After every Open SQL statement, the system field SY-SUBRC contains the value 0 if the operation was successful, a value other than 0 if not.

SY-DBCNT After an open SQL statement, the system field SY-DBCNT contains the number of database lines processed.

India SAP CoE, Slide 40

Reading Data
Defining Selections Specifying a Target Area Specifying Database Tables Selecting Lines Grouping Lines Selecting Groups of Lines Specifying a Sort Order Sub queries

India SAP CoE, Slide 41

Reading Data
The Open SQL statement for reading data from database tables is: SELECT <result> INTO <target> FROM <source> [WHERE <condition>] [GROUP BY <fields>] [HAVING <cond>] [ORDER BY <fields>]. The SELECT statement is divided into a series of simple clauses, each of which has a different part to play in selecting, placing, and arranging the data from the database.
India SAP CoE, Slide 42

Methods Used for Data Selection


Reading A Single Line. Reading Several Lines. Reading The Whole Lines. Reading Single Columns. Specifying Columns Dynamically .

India SAP CoE, Slide 43

Reading a Single Line From Database Table


To read a single entry from the database, use the following: SELECT SINGLE <cols> ... WHERE ... Specify values for all of the fields of the primary key of the table in the WHERE clause to ensure that lines can be uniquely identified. If the WHERE clause does not contain all of the key fields, the syntax check produces a warning, and the SELECT statement reads the first entry that it finds that matches the key fields that you have specified. If the system finds a line with the corresponding key, SY-SUBRC is set to 0, otherwise to 4.
India SAP CoE, Slide 44

Reading Several Lines From Database Tables


To read a several entries from the database, use the following: SELECT [DISTINCT] <cols> ... WHERE ...... Without Distinct- The system reads all of the lines that satisfy the WHERE condition With Distinct - the system excludes duplicate entries. If the target area is not an internal table, but a flat structure, an ENDSELECT statement must be included after the SELECT statement SELECT [DISTINCT] <cols> ... WHERE ... ENDSELECT. If at least one line is read, SY-SUBRC is set to 0 after the statement (or loop) has been processed. If no lines are read, SY-SUBRC is set to 4.
India SAP CoE, Slide 45

Reading All Columns From The Database Tables


To read all of the columns in the database table, use the following: SELECT <lines> * ...

This reads all columns for the specified lines. The data type of the selected lines is a structure with exactly the same data type as the database table in the ABAP Dictionary. For better performance read the columns that you need in your program and never read all the columns.

India SAP CoE, Slide 46

Reading Columns Dynamically


You can specify <cols> dynamically as follows: SELECT <lines> (<itab>) ... The parentheses must include the name of an internal table <itab> that is either empty or contains <s1 > <s2 > ... to specify the columns or aggregate expressions to be read. For this purpose, the line type of <itab> must be a type C field with a maximum length of 72. If the internal table is empty, the system reads all columns.

India SAP CoE, Slide 47

Specifying internal Tables While Selecting


SELECT ... INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE <itab> [PACKAGE SIZE <n>] ... If you select more than one line, the target area may be either tabular (internal table) or flat. If the target area is flat, you need to use a SELECT loop. With INTO clause- all existing lines in the table are deleted . With APPENDING clause - the new lines are added to the existing internal table <itab>. Fields in the internal table not affected by the selection are filled with initial values.

India SAP CoE, Slide 48

Specifying Single Fields While Selecting


To read data into single fields, use the following in the INTO clause: SELECT ... INTO (<f1>, <f 2>, ...). ... Specify as many individual fields <f i > as are specified in the field list of the SELECT clause. The fields in the SELECT clause are assigned, from left to right, to the fields in the list in the INTO clause.

India SAP CoE, Slide 49

Specifying Database Tables While Selection


Specifying

Database Tables Statically. Specifying Database Tables Dynamically Specifying Two or More Database Tables as an Inner Join. Difference Between Inner And Left Outer Join. Specifying Two or More Database Tables as a Left Outer Join. Client Handling Disabling Data Buffering Restricting the Number of Lines.

India SAP CoE, Slide 50

Using FORM Clause While Selection


The FROM clause determines the database tables from which the data specified in the SELECT clause is read. Either a single table or more than one table can be specified using inner or outer joins. The names of database tables may be specified statically or dynamically You can use alias names. Syntax: SELECT ... FROM <tables> <options> ...

India SAP CoE, Slide 51

Specifying Two Or More Database Tables As Inner Join


SELECT ... ... FROM <tab> [INNER] JOIN <dbtab> [AS <alias>] ON <cond> <options> ... Used to read from more than one table in a single SELECT statement, such that the data in the tables all has to meet the same conditions. A join expression links each line of <tab> with the lines in <dbtab> that meet the condition <cond>. If <dbtab> does not contain any lines that meet the condition <cond>, the line from <tab> is not included in the selection.
India SAP CoE, Slide 52

Specifying Two Or More Database Tables As Inner JoinContinued

The syntax of the <cond> condition is like that of the WHERE clause, although individual comparisons can only be linked using AND. Each comparison must contain a column from the righthand table <dbtab>.

India SAP CoE, Slide 53

Specifying Two Or More Database Tables As Left Outer JoinContinued


SELECT ... ... FROM <tab> LEFT [OUTER] JOIN <dbtab> [AS <alias>] ON <cond> <options> ... Used to read lines from the left-hand database table or join even if there is no corresponding line in the righthand table. The tables are linked in the same way as the inner join with the one exception that all lines selected from <tab> are included in the final selection. If <dbtab> does not contain any lines that meet the condition <cond>, the system includes a single line in the selection whose columns from <dbtab> are filled with null values.
India SAP CoE, Slide 54

Selecting Lines Using Where Condition


The WHERE clause restricts the number of lines selected by specifying conditions that must be met. SYNTAX - SELECT ... WHERE <cond> ... The <cond> conditions in the WHERE clause can be comparisons or a series of other special expressions.

India SAP CoE, Slide 55

Comparison
SELECT ... WHERE <s> <operator> <f> ... <f> can be another column in a database table from the FROM clause.

India SAP CoE, Slide 56

Linking Conditions
SELECT ... WHERE <cond 1> AND <cond 2> ... SELECT ... WHERE <cond 1> OR <cond 2> ... NOT takes priority over AND, and AND takes priority over OR. Negating Conditions : SELECT ... WHERE <s> [NOT ] IN <sub query> ... The condition is true if <cond> is false, and false if <cond> is true. Dynamic Conditions SELECT ... WHERE (<itab>) ... where <itab> is an internal table with line type C and maximum length 72 characters.
India SAP CoE, Slide 57

Tabular Condition
SELECT ... FOR ALL ENTRIES IN <itab> WHERE <cond> ... Data is selected from the database table for all entries in the internal table specified. For each line, the system selects the lines from the database table that satisfy the condition. Duplicate lines are automatically eliminated from the result set. If <itab> is empty, the addition FOR ALL ENTRIES is disregarded, and all entries are read. Can use FOR ALL ENTRIES to replace nested select loops by operations on internal tables. Prerequisite: Table used with for all entries should not be empty.

India SAP CoE, Slide 58

Grouping Lines
The GROUP BY clause summarizes several lines from the database table into a single line of the selection.

India SAP CoE, Slide 59

Selecting groups of lines


The HAVING clause uses conditions to restrict the number of groups selected.

India SAP CoE, Slide 60

Specifying A Sort Order By


Sorting by the Primary Key Sorting by any Columns Specifying the Columns Dynamically

India SAP CoE, Slide 61

Specifying A Sort Order

The ORDER BY clause sorts the lines in the selection according to the contents of their columns. Variants: 1. ... ORDER BY PRIMARY KEY: 2. ... ORDER BY f1 ... fn 3. ... ORDER BY (itab)
India SAP CoE, Slide 62

Updating Database Tables Directly.


Inserting Lines into Tables Changing Lines

Deleting Lines
Inserting or Changing Lines

India SAP CoE, Slide 63

Inserting and Changing Lines Of tables


Inserting lines into database table Adds new records to a database table . You can specify the name of the database table either directly in the program in the form dbtab or at runtime as the contents of the field dbtabname. In either case, the database table must be declared in the ABAP Dictionary . Changing lines of database table Updates values in a database table. You can specify the name of the database table either directly in the form dbtab or at runtime as contents of the field dbtabname.

India SAP CoE, Slide 64

Deleting lines
Deletes lines from a database table. You can specify the name of the database table either in the program itself with DELETE FROM dbtab ... or at runtime as the contents of the field dbtabname with DELETE FROM (dbtabname) .. In both cases, the database table must be known to the ABAP Dictionary .

India SAP CoE, Slide 65

AB1013- Internal Tables and Open SQL v1.0


1 2

PrepareMe TellMe
3

ShowMe
LetMe HelpMe

5
India SAP CoE, Slide 66

Internal Table Objects


Sorted Table TYPES: BEGIN OF LINE, COLUMN1 TYPE I, COLUMN2 TYPE I, COLUMN3 TYPE I, END OF LINE. TYPES ITAB TYPE SORTED TABLE OF LINE WITH UNIQUE KEY COLUMN1.

Hashed Table TYPES VECTOR TYPE HASHED TABLE OF I WITH UNIQUE KEY TABLE LINE.

India SAP CoE, Slide 67

Creating Internal Tables Using Data Types

TYPES VECTOR TYPE SORTED TABLE OF I WITH UNIQUE KEY TABLE LINE. DATA: ITAB TYPE VECTOR, JTAB LIKE ITAB WITH HEADER LINE. DATA: BEGIN OF <itab> OCCURS <n>, ... <fi> ..., ... END OF <itab>.

India SAP CoE, Slide 68

Creating Internal Tables Using Data Types

TYPES VECTOR TYPE SORTED TABLE OF I WITH UNIQUE KEY TABLE LINE. DATA: ITAB TYPE VECTOR, JTAB LIKE ITAB WITH HEADER LINE. DATA: BEGIN OF <itab> OCCURS <n>, ... <fi> ..., ... END OF <itab>.

India SAP CoE, Slide 69

Assigning Internal Tables


DATA: BEGIN OF LINE, COL1, COL2, END OF LINE. DATA: ETAB LIKE TABLE OF LINE WITH HEADER LINE, FTAB LIKE TABLE OF LINE. LINE-COL1 = 'A'. LINE-COL2 = 'B'. APPEND LINE TO ETAB. MOVE ETAB[] TO FTAB. LOOP AT FTAB INTO LINE. WRITE: / LINE-COL1, LINE-COL2. ENDLOOP. The output is: AB
India SAP CoE, Slide 70

Initializing Internal Tables


DATA: BEGIN OF LINE, COL1, COL2, END OF LINE. DATA ITAB LIKE TABLE OF LINE. LINE-COL1 = 'A'. LINE-COL2 = 'B'. APPEND LINE TO ITAB. REFRESH ITAB. IF ITAB IS INITIAL. WRITE 'ITAB is empty'. FREE ITAB. ENDIF. The output is: ITAB is empty.
India SAP CoE, Slide 71

Comparing Internal Tables


DATA: BEGIN OF LINE, COL1 TYPE I, COL2 TYPE I, END OF LINE. DATA: ITAB LIKE TABLE OF LINE, JTAB LIKE TABLE OF LINE. DO 3 TIMES. LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 2. APPEND LINE TO ITAB. ENDDO.

India SAP CoE, Slide 72

Comparing Internal TablesContinued


MOVE ITAB TO JTAB. LINE-COL1 = 10. LINE-COL2 = 20. APPEND LINE TO ITAB. IF ITAB GT JTAB. WRITE / 'ITAB GT JTAB'. ENDIF. APPEND LINE TO JTAB. IF ITAB EQ JTAB. WRITE / 'ITAB EQ JTAB'. ENDIF.

India SAP CoE, Slide 73

Comparing Internal tablesContinued


LINE-COL1 = 30. LINE-COL2 = 80. APPEND LINE TO ITAB. IF JTAB LE ITAB. WRITE / 'JTAB LE ITAB'. ENDIF. LINE-COL1 = 50. LINE-COL2 = 60. APPEND LINE TO JTAB. IF ITAB NE JTAB. WRITE / 'ITAB NE JTAB'. ENDIF. IF ITAB LT JTAB. WRITE / 'ITAB LT JTAB'. ENDIF. The output is: ITAB GT JTAB ITAB EQ JTAB
India SAP CoE, Slide 74

Determining The Attributes Of Internal Table


DATA: BEGIN OF LINE, COL1 TYPE I, COL2 TYPE I, END OF LINE. DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1 INITIAL SIZE 10. DATA: LIN TYPE I, INI TYPE I, KND TYPE C. DESCRIBE TABLE ITAB LINES LIN OCCURS INI KIND KND. WRITE: / LIN, INI, KND.

India SAP CoE, Slide 75

Continued
DO 1000 TIMES. LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 2. INSERT LINE INTO TABLE ITAB. ENDDO. DESCRIBE TABLE ITAB LINES LIN OCCURS INI KIND KND. WRITE: / LIN, INI, KND. The output is: 0 10 H 1,000 10 H
India SAP CoE, Slide 76

Open SQL Reading DataContinued


Selecting Lines DATA: BEGIN OF LINE, CARRID TYPE SPFLI-CARRID, CONNID TYPE SPFLI-CONNID, CITYFROM TYPE SPFLI-CITYFROM, CITYTO TYPE SPFLI-CITYTO, END OF LINE, ITAB LIKE TABLE OF LINE. LINE-CITYFROM = 'FRANKFURT'. LINE-CITYTO = 'BERLIN'. APPEND LINE TO ITAB. LINE-CITYFROM = 'NEW YORK'. LINE-CITYTO = 'SAN FRANCISCO'. APPEND LINE TO ITAB.
India SAP CoE, Slide 77

Open SQL Reading DataContinued


Selecting Lines SELECT CARRID CONNID CITYFROM CITYTO INTO CORRESPONDING FIELDS OF LINE FROM SPFLI FOR ALL ENTRIES IN ITAB WHERE CITYFROM = ITAB-CITYFROM AND CITYTO = ITAB-CITYTO. WRITE: / LINE-CARRID, LINE-CONNID, LINE-CITYFROM, LINE-CITYTO. ENDSELECT. The output is as follows: AA 0017 New York San Francisco
India SAP CoE, Slide 78

Open SQL Reading Data..Continued


Grouping Lines DATA: CARRID TYPE SFLIGHT-CARRID, MINIMUM TYPE P DECIMALS 2, MAXIMUM TYPE P DECIMALS 2. SELECT CARRID MIN( PRICE ) MAX( PRICE ) INTO (CARRID, MINIMUM, MAXIMUM) FROM SFLIGHT GROUP BY CARRID. WRITE: / CARRID, MINIMUM, MAXIMUM. ENDSELECT.

India SAP CoE, Slide 79

Open SQL Reading DataContinued


Specifying a Sort Order DATA: BEGIN OF WA, CARRID TYPE SFLIGHT-CARRID, CONNID TYPE SFLIGHT-CONNID, MIN TYPE I, END OF WA. SELECT CARRID CONNID MIN( SEATSOCC ) AS MIN INTO CORRESPONDING FIELDS OF WA FROM SFLIGHT GROUP BY CARRID CONNID .

India SAP CoE, Slide 80

Open SQL Reading Data..Continued


WRITE: / WA-CARRID, WA-CONNID, WA-MIN. ENDSELECT. The output is as follows: AA 123 456 AA 0017 456 AZ 123 343

India SAP CoE, Slide 81

AB1013- Internal Tables and Open SQL v1.0


1 2

PrepareMe TellMe
3

ShowMe
LetMe HelpMe

5
India SAP CoE, Slide 82

Tips and Tricks


Performance Tuning Following are the key points which should be taken care of for performance tuning: Proper use of selection conditions. Using Views instead of database tables Using Into Table Clause. Modifying a group of lines. Use of binary search option. Using Table buffering Using proper structure of Where clause.

India SAP CoE, Slide 83

Tips and Tricks


Use of Selection conditions

Instead of selecting all the data and doing the processing during the selection, it is advisable to restrict the data to the selection criteria itself, rather than filtering it out using the ABAP code.

India SAP CoE, Slide 84

Tips and Tricks


Using Views Instead Of Database Tables

Many times ABAP programmers deal with base tables and nested selects. Instead it is always advisable to see whether there is any view provided by SAP on those base tables, so that the data can be filtered out directly, rather than specially coding for it.

India SAP CoE, Slide 85

Tips and Tricks


Using Into Table Clause

Instead of appending one record at a time into an internal table, it is advisable to select all the records in a single shot. Modifying Group of Lines
Use the variations of the modify command to speed up this kind of processing.

India SAP CoE, Slide 86

Tips and Tricks


Use Of Binary Search Option

When a programmer uses the read command, the table is sequentially searched. This slows down the processing. Instead of this, use the binary search addition. The binary search algorithm helps faster search of a value in an internal table. It is advisable to sort the internal table before doing a binary search.

India SAP CoE, Slide 87

Tips and Tricks


Using Table buffering

Use of buffered tables is recommended to improve the performance considerably. The buffer is bypassed while using the following statements Select distinct Select for update Order by, group by, having clause Joins Use the Bypass buffer addition to the select clause in order to explicitly bypass the buffer while selecting the data.

India SAP CoE, Slide 88

Tips and Tricks


Using proper structure of Where clause When a base table has multiple indices, the where clause should be in the order of the index, either a primary or a secondary index. To choose an index, the optimizer checks the field names specified in the where clause and then uses an index that has the same order of the fields. One more tip is that if a table begins with MANDT, while an index does not, there is a high possibility that the optimizer might not use that index. In certain scenarios, it is advisable to check whether a new index can speed up the performance of a program. This will come handy in programs that access data from the finance tables.

India SAP CoE, Slide 89