Internal tables in sap

DharmaRaju2 832 views 13 slides Apr 29, 2019
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

Internal Tables in SAP by Dharmaraju BK. An easy to use book written by a programmer for the programmers.


Slide Content

Internal Tables
Internal tables are the temporary tables which are created in SAP system while executing an ABAP
program. Since they are temporary, they will be destroyed once execution of an ABAP program ends.
These tables are used to hold data temporarily either to display on the screen or to manipulate or to
print in a report.
How to create Internal Tables?
Internal tables can be created in any one of the following methods:
Method 1: First by defining a Type (or a structure)
Example:
types : begin of emp_structure,
name(20) type c, "This column is character type and 20 characters width
place(25) type c, "This column is character type and 20 characters width
dob type d, "This column or field is date type
end of emp_structure.
Now use the above type / structure to create a temorary table employees. Code for the same is given below:

data: employees type emp_structure.
You can create any number of temporary tables (having the structure similar to type
emp_structure) by referring to the about type or strcuture. Two examples are given
below:

Data : sw_employees type emp_structure.
Data : hw_employees type emp_structure.

The above create two internal tables nam ely sw_employees and hw_employee s whose
structure is same as that of emp_structure.

Method 2: Straightaway using data type as below
data: begin of employees1,
name(20) type c,
place(25) type c,
dob type d,
end of employees1.
Method 3: By referring to an existing SAP table as below:
data : employees3 type table of mara.
Where mara is one of the tables in SAP system.
Adding data into Internal Tables:
(Internal tables created in method 1 has not been considered in the remaining part of this document as
method 2 and 3 seems to be better ones)
Now, let us see how to add data into internal tables. You need to slighly modify (ie. you need to add
occurs 0) in the codes used for creating internal tables as shown below:
data: begin of employees1 occurs 0,
name(20) type c,
place(25) type c,
dob type d,
end of employees1.

Occurs allocate a temporary work area (also known as header line) which can be used to add/change
the data into / from a table.
Now, let us add details of employees name, place, dob into employees1 table.
employees1-name = 'Dharmaraju BK'.
employees1-place = 'Bangalore'.
employees1-dob = '19650520'
(Notice that the dob entered in the format yyyymmdd format)
Now the above data is available in gthe work area of the internal table. To move data from this work
area into the internal table, use the following command.
append employees1.
Let us add few more records to employees1 internal table.
employees1-name = 'Sanjana'.
employees1-place = 'Mysore'.
employees1-dob = '20020615'.
Append employees1.

employees1-name = 'Samarth D'.
employees1-place = 'Mysore'.
employees1-dob = '20040404'.
Append employees1.

employees1-name = 'Kiran'.
employees1-place = 'Bangalore'.
employees1-dob = '19901024'.
Append employees1.

Displaying data added to the internal table.
Now, we have added 4 rows / records to our internal table employees1. Now, to display the
contents of this internal table, add the follwing lines at the end of the program.
loop at employees1.
write : / employees1-name, employees1-place, employees1-dob.
endloop.
The output:

To display dates in dd.mm.yyyy format, change the Write :/ as below:

write : / employees1-name, employees1-place, employees1-dob dd/mm/yyyy.

Notice how the dates are displayed in the following screen:



Complete program is listed below:

REPORT ZCHECK4_PROGRAM20.

data: begin of employees1 occurs 0,
name(20) type c,
place(25) type c,
dob type d,
end of employees1.

employees1-name = 'Dharmaraju BK'.
employees1-place = 'Bangalore'.
employees1-dob = '19650520'.
append employees1.

employees1-name = 'Sanjana'.
employees1-place = 'Mysore'.
employees1-dob = '20020615'.
Append employees1.

employees1-name = 'Samarth D'.
employees1-place = 'Mysore'.
employees1-dob = '20040404'.
Append employees1.

employees1-name = 'Kiran'.
employees1-place = 'Bangalore'.
employees1-dob = '19901024'.
Append employees1.


* loop at employees1.
* ' write : / employees1-name, employees1-place, employees1-dob.

* 'endloop.

loop at employees1.
write : / employees1-name, employees1-place, employees1-dob dd/mm/yyyy.
endloop.

Conditional Display
Suppose, if you want to display names of employees from mysore, then you can change the code as follows

loop at employees1 where place = 'Mysore'.
write : / employees1-name, employees1-place, employees1-dob dd/mm/yyyy.
endloop.



similarly, to display list of employees who are born after the year 2000, you can modify your code as follows:

loop at employees1 where dob >= '20000101'.
write : / employees1-name, employees1-place, employees1-dob dd/mm/yyyy.
endloop.
(Notice that the date is entered in yyyymmdd format above).

Output:



Now, to display header for each column, insert the following line in your code just before loop at …. Line.

write : / 'Name', 'Place', 'Date of Birth'.

