Sei sulla pagina 1di 52

Working With Tables.

Internal Tables

Difference between Internal Tables and Work Areas

Types of Internal Tables


Creating Internal Tables Populating Internal Tables

Reading Internal Tables


Deleting from Internal tables Sub-Totals.

Difference between Internal Tables and Work Areas


INTERNAL TABLES - Internal tables are used to obtain data from a fixed structure for dynamic use in ABAP. - Each line in the internal table has the same field structure. - The main use for internal tables is for storing and formatting data from a database table within a program. WORK AREAS - Work areas are single rows of data. - It should have the same format as any of the internal tables. - It is used to process the data in an internal table one line at a time.

Difference between Internal Tables and Work Areas (contd)

WORK AREA

Types of Internal Tables


With Header line - System automatically creates a work area for the internal table.

- Work area has same data type as internal table.


- Work area is called the header line.

Without Header line - Internal tables without header line do not have a work area. - Work area to be explicitly defined to access such internal tables.

Creating Internal Tables


By using Type statement By using the Data statement - By referring to another table - By referring to a structure - With a new structure

Creating Internal Tables


By using Type statement

Types : begin of line, column1 type I, column2 type I, end of line.


Using TYPES we cannot create an internal table. Data itab type line occurs 10.

Internal table itab is created with the same structure as internal table data type line.

Creating Internal Tables (contd.)


By using the Data statement - By referring to another table - By referring to a structure - With a new structure In the first two cases Header line is optional. In the third case Header line is created by default.

Creating Internal Tables (contd.)


By referring to another table

Syntax Data <f> <type> [with header line]. <type> refers to a table data type or table data objects using type or like. Here internal table <f> is created of the type <type>.

Example: DATA t_line TYPE line OCCURS 10 with header line.

Creating Internal Tables (contd.)


By referring to existing structure Syntax

Data <f> <type> occurs n [with header line].


The lines of the internal table <f> have the data type specified in <type> (Can use either like or type). Example: DATA flight_tab LIKE sflight OCCURS 10.

Creating Internal Tables (contd.)


With a new structure Syntax

Data : Begin of <f> occurs <n>, <component declaration>, , End of <f>. Work area is created by default.
Example: Data : Begin of itab occurs 10, column1 type I, column2(4) type C, column3 like mara-ernam, End of itab.

Populating Internal Tables


Append data line by line Syntax APPEND [<wa> TO / INITIAL LINE TO] <itab>. - Appends new line to internal table <itab>. - <wa> is source area which is appended. - INITIAL LINE TO adds a line initialized with the correct value for its type to the table.

Note: sy-tabix contains the index of the appended line.

Populating Internal Tables (contd.)...


Example: Data: Begin of itab occurs 10, col1 type C, col2 type I, end of itab. Do 3 times. Append initial line to itab. itab-col1 = sy-index. itab-col2 = sy-index ** 2. Append itab. Enddo. Loop at itab. write :/itab-col1, itab-col2. endloop.

Populating Internal Tables (contd.)...


Result: 0 1 0 4 0 9

1
2 3

CAUTION : Using APPEND may give duplicate entries.

APPEND works even if a line with same standard key already exists.

Populating Internal Tables (contd.)...


Using COLLECT statement Syntax COLLECT [<wa> INTO] <itab>. Note: Use COLLECT statement for tables having unique standard keys. Incase of tables with header line omit INTO option. If an entry with same standard key exists new line is not appended but adds the numeric fields to existing contents.

Else collect acts in a similar way as append.


Sy-tabix contains the index of the processed line.

Populating Internal Tables (contd.)...


Example: Data: Begin of itab occurs 3, column1(3) type C, column2(2) type N, column3 type I, End of itab. itab-column1 = abc. itab-column2 = 12. itab-column3 = 3. collect itab. write /sy-tabix. itab-column1 = def. itab-column2 = 34. itab-column3 = 5. collect itab. write/sy-tabix.

Populating Internal Tables (contd.)...


itab-column1 = abc. itab-column2 = 12. itab-column3 = 7. collect itab. write /sy-tabix. Loop at itab. write :/itab-column1, itab-column2, itab-column3. Endloop.

Populating Internal Tables (contd.)...


Result: 1 2 1 abc 12 def 34

10 5

Populating Internal Tables (contd.)...


Using INSERT statement Syntax INSERT [<wa> INTO / INITIAL LINE INTO] <itab> [index <idx>].

Note: New entry has index <idx> and following line has index <idx>+1. If less than <idx>-1 entries then cannot insert line and sy-subrc = 4.

Populating Internal Tables (contd.)...


Example: Data: Begin of line, col1 type I, col2 type I, End of line. Data itab like line occurs 10. Do 2 times. Line-col1 = sy-index. Line-col2 = sy-index ** 2. Append line to itab. Enddo . Line-col1 = 11. Line-col2 = 22. Insert line into itab index 2. Insert initial line into itab index 1. Loop at itab into line. Write: / sy-tabix, Line-col1, Line-col2. Endloop.

