Internal Tables Example
Internal Tables Example
Internal Tables Example
END OF TY_T001.
SELECT BUKRS,
BUTXT,
ORT01,
LAND1
FROM T001
*SORT
WA_T001-BUKRS,
WA_T001-BUTXT,
WA_T001-ORT01,
WA_T001-LAND1.
ENDLOOP.
WA_T001-BURKS = 'F001'.
WA_T001-ORT01 = 'INDIA'
*INSERT
WA_T001-BURKS = 'S001'.
WA_T001-ORT01 = 'INDIA'
SKIP 2.
ULINE.
WRITE : / SY-TABIX,
WA_T001-BURKS,
WA_T001-BUTXT,
WA_T001-ORT01,
WA_T001-LAND1.
ENDLOOP.
*DELETE
SKIP 2.
ULINE.
WRITE : / SY-TABIX,
WA_T001-BURKS,
WA_T001-BUTXT,
WA_T001-ORT01,
WA_T001-LAND1.
ENDLOOP.
*MODIFY
*Set country key to 'IN' for both company codes 'F001' and 'S001'.
CLEAR WA_T001.
WA_T001-LAND1 = 'IN'.
IF SY-SUBRC = 0.
SKIP 2.
WRITE : / SY-TABIX,
WA_T001-BURKS,
WA_T001-BUTXT,
WA_T001-ORT01,
WA_T001-LAND1.
ENDLOOP.
ENDIF.
*READ
CLEAR WA_T001.
BINARY SEARCH.
SKIP 2.
ULINE.
WRITE : / SY-TABIX,
WA_T001-BURKS,
WA_T001-BUTXT,
WA_T001-ORT01,
WA_T001-LAND1.
ENDDO.
SKIP 2.
ULINE.
WRITE : / SY-TABIX,
WA_T001-BURKS,
WA_T001-BUTXT,
WA_T001-ORT01,
WA_T001-LAND1.
ENDLOOP.
SKIP 2.
ULINE.
WRITE : / SY-TABIX,
WA_T001-BUKRS,
WA_T001-BUTXT,
WA_T001-ORT01,
WA_T001-LAND1.
ENDLOOP.
Categories: SAP
1. Declare the structure of Internal Table i.e. A User Defined Data type with the required
fields, According tp the fileds required from the Corresponding database tables(s).
END OF <TY>.
EXAMPLE: Declare an ITAB to maintain data from table T001 (Company Code Data)
NOTE: While Declaring <ITAB>, refer the corresponding database table i.e. T001 here.
Syntax to select the Data from Database table into Internal table:
SELECT <F1>,
<F2>,
<F3>,
……..
FROM <DBT>
NOTE: Make sure that the structure (order) of the fields in the SELECT and Internal table should
be same, because the content if 1st filed in SELECT is transferred to the 1st filed of <ITAB> and
similarly 2nd , 3rd etc.
END OF TY_T001.
SELECT BUKRS,
BUTXT,
ORT01,
LAND1
FROM T001
NOTE: After the SELECT data from database table is transferred to Internal table.
*Since data in Internal table is Sorted record by record, Accessing/Displaying Data from Internal
table is also record by recoord. So that there is a special loop which starts with the 1st record and
ends with the second record of <ITAB> by default..
*Keep the Internal table in a LOOP, LOOP points to the first record by default and collect it into
<WA> and process (Print) the data from <WA>.
REPORT Z9AMPROGRAM52.
tables vbak.
select-options : so_vbeln for vbak-vbeln.
Categories: SAP
This overview describing applications programming in the R/3 System. All applications
programs, along with parts of the R/3 Basis system, are written in the ABAP Workbench
using ABAP, SAP’s programming Language. The individual components of application
programs are stored in a special section of the database called the R/3 Repository. The R/3
Repository serves as a central store for all of the development objects in the R/3 System. The
following sections of this documentation cover the basics and characteristics of application
programming.
A - Advanced
B - Business
A - Application(s)
P - Programming
Program :
An Instruction is Group of
Keywords + Variables + Operators + Data Types
Keywords :
Syntax : Each ABAP statement Should begin with a keyword and ends with a period.
Since Each Statement Should Start with a Keyword. It is Difficult to give the exact no. of
Keywords So that Keywords are Divided into Different Types Depends On the Functionality Of
the Keywords.
Types Of Keywords :
Syntax for Variables : DATA < Var. Name> TYPE <Data Types>.
Control Keywords :
Statements are used to control the flow of an ABAP program within a processing block
according to certain conditions.
Ex :
Ex :
FORM - ENDFORM
FUINCTION - ENDFUNCTION
MODULE - ENDMODULE
Calling Keywords :
Are used to call Re-usable Modules (Blocks) that are already defined.
To Process the data that you have defined using declarative statements.
Event Keywords :
END-OF-PAGE To Print the Same FOOTER for every Page on the Output List.
The physical units with which ABAP statements work at routine are called internal program data
objects. The contents of a data occupy memory space in the program. ABAP statements access
these contents by addressing the name of the data object. Each ABAP data objects has a set of
technical attributes which are fully defined at all times when an ABAP program is running. The
technical attributes of a data object are : Data Type, Field Length and Number of Decimal
Places.
Date (D)
Time (T)
Numeric Types :
Integer (I)
The Field Length for data types D, F, I and T is fixed. The field length determines the number of
bytes that the data object occupies in memory. In types C, N, X and P, the length is not part of
the type definition. Instead, you define it when you declare the data object in your
program.
Data Type P is particularly useful for exact calculations in a business context. When you define
an object with type P, you also specify a number of decimal places.
You can also define your own elementary data types in ABAP using the TYPES statements. You
base these on the predefined data types. This determines all of the technical attributes of the new
data type. For example, you could define a data type P_2 with two decimal places on the
predefined data type P. You could then use the new type in your data declarations.
Predefine Elementary ABAP Types : All field lengths are specified in bytes.
Data Types Initial Field Length Valid Field Length Initial Value Meaning
Numerica Types
16
Character (Non-Numeric) Types
C 1 1 ‘………..’ Text Field
5
D 8 8 ’00000000′ Date Field
(Format : YYYYMMDD)
N 1 1 ’0 … 0′ Numeric Text Fields
5
T 6 6 ’000000′ Time Field
(Format : HHMMSS)
Note : Data Type N is not a numeric type. Type N objects can only contain number characters
(0…….9) but are not represented internally as numbers. Typical type N fields are account
numbers and zip codes.
Integers – Type 1
The value range of type 1 numbers is -2**31 to 2**31-1 and includes only whole numbers. Non-
integer results of arithmetic operations (e.g. fractions) are rounded, not truncated.
You can use type 1 data for counters, number of items, indexes, time periods, and so on.
Type P data allows digits after the decimal point. The number of decimal places is generic,
and is determined in the program. The value range of type P data depends on its size and
number of digits after the decimal point. The valid size can by any value from 1 to 16 bytes.
Two decimal digits are packed into one byte, while the last byte contains one digit and the sign.
When working with type P data, it is a good idea to set the program attributes Fixed point
arithmetic. Otherwise, type P numbers are treated as integers.
Note : You can use type P data for such values as distances, weights, amounts of money, and so
on.
The value ranges of type F numbers is 1×10**-307 to 1×10**308 for positive and negative
numbers, including 0 (zero). The accuracy range is approximately 15 decimals, depending on the
floating point arithmetic of the hardware platform. Since type F data is internally converted to a
binary system, rounding errors can occur. Although the ABAP processor tries to minimize these
effects, you should not use type F data if high accuracy is required. Instead, use type P data.
You use type F fields when you need to cope with very large ranges and rounding errors are not
critical.
Operations using I and F fields are very similar to the actual machine code operations, while P
fields requires more support from the software. Nevertheless, you have to use type P data to meet
accuracy or value range requirements.
Of the five non-numeric types, the four types C,D, N and T are characters types. Fields with
these types are known as character field. Each position in one of these fields talks up enough
space for the code of the character. Currently, ABAP only works with single-byte codes such as
ASCII and EBCDI. However, an adaptation to UNICODE is in preparation. Under UNICODE,
each character occupies two or four bytes.
Categories: SAP
Standard Table
These are default table. The key of a standard table is always non-unique, so duplicates
are allowed.
DATA <ITAB> TYPE STANDARD TABLE OF <TY>.
Record can be accessed through both INDEX and Condition.
READ TABLE <ITAB> INTO <WA> INDEX <N>.
Or
Sorted Table
This is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted
tables using INSERT statement. Entries are inserted according to the sort sequence defined
through the table key.
The response time for key access is logarithmically proportional to the number of table entries,
since the system always uses a binary search.
Note: records can be accessed through both INDEX and with KEY(Condition).
Sorted internal tables are always either UNIQUE/ NON UNIQUE i.e. sorted internal tables
cannot be declared without UNIQUE/NON-UNIQUE keyword.
Hashed Table
This is the most appropriate type for any table where the main operation is key access.
The response (Search) time doesn’t depend on the number of records, instead it always remain
constant regardless the number of table entries.
Hashed tables are useful if you want to construct and use an internal table which resembles a
database table or for processing large amounts of data.
Sorted tables, hashed tables are only introduced in Release 4.0 standard tables already existed
several release previously.
Defining a line type, table type and tables without a header line have only been possible since
Release 3.0. For this reason, there are certain features of standard tables that still exist for
compatibility reasons.
Before Release 3.0, internal tables all had header lines and a flat-structured line type. There were
no independent table types. You could only create a table object using the OCCURS addition in
the DATA statement, followed by a declaration of a flat structure:
<F1>
<F2>
……..
<FN>
END OF <ITAB>.
This statement declared an internal table <ITAB> with the line type defined following the
OCCURS addition. Furthermore, all internal tables had header lines.
The number <N> in the OCCURS addition had the same meaning as in the INITIAL SIZE
addition from Release 4.0. Entering ‘0’ had the same effect as omitting the INTIAL SIZE
addition. In this case, the initial size of the table is determined by the system.
Since Release 3.0, it has been possible to create table types using
TYPES <T> TYPE|LIKE <linetype> OCCURS <n>.
The above statement is still possible in Release 4.0, and has the same function as the following
statement:
NOTE: OCCURS <0> allocates the initial memory 8kb and the system keep on allocated by
8kb, whenever it is required.
OCCURS <N> allocates memory for <N> records initially and keep on allocates for <N>
records, whenever it requires.
Categories: SAP
FROM <N1> TO <N2> is Optional, If We Ignore it, All the Records are Transferred, Else Only
the records from <N1> TO <N2> are transferred.
Finding No Of Records
NOTE: The default key is made up of the Non-Numeric fields of the table line in the order in
which they occur.
OR
READ TABLE <ITAB> INTO <WA> WITH KEY <Condition> BINARY SEARCH.
NOTE: Make sure that the Internal Tabel is Sorted to Apply BINARY SEARCH.
ENDLOOP.
OR
ENDLOOP.
DELETING Records:
Single Record: DELETE <ITAB> INDEX <N>.
NOTE: Make Sure that the Duplicates should be Adjacent, which can be done through
SORTING.
COMPARING <F1><F2>……..
NOTE: Make sure that the <ITAB> is Sorted by all the comparing fields in the same order…
MODIFY:
WHERE <Condition>.
Categories: SAP
Now that we have our table created in SAP we want to fetch records from the table and display it
on the LPS (List Processing Screen).This is very simple.
There are no connections to be made with the database, as the table that was created is treated as
a object by SAP. Let’s suppose the name of the table is zemployee – it has three fields as
This structure has two fields X & Y for type character which will hold data that we get from
zemployee table.
Please note that variable X & Y can hold just one value at a time. As only one memory location
is allocated for both variables in the structure.
REPORT ZABC.
Here we are accepting value p_z from user which will be employee number.
Also note that we are using SELECT SINGLE in SQL query. This type SQL query is native to
SAP.
Select single will get only one record from the table and place it into the structure variables X &
Y.
In above code we are using data types which are not generic. To use more generic value for data
types, we can directly access data types of the fields in the table, here how it is done
REPORT ZXYZ.
Categories: SAP
Do Loop
The statements DO and ENDDO define a control structure, which can contain a closed statement
block statement_block .
Without the addition n TIMES ,the statement block will be repeated until one of the statements
termination of loops terminates the loop. Especially the statement EXIT is intended to
completely terminate loops.
With the addition n TIMES you can limit the amount of loop passes. n expects a data object of
the data type i. The numerical value of n at entry in the loop determines the maximum amount of
passes of the statement block. The control structure does not accept a change of the value n
within the loop. If n contains a value less than or equal to 0, the statement block will not be
executed.
Example
REPORT Z9AMPROGRAM15.
do 10 times.
z = x * y.
write :/ x,'*',y,'=',z.
y = y + 1.
enddo.<strong></strong>
Categories: SAP
Case Statement
These statements define a control structure that can contain multiple statement blocks
statement_block1, …, statement_blockn, of which no more than one is executed depending of
the value in the operand operand.
Starting with the first WHEN statement, the content of the operand in operand is compared with
the content of one of the operands operand1, operand2, … from the top down. The statement
block is executed after the first identical instance is found. If no matches are found, the statement
block is executed after the statement WHEN OTHERS.
If the end of the executed statement block is reached or no statement block is executed,
processing continues after ENDCASE.
For the comparison of the contents, the following logical expression can be used:
Notes
● You cannot use a statement between the statement CASE and the first statement WHEN.
It is suggested to use case-endcase over if-endif, because performance wise case end-case
performs better than if-endif
REPORT ZABC.
parameters : x type i,
y type i,
ch type i.
data z type i.
case ch.
when 1.
z = x + y.
write :/ 'Sum is ',z.
when 2.
z = x - y.
write :/ 'Difference is ',z.
when 3.
z = x * y.
write :/ 'Product is ',z.
when 4.
z = x / y.
write :/ 'Division is ',z.
when others.
write :/ 'Invalid choice, please enter 1,2,3,4'.
endcase.<strong></strong>
Categories: SAP
IF Statement
These statements define a control structure which can contain several statement_block
statement blocks; depending on the logical expressions, maximally one of the blocks is executed.
After IF and ELSEIF, any number of logical expressions can be specified, while the expressions
statement_block represent any kind of statement blocks.
The logical expressions are checked from top to bottom, starting with the IF statement, and the
statement block after the first true logical expression is executed. If none of the logical
expressions is true, then the statement block after the ELSE statement is executed.
If the end of the executed statement block is reached or if no statement block was executed, then
the processing is resumed after ENDIF.
REPORT ZABC.
data z type i.
if r1 = 'X'.
z = x + y.
elseif r2 = 'X'.
z = x - y.
if z >= 0.
else.
left-justified.
endif.
elseif r3 = 'X'.
z = x * y.
elseif r4 = 'X'.
z = x / y.
endif.<strong></strong>
Categories: SAP
Parameter
REPORT Z9AMPROGRAM6.
parameters : x type,
y type.
data z type i.
z = x + y.
write z.
If you want to provide a default value to the parameter then we use the keyword default.
Example
Categories: SAP
By default the character field values on selection screen are stored in upper-case. To control the
case sensitivity of value that is being entered we can use lower case keyword as a part of
parameters statement.
REPORT ZABC.
write :/ x.
While handling string, ABAP has given predefined string functions which can be directly used
with string variable. These functions are :
strlen
translate
concatenate
replace
split
Example :
REPORT ZABC.
* STRLEN ***
parameters x type string.
data k type i.
k = strlen( x ).
write :/ 'length of string is ',k.
***********************************
* Translate **
*parameters x type string lower case.
write :/ x.
translate x to lower case.
write :/ x.
translate x to upper case.
write :/ x.
* Concatenate
data : s1 type string value 'ABC',
s2 type string value 'Systems',
s3 type string.
write :/ s3.
concatenate s1 s2 into s3.
write :/ s3.
concatenate s1 s2 into s3 separated by ' '.
write :/ s3.
s1 = 'abc'.
s2 = 'pqr'.
concatenate s1 s2 into s2.
write :/ s2.
***********************************
** Replace
*data : s1 type string value
* 'ABC Software Systems'.
write :/ s1.
replace 's' with 'k' into s1.
write :/ ' after replacing s with k : ' , s1.
s1 = 'ABC Software Systems'.
replace all occurrences of 's' in s1 with 'k'.
write :/ s1.
s1 = 'ABC Software Systems'.
replace all occurrences of 's' in s1 with 'k'
ignoring case.
write :/ s1.
***********************************
** SPLIT
*data : s1 type string value
* 'ABC Software Systems',
* s2 type string
* s3 type string,
* s4 type string.
*write :/ s2,s3,s4.
split s1 at ' ' into s2 s3.
write :/ s2,
/ s3.
*split s1 at ' ' into s2 s3 s4.</em>
*write :/ s2,
* / s3,
* / s4.
* OFFSET
*data : s1 type string
* value 'ABC Software Systems',
* s2 type string.
*s2 = s1+5(10).
*write :/ s2.
Categories: SAP
By default integers are right justified. And characters are left justified.
To Adjust the output for your screen, you can use left-justified
Here is an example :
REPORT Z9AMPROGRAM3.
data : x type i,
y type i,
z type i.
x = 10.
y = 20.
z = x + y.
write z.
write :/ ‘sum of two numbers is ’,z.
write :/ ‘sum of two numbers is ’,z left-justified.
CHAIN OPERATOR ‘ : ’
Chain operator is used when we are declaring multiple variables by using single data statement
or when we are displaying multiple variable values by using single write statement.
Go to SE11 and enter the Z table name for which the table maintenance generator has to be generated.
Click on Change.
Click on Utilities Table Maintenance Generator
Enter Authorization group (For e.g. &NC&)
Function group (go to SE80 for creating function group)
You can select either one step or two step. Assign Screen numbers (To assign Screen numbers click on
the button 'Find Scr no'. It will propose screen no's)
Single step: Only overview screen is created i.e. the Table Maintenance Program will have only one
screen where you can add, delete or edit records.
Two step: Two screens namely the overview screen and Single screen are created. The user can see the
key fields in the first screen and can further go on to edit further details.
You can now create TCode for the table maintenance generator. Enter the TCode to be created and click
on Create.
Select “Transaction with parameters (Parameter transaction)”
Click on save.
Now you can create the table entries through the transaction created above.
Introduction
Database views are used to combine application data often distributed over several tables. The structure
of such views is defined by specifying the tables and fields that are required. Fields which are not
required can be hidden, thereby minimizing the interfaces. A view can be used in ABAP programs for
data selection.
Following demo shows how to create database view for two database tables having foreign key
relationship.
We will be creating a database view for below shown tables YZ14_BANK & YZ14_ACCOUNTS. Note that
YZ14_ACCOUNTS is foreign key table (dependent table) and YZ14_BANK is check table (referenced
table) for field BRANCH_ID. It should be kept in the mind that we can only include transparent tables in
database view.
Steps
1. Go to transaction SE11 -> select radio button 'View' -> enter the name of the view -> press 'Create'.
2. Below pop up screen will be displayed. Select 'Database View' and press button.
5. Next, we need to link the entered tables by specifying the fields in join condition. We can also derive
the join conditions from existing foreign keys between the base tables of the view. To do this, position
the cursor on the table names and click on present at the bottom.
6. Below pop up comes wherein linked tables are present. Select the tables and press 'Copy'.
7. On clicking copy button, join condition will be derived from the base tables. In our case, we have the
below shown conditions.
8. In the 'View Flds' tab, we need to enter all the fields, we want in our view from the database tables.
We can either enter the fields directly or we can copy them from base tables. Later can be achieved
by pressing button present in the tab 'View Flds'. We can also include complete
table in a view by entering * in 'View field' & table name in 'Table'. If fields are inserted or deleted from
this table, similar modification will be automatically made in view structure.
10. We repeat the above step to include below shown fields from table YZ14_ACCOUNTS.
11. In above steps do not include 'MANDT' & 'BRANCH_ID' twice. While activating the view, it will
lead to an error.
12. As shown, we have the below fields in our view.
13. We can also formulate the selection criteria using 'Selection Conditions' tab likewise coded below.
Here we have entered the condition for account type and status. All the data in the base table
satisfying the below condition will be selected based on other condition present.
14. Now save the view by pressing button present in standard tool bar. Then activate the view with
present in application tool bar. Once view got activated, press to see the contents. Below
screen gets displayed, do not enter any value, just execute it.
15. Because of the condition we have put on acc_typ & status fields, entries in the base table got filtered.
We can see that all the entries have account type as 'CURRENT' & status as 'X'.
Summary
Above steps demonstrate how to create database view from multiple tables having foreign key
relationships.
Contain a single table. They are used to hold a They are used to hold
Used to store master large number of very data from a few number
data small tables(stores of large tables.(stores
customizing data or system data)
system data)
For each transparent It is stored with other Many cluster tables are
table there is one pooled tables in a single stored in a single table
associated table in the table called table pool in in the database called a
database the database table cluster
The database table has The database table has The database table has
the same name, same different name, different different name, different
number of fields and the number of fields and number of fields and
fields have the same fields have different fields have different
names names names
Single table can have Primary key of each Primary key of each
one or more primary key table does not begin table begins with same
with same fields or fields fields or fields
USE: They are used to USE: They reduce the USE: They would be
hold master data e.g. amount of database used when the tables
Table vendors or table resources needed when have primary key in
of customers. Example many small tables have common and data in
of transaction data is to be opened at the these tables are all
orders placed by same time accesses
customers simultaneously