"Referring To Local Data Type
"Referring To Local Data Type
"Referring To Local Data Type
program execution and gets destroyed once the program ends. It is used to hold data temporarily.
An internal table can be defined using the keyword TABLE OF in the DATA statement.
Id(10) TYPE n,
Name(20) type c,
END OF ty_student.
Wa-id = 1.
Wa-name = ‘Suneel’.
APPEND wa to Itab1. (itab2 or itab3)
Wa-id = 2.
Wa-name = ‘Nawaz’.
APPEND wa to Itab1. (itab2 or itab3)
Wa-id = 3.
Wa-name = ‘Sandeep’.
APPEND wa to Itab1. (itab2 or itab3)
After the last APPEND statement in the above program, internal table ‘ITAB1’ has the following 3
entries. But the internal table values are not save in DB. The internal table and its values are
discarded once the program ends.
ID Name
1 Suneel
2 Nawaz
3 Sandeep
Usually internal tables are used to hold data from database tables temporarily for displaying on the
screen or further processing. To fill the internal table with database values, use SELECT statement
to read the records from the database one by one, place it in the work area and then APPEND the
values in the work area to internal table.
REPORT ZSAMPLE.
DATA: wa TYPE zproduct,
itab TYPE TABLE OF zproduct.
SELECT * FROM zproduct INTO wa.
APPEND wa TO itab.
ENDSELECT.
After ENDSELECT the internal table ITAB contains all the records that are present in table
ZPRODUCT.
Using INTO TABLE addition to SELECT statement we can also read multiple records directly into
the internal table directly. No work area used in this case. This select statement will not work in loop,
so no ENDSELECT is required.
SELECT * FROM zproduct INTO TABLE itab.
We can use internal tables in two ways:
SELECT * FROM zproduct INTO itab.
ENDSELECT.
SELECT * FROM zproduct INTO wa.
APPEND wa TO itab.
ENDSELECT.
Types of Internal Tables
1. Standard
2. Sorted
3. Hashed
Standard Table - The most commonly used type of table. You can sort and resort this table
anytime. You can have duplicate records in them. Recommend that when accessing these
tables, you sort them by a key and then READ via a BINARY SEARCH - better performance.
No need of defining the Key while declaration. Ex: IT_VEN TYPE STANDARD TABLE OF TY_VEN.
Sorted table - this table has a defined sort sequence. You cannot resort. You can have
duplicates. When you READ these tables, SAP automatically tries to use a BINARY SEARCH.
Must and should define the Key (Unique /Non Unique) while declaration. Ex:
IT_VEN TYPE SORTED TABLE OF TY_VEN WITH UNIQUE KEY lifnr.
IT_MAIL TYPE SORTED TABLE OF TY_MAIL WITH NON-UNIQUE KEY ADDRNUMBER.
Hashed Table - this table has a defined key. Hashed table is useful when you have to work with
very big internal table and to read it.
Must and should define the Key (Unique) while declaration. Ex:
IT_VEN TYPE HASHED TABLE OF TY_VEN WITH UNIQUE KEY lifnr.
Syntax: DESCRIBE TABLE <ITAB> LINES <v_lines> ." Count the no. of
records of a internal table, here v_lines stores count
DATA : V_LINES TYPE I. "Type integer
DESCRIBE TABLE IT_MARA LINES V_LINES ." Count the no. of record of a internal table
Write : v_lines .
Syntax: READ TABLE <ITAB> INTO <WA> WITH KEY <FIELD1> = <FIELD1 VALUE> <FIELD1> =
<FIELD1 VALUE>
BINARY SEARCH.
" Read a record into work area where some field = some value
READ TABLE IT_MARA INTO WA_MARA WITH KEY MATNR = '0001' BINARY SEARCH. "Read a
record into work area where MATNR is '0001'
Using READ TABLE WITH INDEX in SAP ABAP
READ TABLE WITH INDEX is used to read a single record from an internal table into work area
specified by index.
index is 2.
ENDLOOP.
ENDLOOP.
ENDLOOP.
TRANSPORTING is a keyword which is used to specify list particular fields to be modified instead of all
fields.
Syntax1: MODIFY <ITAB> FROM <WA> INDEX <INDEX NO> TRANSPORTING <FIELD1> <FIELD2>
SY-TABIX is a key word which stores the index no of currently processed record.
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
SELECT * FROM MARA INTO TABLE IT_MARA . " GET DATA INTO ITAB IT_MARA
MODIFY IT_MARA FROM WA_MARA INDEX SY-TABIX TRANSPORTING MTART. " NOW THE VALUE OF
SELECT * FROM MARA INTO TABLE IT_MARA . " GET DATA INTO ITAB IT_MARA
MODIFY IT_MARA FROM WA_MARA TRANSPORTING MTART WHERE MATNR = '0001'. " NOW THE
FREE is used to clear (free) memory of a internal table or work area. We all know when ever we
declare a internal table or work area, 8kb memory will be allocated.
Syntax REFRESH : REFRESH <ITAB> "CLEAR ALL RECORDS OF INTERNAL TABLE BUT
REFRESH IT_MARA.
FREE IT_MARA.
TO <ITAB2>.
INSERT LINES OF IT_MARA FROM 3 TO 5 INTO IT_MARA1 INDEX 3 . "DATA IN IT_MARA WILL
Check statement
Continue statement
If the condition Not satisfies or False then only it
goes to next statement.
*DO 4 TIMES.
*write:/ sy-index.
*if sy-index <= 2.
* CONTINUE.
*endif.
*write 'JUMBLE'.
*ENDDO.
o/p: 1
2
3 JUMBLE
4 JUMBLE
If you use EXIT keyword inside IF .. ENDIF., it will comes out of the program.
If you use EXIT inside LOOP .. ENDLOOP., it will come out of loop.
If you use EXIT inside FORM .. ENDFORM., it will comes out of form (subroutine).
*DATA: IT_MARA TYPE TABLE OF MARA,
WA_MARA TYPE MARA.
PARAMETERS: P_MATNR TYPE MARA-MATNR.
SELECT SINGLE * FROM MARA INTO WA_MARA WHERE MATNR = P_MATNR.
IF WA_MARA IS INITIAL.
EXIT. "exit program
else.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
endif.
STOP
STOP is a statement which is used to stop processing an event block, ex: If I have two events
START-OF-SELECTION and END-OF-SELECTION in my program, If I use STOP keyword in
START-OF-SELECTION, the keyword will exits start-of-selection and goes to END-OF-SELECTION.
START-OF-SELECTION.
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
STOP.
Result: NO OUTPUT
START-OF-SELECTION.
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
STOP.
END-OF-SELECTION.
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
ENDLOOP.
AT NEW <FIELD> - This is executed whenever there is a new value on specified field.
AT END OF - This statement is executed whenever the new value ends on specific field.
Report ZCONTROL_BREAK1
TYPES : BEGIN OF TY_VBAP,
VBELN TYPE VBAP-VBELN,
MATNR TYPE VBAP-MATNR,
ZMENG TYPE VBAP-ZMENG,
NETPR TYPE VBAP-NETPR,
END OF TY_VBAP.
DATA : IT_VBAP TYPE TABLE OF TY_VBAP.
DATA : WA_VBAP TYPE TY_VBAP.
PARAMETERS : P_DATE TYPE VBAP-ERDAT .
START-OF-SELECTION.
SELECT VBELN MATNR ZMENG NETPR
FROM VBAP INTO TABLE IT_VBAP
WHERE ERDAT = P_DATE.
LOOP AT IT_VBAP INTO WA_VBAP.
AT FIRST .
WRITE :/2 'SALES DOC', 18 'MATRIAL', 28'QUANTITY', 40 'PRICE'.
ENDAT.
AT NEW VBELN.
WRITE :/2 WA_VBAP-VBELN, 18 WA_VBAP-MATNR, 28 WA_VBAP-ZMENG, 40 WA_VBAP-NETPR.
ENDAT.
AT END OF VBELN.
SUM.
WRITE :/23 'SUB TOTAL IS :' COLOR 6, WA_VBAP-NETPR.
ENDAT.
AT LAST.
SUM.
WRITE:/23 'Total is:', WA_VBAP-NETPR .
ENDAT.
ENDLOOP.
Field symbols are similar to pointers in C language, field symbols does not have any memory
instead they will be pointing to a memory location.
The concept of field symbols is very important in order to increase the performance of SAP
applications.
Field symbol name should always be within <>, example:.
Syntax for declaring a field symbol.
In the below example we are going to use field symbol as work area.
REPORT ZSAPN_FIELDSYMBOLS.
Check weather the field symbol is assigned or not before processing field symbol, it is very
important to check field symbol, if it is not assigned it will get a run-time error,
REPORT ZSAPN_FIELDSYMBOLS1.
DATA : IT_MARA TYPE TABLE OF MARA. "internal table
FIELD-SYMBOLS : <FS_MARA> TYPE MARA. "field symbols
DATA : LV_TIME_START TYPE TIMESTAMPL. "time at start of loop
DATA : LV_TIME_END TYPE TIMESTAMPL. "time at the end of loop
DATA LV_TIME_TAKEN TYPE P DECIMALS 8. "time difference
GET TIME STAMP FIELD LV_TIME_END. "get time at the end of loop
LV_TIME_TAKEN = LV_TIME_END - LV_TIME_START. "time taken
WRITE:/ 'Time taken: ', LV_TIME_TAKEN. "display time taken
Program2
REPORT ZSAPN_FIELDSYMBOLS2.
DATA : IT_MARA TYPE TABLE OF MARA. "internal table
DATA : WA_MARA TYPE MARA. "work area
DATA : LV_TIME_START TYPE TIMESTAMPL. "time at start of loop
DATA : LV_TIME_END TYPE TIMESTAMPL. "time at the end of loop
DATA LV_TIME_TAKEN TYPE P DECIMALS 8. "time difference
GET TIME STAMP FIELD LV_TIME_END. "get time at the end of loop
LV_TIME_TAKEN = LV_TIME_END - LV_TIME_START. "time taken
WRITE:/ 'Time taken: ', LV_TIME_TAKEN. "display time taken
See the time take to process loop in both programs.
REPORT ZCONTROL_BREAK.
TYPES: BEGIN OF ty_marc,
matnr TYPE marc-matnr,
werks TYPE marc-werks,
END OF ty_marc.
DATA: it_marc TYPE STANDARD TABLE OF ty_marc,
wa_marc TYPE ty_marc,
wa_temp TYPE ty_marc.
data: itab type STANDARD TABLE OF marc,
wa type marc.
SELECT matnr werks FROM marc INTO TABLE it_marc UP TO 10 ROWS WHERE matnr gt '
000000000000000040'.
SORT it_marc BY matnr.
FIELD-SYMBOLS : <matnr> type matnr.
WRITE:/ 'FULL TABLE'.
LOOP AT it_marc INTO wa_marc.
wa_temp = wa_marc.
WRITE: / sy-tabix , wa_temp-matnr, wa_temp-werks.
ENDLOOP.
clear wa_temp.
WRITE:/ 'AT FIRST AND LAST'.
LOOP AT it_marc INTO wa_marc.
wa_temp = wa_marc.
AT FIRST.
WRITE:/ 'First Entry'.
WRITE:/ wa_temp-matnr, wa_temp-werks.
ENDAT.
AT LAST.
WRITE:/ 'Last Entry'.
WRITE:/ wa_temp-matnr, wa_temp-werks.
ENDAT.
ENDLOOP.
WRITE:/ 'AT END OF'.
LOOP AT it_marc INTO wa_marc.
wa_temp = wa_marc.
AT END OF matnr.
WRITE: / sy-tabix , wa_temp-matnr, wa_temp-werks.
ENDAT.
ENDLOOP.
WRITE:/ 'AT NEW'.
LOOP AT it_marc INTO wa_marc.
wa_temp = wa_marc.
AT NEW matnr.
WRITE: / sy-tabix , wa_temp-matnr, wa_temp-werks.
ENDAT.
ENDLOOP.
WRITE:/ 'ON CHANGE OF'.
LOOP AT it_marc INTO wa_marc.
wa_temp = wa_marc.
ASSIGN wa_marc-matnr TO <matnr>.
ON CHANGE OF wa_marc-matnr.
WRITE: / sy-tabix ,wa_temp-matnr, wa_temp-werks.
ENDON.
ENDLOOP.
REPORT ZSAMPLE1.
*DATA: itab TYPE TABLE OF zproduct WITH HEADER LINE.
*
*SELECT * FROM zproduct INTO itab.
*ENDSELECT.
**APPEND statement is used to append or add a record from work area to inte
rnal table,
**the new record will be added at the end of the internal table.
*DATA : IT_MARA TYPE TABLE OF MARA,
* WA_MARA TYPE MARA.
*WA_MARA-MATNR = '00001'.
*WA_MARA-MTART = 'FERT'.
*WA_MARA-MEINS = 'EA'.
*APPEND WA_MARA TO IT_MARA . "APPEND WORK AREA TO INTERNAL TABLE
*
*WA_MARA-MATNR = '00002'.
*WA_MARA-MTART = 'ROH'.
*WA_MARA-MEINS = 'MA'.
*APPEND WA_MARA TO IT_MARA .
*
*loop. " this is mentioned to see Internal table data in Debugging
* endloop.
*****************************************************************************
*******************
**INSERT statement is used to insert or add a record from work area into in
ternal
*table at specified location
* DATA : IT_MARA TYPE TABLE OF MARA,
* WA_MARA TYPE MARA.
*
*select * from mara into table it_mara.
*WA_MARA-MATNR = '00001'.
*WA_MARA-MTART = 'FERT'.
*WA_MARA-MEINS = 'EA'.
*INSERT WA_MARA INTO IT_MARA INDEX 2. "The record will be inserted into inte
rnal table at 2nd position
*loop.
* endloop.
*****************************************************************************
********************
**SORT is used to sort a Internal table data in ascending order or descendi
ng order,
*by default it will sort data in ascending order. In addition to this we ca
n able to sort data based on specified fields.
*
*DATA : IT_MARA TYPE TABLE OF MARA,
* WA_MARA TYPE MARA.
*select * from mara into table it_mara.
*SORT IT_MARA . "Default sorts data in ascending order
*SORT IT_MARA DESCENDING . " Sort in descending order
**SORT IT_MARA BY MATNR DESCENDING ."It sorts data by specified fields <FIEL
D1>, <FIELD2>..
*loop.
* endloop.
*****************************************************************************
*********************
**DESCRIBE TABLE is used to count the no of records in a internal table
*DATA : IT_MARA TYPE TABLE OF MARA,
* WA_MARA TYPE MARA.
*DATA : V_LINES TYPE I. "Type integer
*select * from mara into table it_mara.
*DESCRIBE TABLE IT_MARA LINES V_LINES ." Count the no. of record of a inter
nal table
*Write : v_lines .
*****************************************************************************
********************
*READ TABLE WITH KEY .. BINARY SEARCH is used to read a single record from
an
* internal table into work area specified by field name and field value .
*DATA : IT_MARA TYPE TABLE OF MARA,
* WA_MARA TYPE MARA.
*select * from mara into table it_mara.
**READ TABLE IT_MARA INTO WA_MARA WITH KEY MATNR = '000000000000000001' BINA
RY SEARCH. "Read a record into work area where MATNR is '0001'
*READ TABLE IT_MARA INTO WA_MARA INDEX '2'. "Read a record into work area w
here index is 2.
*loop.
* endloop.
*****************************************************************************
*********************
**Loop...Endloop. is also used to read data from a internal table into work
area,
** this is used to read multiple records serially one after one .
* DATA : IT_MARA TYPE TABLE OF MARA,
* WA_MARA TYPE MARA.
*select * from mara into table it_mara.
* LOOP AT IT_MARA INTO WA_MARA.
*ENDLOOP.
*LOOP AT IT_MARA INTO WA_MARA WHERE MATNR = '000000000000000004' .
*ENDLOOP.
* LOOP AT IT_MARA INTO WA_MARA FROM 6 TO 8.
* ENDLOOP.
*****************************************************************************
**********************
**MODIFY is used to modify single or multiple internal table records based o
n
* condition.
**TRANSPORTING is a keyword which is used to specify list particular fields
to be
* modified instead of all fields.
*DATA : IT_MARA TYPE TABLE OF MARA,
* WA_MARA TYPE MARA.
*select * from mara into table it_mara.
* WA_MARA-MATNR = '000000000000000004'.
** loop at it_mara into wa_mara.
* WA_MARA-MTART = 'FERT'.
**MODIFY IT_MARA FROM WA_MARA INDEX 3 TRANSPORTING MATNR MTART.
**MODIFY IT_MARA FROM WA_MARA INDEX sy-tabix TRANSPORTING MTART.
*MODIFY IT_MARA FROM WA_MARA TRANSPORTING MATNR MTART WHERE MATNR = '0000000
00000000006'.
*loop.
* endloop.
*****************************************************************************
************************
**DELETE is used to delete single or multiple records from an internal table
* based on some condition.
*DATA : IT_MARA TYPE TABLE OF MARA.
** WA_MARA TYPE MARA.
*select * from mara into table it_mara.
**DELETE IT_MARA INDEX 3. "3rd RECORD WILL BE DELETED IN IT_MARA
**DELETE IT_MARA WHERE MTART = 'FERT'. "MATERIALS WITH MTART = 'FERT' WILL B
E DELETED
*loop.
* endloop.
*****************************************************************************
*************************
**DELETE ADJACENT DUPLICATES is used to delete duplicate records which are a
djacent to each-other.
**Pre-requisite for this is the internal table must be sorted in ascending o
rder
*DATA : IT_MARA TYPE TABLE OF MARA,
* WA_MARA TYPE MARA.
*select * from mara into table it_mara.
*SORT IT_MARA ASCENDING.
*DELETE ADJACENT DUPLICATES FROM IT_MARA.
*DELETE ADJACENT DUPLICATES IT_MARA COMPARING MATNR, MTART. "DUPLICATES WILL
BE DELETED BY COMPARING MATNR AND MTART
*LOOP.
*ENDLOOP.
*****************************************************************************
*************************
**APPEND LINES OF is used to append multiple records to an internal table f
rom another internal table .
*DATA : IT_MARA TYPE TABLE OF MARA. "FIRST INTERNAL TABLE
*DATA : IT_MARA1 TYPE TABLE OF MARA. "SECOND INTERNAL TABLE
*select * from mara into table it_mara.
*APPEND LINES OF IT_MARA FROM 3 TO 5 TO IT_MARA1 . "DATA IN IT_MARA WILL BE
APPENDED TO IT_MARA1 FROM INDEX 3 TO INDEX 5 .
*LOOP.
*ENDLOOP.
*****************************************************************************
**************************
*DATA : IT_MARA TYPE TABLE OF MARA. "FIRST INTERNAL TABLE
*DATA : IT_MARA1 TYPE TABLE OF MARA. "SECOND INTERNAL TABLE
*select * from mara into table it_mara.
***INSERT LINES OF IT_MARA INTO TABLE IT_MARA1.
*INSERT LINES OF IT_MARA FROM 3 TO 8 INTO TABLE IT_MARA1.
*INSERT LINES OF IT_MARA FROM 1 TO 3 INTO IT_MARA1 INDEX 4.
*LOOP.
*ENDLOOP.
*****************************************************************************
**************************
**MOVE keyword is used to move one internal table data to another.
*DATA : IT_MARA TYPE TABLE OF MARA. "FIRST INTERNAL TABLE
*DATA : IT_MARA1 TYPE TABLE OF MARA. "SECOND INTERNAL TABLE
*select * from mara into table it_mara.
*IT_MARA1 = IT_MARA . "Move IT_MARA to IT_MARA1
*LOOP.
*ENDLOOP.
*****************************************************************************
*************************
** Check statement
*DO 4 TIMES.
*write:/ sy-index.
*check sy-index <= 2.
*write 'JUMBLE'.
*ENDDO.
** Continue statement
*DO 4 TIMES.
*write:/ sy-index.
*if sy-index <= 2.
* CONTINUE.
*endif.
*write 'JUMBLE'.
*ENDDO.
** Exit statement
*DATA: IT_MARA TYPE TABLE OF MARA,
* WA_MARA TYPE MARA.
**PARAMETERS: P_MATNR TYPE MARA-MATNR.
* SELECT SINGLE * FROM MARA INTO WA_MARA WHERE MATNR = '000000000000000006'
. "P_MATNR.
* IF WA_MARA IS INITIAL.
* EXIT. "exit program
* else.
* WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
* endif.
** STOP statement
*DATA: IT_MARA TYPE TABLE OF MARA,
* WA_MARA TYPE MARA.
*
*START-OF-SELECTION.
* SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
* STOP.
*
* LOOP AT IT_MARA INTO WA_MARA.
* WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
*ENDLOOP.
***** output will display to this program
*DATA: IT_MARA TYPE TABLE OF MARA,
* WA_MARA TYPE MARA.
*
*START-OF-SELECTION.
* SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
* STOP.
*
*END-OF-SELECTION.
* LOOP AT IT_MARA INTO WA_MARA.
* WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
*ENDLOOP.