Populating Internal Tables (contd.)...


Result: 1 2 3 4 0 1 11 2 0 1 22 4

Note: New line containing values is inserted before second line. Initialized line is inserted before the first line.

Appending lines of internal table.


To append part or all of an internal table Syntax APPEND LINES OF <itab1> [FROM <n1>] [TO <n2>] TO <itab2>. Note: Without the FROM and TO options, this statement appends the entire table <itab1> to <itab2>.

Inserting lines of an internal table


To insert part or all of an internal table into another internal table Syntax INSERT LINES OF <itab1> [FROM <n1>] [TO <n2>] INTO <itab2> [INDEX <idx>]. Note:

Lines are inserted before the line having index <idx>.


These two methods of appending and inserting are faster than appending or inserting line by line in a loop.

Copying internal tables


To copy entire contents of one table into another in one execution Syntax MOVE <itab1> To <itab2>. OR <itab1> = <itab2>. Note: Copies the entire contents of one table into another in one execution. Original data in target table overwritten. For tables with header line use [] to distinguish it from work area since both have the same name.

Copying internal tables (contd.)


Syntax MOVE-CORRESPONDING <itab1> TO <itab2>.. - Executes the statement for their header lines. Searches for the sub-fields which occur both in itab1 and itab2 and then generates, for all relevant field pairs which correspond to the sub-fields ni , statements of the form MOVE itab1-ni TO itab2-ni. The other fields remain unchanged.

Reading internal tables


Using Loop Endloop Syntax LOOP AT <itab> [INTO <wa>] [FROM <n1>] [TO <n2>] [WHERE <condition>]. .. ENDLOOP. Note: Reads the internal table line by line.

sy-subrc is set to 0 even if only one line is read.

Reading internal tables (contd.)


Using READ statement Syntax READ TABLE <itab> [INTO <wa>] INDEX <idx>. Note: If entry with specified index is found then sy-subrc is set to 0. sy-tabix contains index of that line. If <idx> is less than 0, run-time error occurs. If <idx> exceeds table size then sy-subrc is set to 4.

Reading internal tables (contd.).


Reading lines using self defined keys Syntax READ TABLE <itab> [INTO <wa>] WITH KEY <k1> = <f1> ..<kn> = <fn>. <k1><kn> are the table components. <f1>..<fn> are the values the fields must match. Reading parts of single line using transporting Syntax READ TABLE <itab> [INTO <wa>] <key option> TRANSPORTING <fields>. The components specified in <fields> are transported to <wa>.

Deleting from Internal Tables


Deleting lines in a loop Syntax DELETE <itab>. This is processed only within a loop. Deletes the current line. Can give conditional statements. Deleting lines using the index Syntax DELETE <itab> INDEX <idx>. The line with index <idx> is deleted. The index of following line is decremented by 1.

Deleting from Internal Tables (contd.)


Deleting adjacent duplicate entries Syntax DELETE ADJACENT DUPLICATES FROM <itab> COMPARING <f1>, <f2>, .. Those entries having same values for the specified fields <f1>,<f2>,... are deleted. Deleting selected lines

Syntax DELETE <itab> [FROM <n1>] [TO <n2>] [ WHERE <condition>]. CAUTION: If no WHERE condition is given, all lines between <n1> and <n2> are deleted.

Initializing internal table

Syntax

REFRESH <itab>.
If no header line in internal table then use CLEAR <itab>. In case of table with header line CLEAR <itab> clears only the <wa>. So use CLEAR <itab>[].

Sub Totals
at first - endat Processed at the beginning of the table . Executes the relevant series of statements just once at new f - endat Processed when f changes or one of the preceding fields has just changed. at end of f endat at last - endat Processed at the end of the table Executes the relevant series of statements just once

SUB TOTALS (Contd)

SUM.
When processing an internal table in a block starting with LOOP and concluded by ENDLOOP , SUM calculates the control totals of all fields of type I , F and P (see also ABAP/4 number types ) and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).

Types of SQLs
Native SQL Open SQL

Open SQL commands


SELECT INSERT

UPDATE
MODIFY DELETE OPEN CURSOR, FETCH, CLOSE CURSOR

Open SQL Return codes


Sy-subrc - Return value after specific ABAP/4 statements

Sy-dbcnt - DB operations: Number of elements in the edited dataset

SELECT - syntax
SELECT <result> INTO <target> FROM <source> [WHERE <condition>] [GROUP BY <fields>] [HAVING <cond.>] [ORDER BY <fields>].

SELECT statements
Reading Single Entries e.g. Select single * from customers

Getting statistical information using max, min, avg, sum


Reading each line using Select .. EndSelect e.g. Select * from Customers. Write :/ Customers-name EndSelect Reading all the table entries using Select . e.g. Select * from Customers into table all_Customers