loop at employees1 where dob >= '20000101'.
write : / employees1-name, employees1-place, employees1-dob dd/mm/yyyy.
endloop.

Output:

You can align the column headers along with the colum data. Modifications is required as below:

write : / 'Name' , 20 'Place', 50 'Date of Birth' .

loop at employees1 where dob >= '20000101'.
write : / employees1-name, 20 employees1-place, 50 employees1-dob dd/mm/yyyy.
endloop.

Output:


You can also highlight column labels with colours by using colour codes as below:
write : / 'Name' color 3, 20 'Place' color 3, 50 'Date of Birth' color 3.

loop at employees1 where dob >= '20000101'.
write : / employees1-name, 20 employees1-place, 50 employees1-dob dd/mm/yyyy.
endloop.

Output:











Sorting the Internal Table data
You can sort (also known as index) data of an internal table by using sort command.
- To sort the above internal table by employee name us the command:
sort employees1 by name.
output:


By default the sorting will be in descending order. To sort in
descending order use the condition descending as shown below.
sort employees1 by name descending.
Output:



Colour Codes for Write Statement
1 Grayish Blue
2 Bright Gray
3 Yellow
4 Bluish Green
5 Green
6 Red
7 Violet

Similarly try with other fields/columns like age, place too.
You can also sort on multiple fields / columns. Source code for the same is given below:

sort employees1 by place name.
Output:


Creating Internal Tables using SAP’s Tables.
As you aware that the SAP system consists of thousands of database tables in its database
repository. You can any of these tables to create internal tables as explained in this section.
Souce code is given below:
REPORT ZCHECK4_PROGRAM21.
data : mara_it type table of mara with header line.

select * from mara into corresponding fields of table mara_it.

loop at mara_it.
write : / mara_it-matnr.
endloop.
Notice that the mara is a SAP table (one of the material master tables). An internal table mara_it has
been created whose structure is same has mara. This internal table has a header line. It means that
the work area is created automatically along with the internal table.
The above code copy the content of the entire MARA
table and displays the material Nos (as we have used only
MATNR field in the write command). You can also
include where clause in the select command to filter the
records copied from mara table as shown below.


select * from mara into corresponding fields of table mara_it where matnr like
‘A%’.
The above Select command fetches rows having matnr starting with A are copied into
mara_it table.
Header Line

Creating Internal Tables using SAP’s two or more Tables (Inner Join)
Suppose, you want to copyt data from two tables say
matnr (ie, material No), MTART (material type) from
MARA table and short description (MAKTX) from makt
table. Then, our ABAP code will be looking like the
following:



REPORT ZCHECK4_PROGRAM22.

data : begin of material occurs 0,
matnr like mara-matnr,
mtart like mara-mtart,
maktx like makt-maktx,
end of material.

select a~matnr a~mtart b~maktx into corresponding fields of material from mara as a
inner join makt as b on a~matnr = b~matnr.
append material.
endselect.

loop at material.
write : / material-matnr, material-mtart, material-maktx.
endloop.

Output (as per my SAP tables):

In the above display, the first column is the material number, second column
displays material type (HERS – Manufacturer part Numbers) and the third colum is
the short description of the materials.

Now, let us see how to extract data from three tables into an internal table.
In the above examples, we have tried how to extract data from one or two SAP tables into inner
tables. Now, let us see how to extract data from 3 or more tables.
Source code goes here ….
Joins in Tables
Joins are used to combine two or
more tables. An inner join finds all
the matching rows from two or more
tables.

REPORT ZCHECK4_PROGRAM24.
tables : eord, lfa1, makt.

data : begin of material occurs 0,
matnr like eord-matnr,
lifnr like eord-lifnr,
name1 like lfa1-name1,
maktx like makt-maktx,
end of material.

select a~matnr a~lifnr b~name1 c~maktx into corresponding fields of mate
rial from eord as a
inner join lfa1 as b on a~lifnr = b~lifnr
inner join makt as c on a~matnr = c~matnr
where a~matnr like 'CN%'.

append material.
endselect.


loop at material.
write : / material-matnr, material-maktx, material-lifnr, material-
name1.
endloop.


In the above example, material Number and vendor code details have been
extracted from EORD table. Vendor names for the correspodning vendor codes
have been extracted from LFA1 table and Material Short Description (MAKTX
field) has been extreacted from MAKT table.

Typical output from my database tables is re-produced below:

The above display can be sorted by sorting the internal table. Include the following command just
before loop at material line.
Sort material.

Since no field name / column name is included on which index is required, the system sorts internal
table on the first field (in this case, it is matnr). By default the index will be in ASCENDING order. If
you want in DESCENDING order, then add descending clause to the above command as shown
below:
Sort material descending.
In case, if you want to index other than the first column of the internal table, then include the
column name(s) in the sort command as shown below:
Sort material by lifnr. “ To index on LIFNR (Vendor Code)
Or
Sort material by name1. “ To index on name1 (Vendor name)
Or
Sort material by maktx. “ To index on maktx (Short Description).

