Вы находитесь на странице: 1из 34

Internal Tables

kvn.sunny@gmail.com

Internal tables
By definition, Internal tables and Field strings
are user defined structured data types.

Internal tables contd.


These internal tables can be created as a
replica of data base table, using some or all
fields of one table or more tables.
These internal tables are created only during
run time. No memory is reserved.

Why we need inttables?


Long Life data is stored in database tables.
When we are directly modifying data, there is
every possibility that we may loose data by
accident, which we can not afford to do so.
As such we need some intermediate tables to
do some operations. Up on satisfactory
results, we can modify database table.

Internal tables
Basically there are 2 types of Internal tables.
Internal tables with header line.
Internal tables without header line.

Internal tables with header line.


When you create internal table with header
line, a default work area is created.

Internal tables w/o header line.


In this case, we need to create explicit work
area to work with it.
When we need to nest the tables with in
tables, we need to use this type of internal
table.

Creation of internal tables


There are 3 steps involved in creating Internal
tables.
Declaration of Internal table.
Populating Internal table.
Processing Internal table.

Declaration of internal table


1) data : itab like <table name> occurs 0 with
header line.
Itab : name of internal table.
<table name> : name of data base table.
Occurs 0 : performance factor.
By default internal table created is without
header line.

Declaration contd.
2) data : begin of itab occurs 0.
include structure <table name>.
data : end of itab.

Declaration contd.
3) data : begin of itab occurs 0,
carrid like sflight-carrid,
connid like sflight-connid,
fldate like sflight-fldate,
end of itab.

Declaration contd.
4) data : begin of itab occurs 0,
carrdid like sflight-carrid,
connid like sflight-connid,
bookid like sbook-bookid,
fldate like sbook-fldate,
end of itab.
( In this case we are using fields from more than one
table)

Declaration contd.
5) data : begin of itab occurs 0,
carrid1 like sflight-carrid,
connid1 like sflight-connid,
fldate1 like sflight-fldate,
end of itab.

Populating internal table.


1) itab-name = abcd.
Append itab.

Populating contd.
2) do 5 times.
itab-number = sy-index.
append itab.
clear itab.
Enddo.

Populating contd.

3) Select * from sflight into table itab


<where clause>.

Populating contd.

4) select * from sflight into itab <where.>.


append itab.
clear itab.
endselect.

Populating contd.
5) select * from sflight <where..>.
move sflight to itab.
append itab.
clear itab.
endselect.

Populating contd.
6) select * from sflight <where.>.
move-corresponding sflight to itab.
append itab.
clear itab.
endselect.

Populating contd.
7) select carrid connid fldate from sflight
into (itab-carrid, itab-connid, itab-fldate)
<where.>.
append itab.
clear itab.
endselect.

Populating contd.
8) select * from sflight.
select * from sbook where carrid =
sflight-carrid.
move-corresponding sflight to itab.
move-corresponding sbook to itab.
append itab.
clear: itab-bookid,itab-fldate,sbook.endselect.
clear: itab-carrid,itab-connid,sflight.endselect.

Processing internal table


Processing of internal table includes:
Write
Read
Insert
Modify
Delete

Write
loop at itab < where carrid = LH >.
Write:/ itab-carrid,itab-connid,itab-fldate.
endloop.

Read
Read table itab index <index>.
OR
Read table itab with key carrid = LH connid
= 0400.

Insert
itab-carrid = MN.
itab-connid = 1111.
insert itab index 3.

Modify
itab-carrid = NM.
itab-connid = 2222.
modify itab index 3.

Delete
delete itab index 3.
OR
delete from itab where carrid = LH.

Initializing internal table

clear itab.
clear itab [ ].
refresh itab.
free itab.

: clears header of int table.


: clears body.
: remove contents.
: de allocates memory
associated with int table.

Sorting of internal table


sort itab.
This command will sort internal table on all
non-numeric primary keys in ascending
order.
sort itab by carrid.
sort itab by carrid descending.

Collect itab

If non numeric fields are same, then numeric


fields will be added by using this command.

Collect itab contd.


Itab-name1 = AB. itab-name2=CD.
Itab-sal = 20. Collect itab.
Itab-name1 = AB. Itab-name2 = DE.
Itab-sal = 25. Collect itab.
Itab-name1 = AB. itab-name2 = CD.
Itab-sal = 30. Collect itab.
Loop at itab.write:/ itab.endloop.

Append itab sorted by <f>


Data : begin of itab occurs 5,
num like sy-index,
end of itab.
Do 10 times.
Itab-num = sy-index.
Append itab sorted by num.
Enddo.

Attributes of int tab.


Describe table itab lines v_lines.
If itab is initial.
<.>
endif.

Control break statements


Sorting of internal table is a prerequisite.
1) at first. 3) at end of <f>.
<> <>
endat.
endat.
2) at new <f>. 4) at last.
< > <>
endat.
endat.

Вам также может понравиться