FROM variants
... FROM <dbtabname> ... FROM <tabref1> [INNER] JOIN <tabref2> ON <cond> ... FROM <tabref1> LEFT [OUTER] JOIN <tabref2> ON <cond>

SELECT INTO variants


... INTO <wa> ... INTO CORRESPONDING FIELDS OF <wa>

... INTO (f1, ..., fn)


... INTO TABLE <itab> ... INTO CORRESPONDING FIELDS OF TABLE <itab> ... APPENDING TABLE <itab> ... APPENDING CORRESPONDING FIELDS OF TABLE <itab>

SELECT Loop function


On change of SELECT * FROM T100 WHERE SPRSL = SY-LANGU AND MSGNR < '010' ORDER BY PRIMARY KEY. ON CHANGE OF T100-ARBGB. ULINE. WRITE: / '***', T100-ARBGB, '***'. ENDON. WRITE: / T100-MSGNR, T100-TEXT. ENDSELECT.

Native SQL
EXEC SQL [PERFORMING <form>]. <Native SQL statements> ENDEXEC.

Example: DATA: BEGIN OF WA, CONNID TYPE SPFLI-CONNID, CITYFROM TYPE SPFLI-CITYFROM, CITYTO TYPE SPFLI-CITYTO, END OF WA. DATA C1 TYPE SPFLI-CARRID VALUE 'LH'. EXEC SQL PERFORMING LOOP_OUTPUT. SELECT CONNID, CITYFROM, CITYTO INTO :WA FROM SPFLI WHERE CARRID = :C1 ENDEXEC. FORM LOOP_OUTPUT. WRITE: / WA-CONNID, WA-CITYFROM, WA-CITYTO. ENDFORM.

Open SQL Performance rules


Keep the Result Set Small Using the where clause
If only one record is required from the database, use SELECT SINGLE whenever possible .

Minimize the Amount of Data Transferred Restrict the number of lines


If only certain fields are required from a table, use the SELECT <field1> <field2> INTO statement

Restrict no of columns Use aggregate functions Minimize the Number of Data Transfers Avoid nested select loops
An alternative option is to use the SELECT .. FOR ALL ENTRIES statement. This statement can often be a lot more efficient than performing a large number of SELECT or SELECT SINGLE statements during a LOOP of an internal table.

Open SQL Performance rules (contd.)..


Use dictionary views Use Joins in the FROM clause Use subqueries in the where clause Minimize the Search Overhead Use index fields in the where clause
When accessing databases, always ensure that the correct index is being used .

Reduce the Database Load Buffering Logical databases Avoid repeated database access Using Internal Tables to Buffer Records To avoid executing the same SELECT multiple times (and therefore have duplicate selects), an internal table of type HASHED can be used to improve performance.

Dynamic Selections
Used when the table name or the precise condition of the WHERE clause is not known.

Using Select-Options e.g. select-options sname for customers-name. select * from customers into table all_customers where name in sname.
Dynamic Table Naming e.g . Data: tablename(10) . move customers to tablename. Select * from tablename.

Export and Import


Types of Memory ABAP memory Within the same internal session SAP memory Across internal sessions

Export and Import (contd.).


EXPORT obj1 ... objn TO MEMORY. Exports the objects obj1 ... objn (fields, structures or tables) as a data cluster to ABAP/4 memory . EXPORT obj1 ... objn TO DATABASE dbtab(ar) ID key. Exports the objects obj1 ... objn (fields, structures or tables) as a data cluster to the database table dbtab. IMPORT f itab FROM MEMORY. Imports data objects (fields or tables) from the ABAP/4 memory . Reads in all data without an ID that was exported to memory with "EXPORT ... TO MEMORY." IMPORT f itab FROM DATABASE dbtab(ar) ID key. Imports data objects (fields, field strings or internal tables) with the ID key from the area ar of the database dbtab .

ABAP Memory
Using Temporary Storage e.g. export all_customers all_bookings to memory id CUSTBOOK. import all_customers all_bookings from memory id CUSTBOOK.

Using Clustered Tables e.g. export all_customers all_bookings to database zflight(zz) id CUSTBOOK.

SAP Memory
SET PARAMETER ID <pid> FIELD <f>.
Writes the contents of the field f to the global SAP memory under the key pid . If the key already contains a value, it is overwritten.

GET PARAMETER ID <pid> FIELD <f>.


Transfers the value stored under the key pid from the global user-related SAP memory to the field f .

PARAMETERS or SELECT-OPTIONS MEMORY ID extension


On the selection screen, assigns the memory ID pid to the parameter or left range limit of the select-option i.e. when you execute the report, the selection screen displays the last value which the user entered in a field with the memory ID pid .

Match Codes
Devices for users to find values stored in SAP database system based on defined criteria. For retrieving data based on non-key fields. It is a keyword used to call the Search help for the Selection Screen

element.

Transaction for creating Match Code Object is SE11.

Demo
Its demo time Over to R/3

Thank You.
Do send feedback, queries and suggestions

Potrebbero piacerti anche