Use descending clause after the field /column name, if you need to index
them on descending order.
Compound indexes (ie. indexing on multiple columns) can be created by
including two or more columns in the sort.
Sort material matnr name1. “Internal table is sorted on matnr and name1
columns.
Try yourself by including three or more columns in the sort command.
Complete source code with sort command is reproduced below:

REPORT ZCHECK4_PROGRAM24.
tables : eord, lfa1, makt.

data : begin of material occurs 0,
lifnr like eord-lifnr,
matnr like eord-matnr,
name1 like lfa1-name1,
maktx like makt-maktx,
end of material.

select a~matnr a~lifnr b~name1 c~maktx into corresponding fields of material from eord as a
inner join lfa1 as b on a~lifnr = b~lifnr
inner join makt as c on a~matnr = c~matnr
where a~matnr like 'CN%'.

append material.
endselect.


sort material by matnr name1. " Sorting on matnr and name1 columns.


loop at material.
write : / material-matnr, material-maktx, material-lifnr, material-name1.
endloop.
Adding Field / Column Names to the display list
Now, let us add Field names to the
dsiplay at the top of the page. Add the
following commands within loop at … and
End loop lines.



loop at material.
at First.
write : 'Material No.',
'Description',
'Vendor No',
'Vendor Name'.
endat.


write : / material-matnr, material-maktx, material-lifnr, material-
name1.
endloop.
Output:

At First and At Last
At First is used to perform processing
during the first loop pass and At Last
used to perform last loop pass within
loop …. Endloop.

Notice that there is no proper alignmnet between the field names and the
field values. To achive alignment, change the codes as shown below:
loop at material.
at First.
write : 'Material No.',
20 'Description',
60 'Vendor No',
80 'Vendor Name'.
endat.

write : / material-matnr,
20 material-maktx,
60 material-lifnr,
80 material-name1.

endloop.


Add uline command just before endat to print a line immediately after field
names/labels.
loop at material.
at First.
write : 'Material No.',
20 'Description',
60 'Vendor No',
80 'Vendor Name'.
uline.
endat.

Counting No. of lines in an Internal Table.
To count No. of rows in a SAP table, you can straightaway use the following command.
For example, to count No. of rows from EORD table, use the following lines of code.
data rowscount type i.
select count(*) into rowscount from eord.
write rowscount.

However, in case of internal tables, use the following lines of codes.
data: rowscount type i.
Describe table material lines rowscount.
write rowscount.

Addition of Additional Details to Internal Tables

I have added two more fields/columns into my internal table. The data to
these fields can be updated using loop at ….. Complete source code for the
same is re-produced below:
REPORT ZCHECK4_PROGRAM24.
tables : eord, lfa1, makt, mara.

data : begin of material occurs 0,
matnr like eord-matnr,
lifnr like eord-lifnr,
name1 like lfa1-name1,
maktx like makt-maktx,
matkl like mara-matkl,
WGBEZ like T023T-WGBEZ,
end of material.

select a~matnr a~lifnr b~name1 c~maktx into corresponding fields of material
from eord as a
inner join lfa1 as b on a~lifnr = b~lifnr
inner join makt as c on a~matnr = c~matnr
where a~matnr like 'ED%'.

append material.

endselect.

loop at material.
select single matkl from mara into (material-matkl) where matnr = material-
matnr.
modify material transporting matkl.
endloop.

loop at material.
select single wgbez from T023T into (material-wgbez) where matkl = material-
matkl.
modify material transporting wgbez.
endloop.
loop at material.
write : / material-matnr, material-maktx, material-lifnr, material-
name1, material-matkl, material-wgbez.
endloop.

One more example is given below to show th eusage of loop at …. Modify ….
REPORT ZCHECK4_PROGRAM25.

Data : begin of emails occurs 0,
lifnr type lfa1-lifnr,
adrnr type lfa1-adrnr,
SMTP_ADDR type adr6-SMTP_ADDR,

end of emails.

select lifnr adrnr into corresponding fields of emails from lfa1. "where lifnr lik
e 'L%'.
append emails.

endselect.

loop at emails.
select single SMTP_ADDR from adr6 into (emails-
smtp_addr) where ADDRNUMBER = emails-adrnr.
modify emails transporting smtp_addr.
endloop.



if emails is initial.
write :/ 'No Data'.
else.

loop at emails where smtp_addr is NOT initial. " Display only those records ha
ving some email ID.
write :/ emails-lifnr, emails-adrnr, emails-SMTP_ADDR.
endloop.

endif.

In the above case, I used only two tables. Same results can be obtained by
using inner join too. Try it yourself.


Date : 29
th
Apr 2019, Bangalore, India.
=== 0 ===