Sap Abap Intro
Sap Abap Intro
ABAP is not case sensitive. We can use upper case or lower case or combination also.
But is space sensitive.All user defined object names in SAP should start with either ‘Z’ or ‘Y’. If
we don’t start object name with either ‘Z’ or ‘Y’ it will ask for access key. Hence we can say
that as a user we don’t have the access to create an object name without starting with ‘Z’ or “Y’.
All statements in ABAP should be terminated with a period (full stop or dot).There are 5
types of programs available in ABAP. They are ….
1) Executable Program
2) Function Group
3) Modulepool Program
4) Include Program
5) Subroutinepool Program.
To create all these programs T-Code is SE38. It is also called ABAP Editor.
EXECUTABLE PROGRAM:- A program which takes input from the user and gives output is
called Executable Program or Report Program. Input screen of report program is called
SELECTION-SCREEN. Output screen of report program is called LIST. By default for every
report program Selection-screen number will be 1000.
Whenever you login into SAP first screen you get is called SAP Easy access screen. In
that screen top one is called Menu bar. After that we have standard Tool bar and after
Application tool bar. On Standard tool bar we have command filed where we enter Transaction
code.
If you want to create any of the above 5 types of programs, in command prompt type
SE38 T-Code and press enter. You will go to ABAP Editor screen. There you enter a program
name starting with either ‘Z’ or ‘Y’. For example just say ZSAMPLE. After entering the
program name press create button. Immediately you will get next screen where you have to enter
title. Title is the meaningful description to the program. That is what is the purpose for which
you are writing the program.
Hence in the title you enter some meaningful description. Let us say MY FIRST
PROGRAM. Then in type you have to choose Executable program because right now we want to
create Executable Program. Then press save button. Immediately you get another screen where
you have to actually enter Package. But later we discuss about package. Time being press Local
object button. Immediately you enter into next screen where you can write the program.
First line will be given by system. You start writing the program from second line
onwards. As I said before you have to remember that every line should be terminated with a
period. First we start writing a simple program.
In ABAP Write is an output statement. If you want to write something in the output you
have to use write statement. When ever write statement is used remember that hard coded value
or text you want to display should be in single codes. Here is first sample program which
displays hello in the output.
After writing the program we usually check the errors. Ctrl+F2 is used to check the
errors. You can also use the button shown below.
After checking the errors we activate the program. Activation gives runtime object.
Ctrl+F3 is used to activate the program. You also use the button shown below.
After activating the program you will get next screen where your program is
automatically selected by the system. There you simply press enter or tick mark button present at
the left hand corner of the screen. Now your program becomes active.
Now you can execute the program. F8 is used to execute the program. You can also use
the button shown in the bottom picture.
You will get the following output.
This screen is called list. Here in this program we are not giving any input. Hence there is
no selection-screen. The program what we have written is…..
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
WRITE 'HELLO'.
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
WRITE 'HELLO'.
WRITE 'HAI'.
You will get following output.
In this output we are getting HELLO and HAI in the same line. But my requirement is
that I should HAI in the next line. For that we have to use new line character ‘/’. In ABAP / is
called new line character.
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
WRITE 'HELLO'.
WRITE / 'HAI'.
The above program can be written like this with combination of statements.
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
WRITE: 'HELLO',
/ 'HAI'.
Now we try to input data into program. In C language if we want to input data into
program we use the key word scanf. Similarly here in ABAP also we are having a key word
called PARAMETERS.
PARAMETERS: - It is a keyword used to declare a variable of particular type which allows the
user to input the data at runtime using keyboard.
The syntax is ……
PARAMETERS A TYPE I.
Here A is a variable name. I stands for integer type variable. That means A is a variable
that is going to hold integer type variable. When ever integer type variable is declared system
automatically allocates 14 digits memory.
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
PARAMETERS A TYPE I.
WRITE: / 'ENTERED NUMBER IS',A.
One more important thing you have to remember in this write statement is that the hard
coded text (i.e. ENTERED NUMBER IS) should be in codes and the declared variable (i.e.A)
should not be in codes. That you have to keep in mind.
Let us see how we can declare character type data. For declaring character type data we
have to use C.
Syntax is ……
PARAMETERS A TYPE C.
C stands for character type variable. When ever character type variable is declared
system allocates single character memory. If many characters are needed to be given as input
specify the length.
Syntax is ……
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
PARAMETERS A(10) TYPE C.
WRITE: / 'ENTERED STRING IS', A.
In character type variables the input given is automatically converted to Upper case. To
avoid this use lower case key word.
Syntax is………..
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
PARAMETERS A(10) TYPE C LOWER CASE.
WRITE: / 'ENTERED STRING IS', A.
In other programming languages if data type is not mentioned system gives error. But in
ABAP if no data type is mentioned system allows you input character type data. That means if
no data type is mentioned the default data type assigned is Character type data.
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
PARAMETERS A(10).
WRITE: / 'ENTERED STRING IS', A.
And one more important to remember is character type data in ABAP can also hold
numeric values and we can do all mathematical operations with those variables.
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
PARAMETERS: A TYPE I,
B TYPE I.
DATA C TYPE I.
C = A + B.
WRITE: / 'THE SUM OF TWO NUMBERS IS', C.
In the above program I have used one more keyword DATA to declare the variable.
DATA:- It is a keyword used to declare a variable of particular type which allows the user to
pass the variables that are generated by system at run time. But user is not allowed to input the
data using key board.
If PARAMETERS keyword is used to declare the variable, the values to that variable
can be given at run time using key board. But if DATA keyword is used to declare the variable
for that variable we can not give the values using keyboard. But we can pass the values that are
generated by system at runtime. In the above program I have passed (A+B) value into C variable.
You can clearly see on the selection-screen that what ever variables that I have declared
in the program using Parameters keyword are coming directly on selection-screen. But in real
time developer develops the program end user will use the application or program. If variables
names are directly coming on the selection-screen, the end user will not be knowing what to
enter there. So I want to have meaningful description to the selection-screen fields. This is done
using selection-texts.
Come back to the program by pressing back button or F3 and execute the program. Now
selection-screen looks like this.
COMMENTING A LINE
When ever a line is commented in any programming language, the line will not be
checked for errors and that line will not be executed while execution.
If you want to comment whole line you have to keep * (start) in the first position or first
column of the line. If you want to comment from particular position of a line you have to use “
(double quotes) and write the comment. You see the syntax present in the bottom.
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
If your control is present in the easy access screen and if it is needed to move to a
transaction we directly type transaction code in command field and press enter to move to the
transaction. If your control is not present in the easy access screen (let us say if it is present in
ABAP editor screen) and if it is needed to go to another transaction , if we directly type
transaction code in the command field system will prompt you saying Function code xyz is not
supported. Actually that Transaction code exits in SAP. To solve this or over come this we
have to either use /n or /o before the T-code.
1. sy-datum -- Date
2. sy-uzeit -- Time
3. sy-mandt -- Logon client number
4. sy-uname -- Logon user name
5. sy-repid -- Report program name
6. sy-cprog -- Current program name
7. sy-dynnr -- Screen number
8. sy-tcode -- Transaction code
9. sy-pagno -- Page number
REPORT ZSAMPLE .
write / sy-datum.
write / sy-uzeit.
write / sy-uname.
write / sy-repid.
write / sy-cprog.
write / sy-mandt.
write / sy-dynnr.
write / sy-pagno.
Write / sy-tcode.
CONTROL STATEMENTS
Control statements are nothing but loops. The property of the loop is to rotate by it self
until loop is terminated. There are 4 types of control statements in SAP. They are
1. DO.
………..
………..
ENDDO.
2. DO N TIMES.
………..
………..
ENDDO.
3. WHILE <CONDITION>.
………..
………..
ENDWHILE.
4. CASE <VARIABLE>.
WHEN <VALUE1>.
………..
………..
WHEN <VALUE2>.
………..
…………
WHEN OTHERS.
………..
…………
ENDCASE.
Exit statement is used to terminate the loop. SY-INDEX is a system variable which gives
present loop iteration number. These both things we use in coming program to see how they act
in the loop.
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
DO.
WRITE / SY-INDEX.
IF SY-INDEX EQ 10.
EXIT.
ENDIF.
ENDDO.
In this program the control comes out of the program when sy-index value becomes 11.
That means at 11th iteration the control comes out of the loop.
For aligning the program lines use PRETTY PRINTER button present on the
application tool bar of the ABAP editor.
Good I have written the above program and I will get the following output and I am
happy.
But my requirement is to see how the variable’s values are changing while execution of
the program. This technique is called Debugging. To see how control is moving while execution
of a program we have to stop the control at some point or at some line in the program. That point
is called Break Point.
BREAK POINT:- It is a line in the program where control is needed to be stopped while
execution.
To keep the break point in the program at particular line, keep the cursor on that
particular line and press STOP button. That line immediately turns to yellow color and at the
status bar you will get a message saying that Breakpoint set. When you execute the program the
control goes to that particular line and program execution is stopped there. To execute the
program further you have to use the following keys.
Maximum number of break points allowed are 30. Sap has given option to keep these
many break points to easily debug a program which is having many lines. When you execute the
program in debugging mode the screen will be in this format.
What ever the fields value you want to find out while execution you double click on the
filed name. Immediately field name comes under Field names and value comes under Field
contents. Otherwise you can also type the Filed name under Field names and press enter.
If you want to delete the break point that is set, keep cursor on the line where break point
is set and once again press STOP button. The break point will be deleted. One more thing you
have to remember is break points can be set in a program if program is active. That means we
can not keep break point in a program if program is inactive. The break points that are set using
Stop button are automatically deleted when you logout. You cannot see those break points once
you login.
If you want to keep permanent break points use keyword BREAK-POINT. This
keyword is used to keep permanent break points. Where you want to stop the control before that
line use this break-point key word.
Sometimes a requirement may come to set the break point for a particular user. In that
case BREAK <USER NAME> key board. Let us assume that you have logged in with sapuser
and if you want to set break point to that particular use only then syntax will be …….
BREAK SAPUSER.
If you execute the program in that sapuser only the program goes to debug mode. If you
execute the program in others the program will be executed in normal mode.
From now onwards what ever the program you write you try to execute the program in
debug mode. So that you will understand the program well.
One more thing you have to remember is that whenever do loop is used it is mandatory to
use Exit statement. Other wise the program will be in infinite loop and you never get the output.
= EQ
< LT
> GT
<= LE
>= GE
>< NE
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
DO 10 TIMES.
WRITE / SY-INDEX.
ENDDO.
The output of this program will also be the same as above i.e. 1 to 10. In Do n times loop
there is need to use Exit statement because the loop will be rotated for those many times and then
the control comes out of the loop.
REPORT ZSAMPLE .
The output of this program is also same as above two programs i.e. 1 to 10.
Now we discuss about the differences between DO loop and WHILE loop a famous
question in C language.
In Do loop we enter into the loop and check the condition, but in While loop first we check
the condition then only we enter in to the loop.
Minimum number of number of times Do loop executed is one time, but while loop
minimum number of times executed is Zero. If condition is wrong the control will not
enter into the loop.
*&---------------------------------------------------------------------*
*& Report ZSAMPLE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZSAMPLE .
PARAMETERS Z.
CASE Z.
WHEN 'A'.
WRITE / 'YOU ENTERED A'.
WHEN 'B'.
WRITE 'YOU ENTERED B'.
WHEN OTHERS.
WRITE 'YOU ENTERED OTHER ALPHABIT'.
ENDCASE.
REPORT ZSAMPLE .
PARAMETERS Z.
CASE Z.
WHEN 'a'.
WRITE / 'YOU ENTERED A'.
WHEN 'B'.
WRITE 'YOU ENTERED B'.
WHEN OTHERS.
WRITE 'YOU ENTERED OTHER ALPHABIT'.
ENDCASE.
Here in this program in when condition ‘A’ is changed to ’a’. What will be the out if
enter a in the selection-screen. Think it and read the following lines.
The output will be YOU ENTERED OTHER ALPHABIT. But why? Because the
input characters in the selection-screen are automatically converted to upper case. This we have
discussed earlier. This is the property of selection-screen. Here my intension is to specify that
what ever you give in the case statement in when condition, give with in quotes only upper case
letters. Other wise you get abnormal results. That you have to remember it. You can try this
thing.
EXIT
STOP
CONTINUE
CHECK
1. EXIT :- We have already discussed about it. Exit statement is used to terminate the
loop. The statements after the loop will be executed normally. Let us analyse the
following program.
*&---------------------------------------------------------------------*
*& Report ZLOOPS *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ZLOOPS .
do.
write / sy-index.
if sy-index eq 10.
exit.
endif.
enddo.
write / 'HELLO'.
The output of the above program will be …..
When sy-index becomes 11 the loop will be terminated and we are having a write
statement after the loop. That will be executed normally. That is the reason why we are
getting HELLO in the output along with the numbers.
2. STOP:- Whenever stop statement is used in the program, immediately after the
execution of the STOP statement the whole program will be terminated. That means no
statement after execution of STOP will be executed. STOP statement completely
terminates the program.
REPORT ZLOOPS .
do.
write / sy-index.
if sy-index eq 10.
stop.
endif.
enddo.
write / 'HELLO'.
3. CONTINUE:- This statement terminates present loop iteration and goes to next loop
iteration. Analyze this program.
REPORT ZLOOPS .
do.
if sy-index eq 2.
continue.
endif.
write / sy-index.
if sy-index eq 10.
exit.
endif.
enddo.
3. CHECK:- This statement returns true or false. If check returns true the statements after
it will be executed. If it returns false the statements after it will not be executed. Even
though check returns false value the loop will be continued.
Analyze this following example.
REPORT ZLOOPS .
do.
write / sy-index.
if sy-index eq 10.
exit.
endif.
check sy-index eq 2.
write / sy-index.
enddo.
Let us assume that the CHECK statement is written outside the loop and it is writtening
false value. Then all the statements written after it will not be executed. That means it acts like
STOP. That is If CHECK is written outside the loop and if it returns false value it acts like
STOP.
STRING OPERATIONS
TRANSLATE:- This keyword is used to convert character type variables from one case to
another case.
Syntax is …….
REPORT ZLOOPS .
PARAMETERS A(10).
TRANSLATE A TO LOWER CASE.
If RAMESH is given as input to this program output will be ramesh. You can also use
upper case key word with translate. But there is no use because what ever we give in selection-
screen will be automatically converted to upper case by default.
REPORT ZLOOPS .
parameters: a(20).
data len type i.
len = strlen( a ).
write len.
If I give RAMESH as input to this program I will get 6 as output, because RAMESH
string is containing 6 characters.
REPORT ZLOOPS .
parameters: a(10),
b(10).
data c(20).
concatenate a b into c.
write c.
If I give input RAMESH for A and REDDY for B I get output as RAMESHREDDY. Let
us assume that I want space between two strings, i.e. space between RAMESH and REDDY I
should write program like this.
REPORT ZLOOPS .
parameters: a(10),
b(10).
data c(20).
concatenate a b into c separated by space.
write c.
If I use this line in the program I will get comma between the strings.
When ever we are giving space in the concatenate syntax it should not be in the quotes,
because space is a key word. If any other character is required between the strings other than
space that character should be given in the quotes.
SPLITTING A STRING:- Let us assume that I require to split a string at a particular character.
Let us say that I want to split a string at comma (,). For this I have to use SPLIT keyword.
Syntax is ….
Sample program is ……
REPORT ZLOOPS .
parameters: a(20).
data: b(10),
c(10).
split a at ',' into b c.
write : / b,
/ c.
Sample program can be seen below. This program is used to replace all characters present
in a string A with space.
REPORT ZLOOPS .
parameters: a(20).
do.
if a ca ','.
replace ',' with space into a.
else.
exit.
endif.
enddo.
write a.
Here in the above program we used a keyword CA which means contains any.
You can debug the program you will understand better about the logic.
If you want to see documentation for any keyword present in ABAP just type the
keyword in ABAP editor and keep cursor on the keyword and press F1. It will open beautiful
documentation regarding the keyword.
R A M E S H
0 1 2 3 4 5
Instead of starting from 1 system gives the positions from 0. That means if I want 3rd
character physically I should search for the 2nd character as per system representation.
Here + is called offsetting mechanism. The difference between mathematical operate + and this
offsetting mechanism + is that, when ever we use mathematical operator + we give space before
and after the + but in this offsetting mechanism + the is no space before and after the +. That is
the difference.
Consider the following example. I give the input as RAMESH into variable A. I want to
rd
move 3 character from this variable into B variable.Then
A = RAMESH
B = A+2(1).
Then M will be move to B.
If I want 3rd and 4th characters into B, Then I have to change above statement as ….
B = A+2(2). Both ME will be moved into B.
If I want to move all character from third position ( I am talking about physical positions) into B,
Then I have to say..
B = a+2. Don’t give any thing in brackets.
If I want entire string into B, I have to say…
B = A+0. whole RAMESH will be moved into B.
REPORT ZLOOPS .
parameters: a(20).
data b(20).
b = a+2(1).
write b.
REPORT ZLOOPS .
parameters: a(20).
data len type i.
data b.
len = strlen( a ).
len = len - 1.
b = a+len.
write b.
SY-FDPOS:- It is a system variable used to find the position where the search is successful. Let
us assume that you are searching for a comma in a string and you want to find out the position
where comma is present in the string. Then we can use this system variable. But it acts
abnormally. If string is not containing the particular character for which you are searching
sy-fdpos system variable takes the declared length of a string which you are searching.
You can execute the following program and see the results.
REPORT ZLOOPS .
parameters: a(30).
if a ca ','.
write sy-fdpos.
else.
write sy-fdpos.
endif.
CUSTOMIZED DATA:- The data which never changes is called Customised data. This data is
not created but configured by functional consultant using SPRO tranction.
MASTER DATA: The data which changes very rarely is called Master data.
TRANSACTIONAL DATA:- The data which changes very frequently is called Transactional
data.
TYPES OF TABLES
TRANSPARENT TABLES:- For every table created in Data dictionary (DDIC) there will be
table created in database with same name, same no of fields, fields having same names and the
order of the fields will be same. That means for every same replica of table or mirror image will
be created in Data base. That means it is having one to one relation. By default every table
created will be Transparent Table.
CLUSTERED TABLES:- Many tables created in DDIC are stored in single table in data base
called table cluster. That means it is having many to one relation. All the tables present in table
cluster will have primary key in common.
POOLED TABLES:- In case also many tables created in DDIC are stored in single table in
database called table pool. Hence it also having many to one relation. Here the tables present in
table pool many have primary key in common.
SE11 T-Code is used to create data base objects which includes tables.
A table can be client dependent or client independent. Table data only client dependent or client
independent, but table structure is client independent.
CLIENT:- Client is SAP specific which contains it’s own master data and it’s own set of
records. All SE38 programs are client independent programs. That means if a program is written
in one client it can be seen all the clients present in the system.
To see all the clients present in a system T-code is SCC4.
If first field of the table is MANDT the table becomes client dependent table, otherwise it
becomes client independent table. If the table is client dependent table the data that you enter in
one client can not be seen in other clients. If the table is client independent table the data that is
entered in one client can be seen all other clients. That means if the table is client independent all
table in all clients contain same data.
Every table should have at least one primary key or foreign key. All primary key fields
and foreign key fields of a table should be declared first.
DOMAIN:- It gives technical attributes of the table fields. Domain is the more reusable
component than data element.
Eg:- Type of data it is going to hold.
2. Enter the description, Delivery class and check on table maintenance allowed. Then click on the
tab Fields.
3. Enter the ZCHAR in fields tab and ZCHAR_DE in field type tab.
4. Double click on ZCHAR_DE. Then it will ask for Save. Press ‘YES”.
5. Save it as a local object on pressing Local object button
6. Now it will ask for the creation of the data element. Press Yes.
7. Click on the Field label tab and enter the lengths and descriptions accordingly.
8. Then press Definition tab and enter the domain.
9. Double click on that zchar_d to create domain, then it will ask for save before going next screen.
Press Yes.
12. Give short description for that domain which you are creating. And mention the data type in data
type field and length in no. characters field an press ENTER.
13. Then press Ctrl+F3. It will ask for save before activation. Save it as a local Object.
14. Press Enter.
16. Press Ctrl+F3. You will get the following screen. Press Back button.
18. Create one more field ZNUM and give data element name as ZNUM_DE. And double click on
‘ZNUM_DE’
20. Now it will ask for the data element creation. Press Yes.
21. Give short description of the data element. And give the lengths and field labels accordingly in
Field label tab.
22. Press on Definition tab and give the domain name in Domain field. Double click on ZNUM_D.
26. Give short description in short text field. Enter the data type and no. characters, Press Enter.
29. Now you will get the following screen. Press Back button.
30. Activate the data element By pressing Activate button or Pressing Ctrl+F3.
Now we have to maintain the technical settings. Press on Technical settings button.
34. Give the data class and size category of the table as per the requirement.
38. Now enter the data into table. Go to Utilitiesà Table contents à Create Entries.
39. Now enter the data in the fields and Save it.
40. You will get the following screen after clicking the Reset Button.
41. Enter the data for the second entry into the table.
42. Save it and press reset.
47. To see the table contents go to Utilities à Table contents à Display. Or press Ctrl+Shift+F10.
49. Press on run button or press F8. Now it will display the table contents.
DATA BROWSERS
These are set of Transaction codes used to main data base objects. The T-codes are.
SE11
SE12
SE16
SE17
SM30
SE54
All these T-codes are used to maintain data base objects. But some of the options may be
present in one T-code and some of the options may not be present in other T-codes. You can
use any of them.
All report programs are stored in TRDIR table. All tables are stored in DD02L
Table. It is table of tables.
Now we discuss about the programs to get the data from Data base tables. Now I take MARA
(Material master table) table as example to get the data. Practice only getting data from
standard tables.
Important fields in MARA table are.
1. MATNR ---- MATERIAL NUMBER
2. MBRSH ---- INDUSTRY SECTOR
3. MTART ---- MATERIAL TYPE
4. MEINS ---- UNIT OF MEASURE
5. NTGEW---- NET WEIGHT
6. BRGEW---- GROSS WEIGHT
My requirement is here I want to give MATNR as input to the program and I want to get
MATNR,MBRSH,MTART,MEINS for the particular material number I give as input. We see
how to declare the variables and How to write the select query.
When ever some input is needed to be given into a program we have to use the
PARAMETERS keyword. Here in this program I want to give material number i.e. MATNR as
input to the program. So I have to go to data base table MARA where MATNR field is present
and I have to find out the data type from there and I have to give in the program. It is a big
process. If want to declare many fields I have to go to many times into the table and see the data
type and I have to give in the program. I don’t want to follow all this lengthy process. Only with
the single statement the data type should be assigned to the variable from data base field and
with this statement The F1 and F4 helps should be assigned to the variable.
Give assign the data type to a variable we have to refer to table name and field name. Here in this
example p_matnr is a variable which will take the characteristics from mara table and matnr
field. Instead of P-matnr we can give any name what ever we want. But here I want to follow
industry notation. Where p Stands for Parameters and matnr stands for the field for which we are
declaring the variable. From now onwards we follow this notation. Sample program is as
follows.
REPORT YSELECT9 .
PARAMETERS P_MATNR TYPE MARA-MATNR.
DATA: D_MATNR TYPE MARA-MATNR,
D_MBRSH TYPE MARA-MBRSH,
D_MTART TYPE MARA-MTART,
D_MEINS TYPE MARA-MEINS.
SELECT MATNR
MBRSH
MTART
MEINS
FROM MARA
INTO (D_MATNR,D_MBRSH,D_MTART,D_MEINS)
WHERE MATNR EQ P_MATNR.
ENDSELECT.
WRITE: / D_MATNR,
D_MBRSH,
D_MTART,
D_MEINS.
In this above program I am able to give single material number as input. But my requirement is I
want to give range of materials as input and I want to get the records in that range. For that we
have to use the keyword SELECT-OPTIONS.
SELECT-OPTIONS:- It allows you to input multiple values or range of values into the
program. But when ever select-options is used the program syntax changes like this.
REPORT YSELECT9 .
TABLES MARA.
SELECT-OPTIONS S_MATNR FOR MARA-MATNR.
DATA: D_MATNR TYPE MARA-MATNR,
D_MBRSH TYPE MARA-MBRSH,
D_MTART TYPE MARA-MTART,
D_MEINS TYPE MARA-MEINS.
SELECT MATNR
MBRSH
MTART
MEINS
FROM MARA
INTO (D_MATNR,D_MBRSH,D_MTART,D_MEINS)
WHERE MATNR IN S_MATNR.
WRITE: / D_MATNR,
D_MBRSH,
D_MTART,
D_MEINS.
ENDSELECT.
If in select-options no input is given in the selection-screen system fetches all the data
from database from the table for which select query is written. Some times I may get a
requirement where this should not happen. That means I want to make the input compulsory. For
this use the keyword obligatory. Obligatory keyword makes the input compulsory. The syntax
is.
Some times I may get a requirement to get single record from data base for the given
input. That means I want to get first single matching record for the input what ever I give. For
that use select single.
Select single is used to get fetch first single matching record for the given input.
when ever select single is used you should not use end select.
WRITE: / D_MATNR,
D_MBRSH,
D_MTART,
D_MEINS.
Some times a requirement may come where I need to fetch n no of records from data
base. For that we use select up to n rows. It should contain endselect. The program is as follows
If I write select up to 1 rows it will fetch single record from data base. This is what is
done when we write select single also. Then what is the difference between select single and
select up to 1 rows. The differences are
1. Select single doesn’t require endselect but select up to 1 rows requires endselect.
2. Use select single when all the primary key fields of a table are used in the select query
where condition. Otherwise use select up to 1 rows. Performance wise SAP is
recommending you to use this way. Even though you use interchangingly nothing will
happen but performance comes down.
REPORT YSELECT9 .
TABLES MARA.
SELECT-OPTIONS S_MATNR FOR MARA-MATNR.
DATA: D_MATNR TYPE MARA-MATNR,
D_MBRSH TYPE MARA-MBRSH,
D_MTART TYPE MARA-MTART,
D_MEINS TYPE MARA-MEINS.
SELECT MATNR
MBRSH
MTART
MEINS
FROM MARA
INTO (D_MATNR,D_MBRSH,D_MTART,D_MEINS)
UP TO 10 ROWS
WHERE MATNR IN S_MATNR.
WRITE: / D_MATNR,
D_MBRSH,
D_MTART,
D_MEINS.
ENDSELECT.
This program will fetch first 10 matching records from database for the given input. If
there are no 10 records to be fetched for the given input system fetches what ever the records that
are there for the given input.
In the above program we are getting the data record by record only. That means if we
want 1 lakh records from data base we are hitting data base 1 lakh times. The performance of the
program is low and the network traffic as also high. So my requirement is I want to get all the
matching records from data base in single shot and I want to place it in the memory. The
memory that we have created in the above programs by those declarations can only hold single
record at a time. To over come these problems SAP has come up with a concept of internal
tables.
INTERNAL TABLES
Internal table is an intermediate table. It can hold multiple records at a time. It is a temporary
table. The memory for the internal table will be allocated at runtime and de allocated after the
execution of the program automatically by the system. The changes that made to the records of
internal table are temporary. The changes are not reflected in data base until some DML
commands are written. Hence original data is retained in data base. There are three types of
internal tables.
Header of the internal table can contain only single record and body can contain multiple
records. If we are writing the logic to move record by record by record from DB table to header
of internal table, after record comes into header it has to be moved to body of internal table by
saying append <table name>. If it is needed to write the records present in the body of internal
table into list, record by record has to be moved to header and written out. For this we have to
write loop at <internal table>. When ever it is needed to make the changes in the records
present in the body of internal table, every record has to be moved to header and modified. We
can not do any operations on the records directly present in the body of the internal table. These
things we have to keep in mind.
REPORT YSELECT9 .
TABLES MARA.
SELECT-OPTIONS S_MATNR FOR MARA-MATNR.
data: begin of itab occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of itab.
select matnr
mbrsh
mtart from mara
into itab
where matnr in s_matnr.
append itab.
endselect.
loop at itab.
write: / itab-matnr,
itab-mbrsh,
itab-mtart.
endloop.
This program is also performance wise very poor. Because here also we are getting the
records one by one only. That means here network traffic will be more and the burden on the
server will be high. My requirement is I want to get the data from data base in single shot and
directly I want to place the records in the body of the internal table. For this we have to modify
the select query like this.
REPORT YSELECT9 .
TABLES MARA.
SELECT-OPTIONS S_MATNR FOR MARA-MATNR.
data: begin of itab occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of itab.
select matnr
mbrsh
mtart from mara
into table itab
where matnr in s_matnr.
*append itab.
*endselect.
loop at itab.
write: / itab-matnr,
itab-mbrsh,
itab-mtart.
endloop.
In the above select query I have used select into table. Into table statement fetches all the
matching required records from database in single shot and directly places into the body of
internal table. Hence in the above program I have commented append and endselect. Always
when ever you use internal tables try to use into table which improves the performance of the
program.
Begin of itab occurs 0 statement creates an internal table with name itab with header
line.
SY-DBCNT:- It is also a system variable which gives no of records fetched from data base
table.
SY-SUBRC:- It is an important system variable. It is used to find out where previous ABAP
statement is executed successfully or not. If it is successfully executed SY-SUBRC value will be
0. If it is not executed successfully it’s value can be 4 or 8 or 12 or 16. If you want to check
whether previous statement executed successfully or not always check whether SY-SUBRC
equal to 0 or not equal to 0. If equal to 0 statement executed successfully, otherwise not executed
successfully.
REPORT ZINTERNAL .
tables mara.
select-options s_matnr for mara-matnr.
data: begin of itab occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of itab.
select matnr
mbrsh
mtart from mara
into table itab where matnr in s_matnr.
if sy-subrc eq 0.
write: / 'no of records fetched from db', sy-dbcnt.
loop at itab.
write: / sy-tabix,
itab-matnr,
itab-mbrsh,
itab-mtart.
endloop.
else.
write: / 'no data to display'.
endif.
FORMATTING TECHNIQUES
REPORT ZINTERNAL .
tables mara.
select-options s_matnr for mara-matnr.
data: begin of itab occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of itab.
select matnr
mbrsh
mtart from mara
into table itab where matnr in s_matnr.
loop at itab.
write: / itab-matnr input,
itab-mbrsh,
itab-mtart.
endloop.
In the above program only MATNR field in the output becomes editable because only for
that field I have used input keyword.
My requirement is I want to make all fields in the output as editable. After each and every
field I have to write input keyword. That unnecessarily increases the code. My requirement is
with single statement every field should become editable. For that use FORAMT INPUT ON
before write statement.
REPORT ZINTERNAL .
tables mara.
select-options s_matnr for mara-matnr.
data: begin of itab occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of itab.
select matnr
mbrsh
mtart from mara
into table itab where matnr in s_matnr.
HOTSPOT: - This keyword is used to display hand symbol when ever cursor is placed on a
particular field in the output.
REPORT ZINTERNAL .
tables mara.
select-options s_matnr for mara-matnr.
data: begin of itab occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of itab.
select matnr
mbrsh
mtart from mara
into table itab where matnr in s_matnr.
loop at itab.
write: / itab-matnr hotspot ,
itab-mbrsh,
itab-mtart.
endloop.
To display hand symbol when ever cursor is placed on any field of output use FORMAT
HOTSPOT ON.
REPORT ZINTERNAL .
tables mara.
select-options s_matnr for mara-matnr.
data: begin of itab occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of itab.
select matnr
mbrsh
mtart from mara
into table itab where matnr in s_matnr.
COLORS:- For giving color to any field present in the output after the write statement give
color <color name/color number>. No direct colors like red, blue, green etc in SAP. We are
having following colors.
You can use the color names or the numbers present beside the color name in the above
list.
REPORT ZINTERNAL .
tables mara.
select-options s_matnr for mara-matnr.
data: begin of itab occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of itab.
select matnr
mbrsh
mtart from mara
into table itab where matnr in s_matnr.
loop at itab.
write: / itab-matnr color col_heading ,
itab-mbrsh color col_positive ,
itab-mtart color col_negative .
endloop.
If you execute above program you get matnr field values in blue color, mbrsh field in
green color and mtart field in red color. If you want same color to all fields of output use
FORMAT COLOR <COLOR NAME/COLOR NUMBER> before the write statement.
REPORT ZINTERNAL .
tables mara.
select-options s_matnr for mara-matnr.
data: begin of itab occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of itab.
select matnr
mbrsh
mtart from mara
into table itab where matnr in s_matnr.
REPORT ZINTERNAL .
tables mara.
select-options s_matnr for mara-matnr.
data: begin of itab occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of itab.
select matnr
mbrsh
mtart from mara
into table itab where matnr in s_matnr.
loop at itab.
write: / itab-matnr ,
itab-mbrsh ,
itab-mtart .
endloop.
skip 5.
write / 'hai'.
BACK:- This keyword is used to move the control to first line first column of the output list. Let
us assume the requirement. I have displayed around 10 lines in the output and the next write
statement what I am going to write should display the contents in first line of the list. If I write
simply write statement it will be displayed after those lines which are already displayed in the
output. But my requirement is I have to take the control to first line of the output. For this use
BACK keyword. But the problem with back is that what ever that is written previously in first
line and first column will be over written. To avoid this specify position in write statement so
that the contents will be written in that particular position instead of writing in the first column.
REPORT ZINTERNAL .
tables mara.
select-options s_matnr for mara-matnr.
data: begin of itab occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of itab.
select matnr
mbrsh
mtart from mara
into table itab where matnr in s_matnr.
loop at itab.
write: / itab-matnr ,
itab-mbrsh ,
itab-mtart .
endloop.
back.
write /45 'hai'.
When ever begin of <internal table name> occurs 0 is used to create an internal table system by
default allocates 8KB of memory. If no of records fetched from database table are more, that
memory system initially allocated may not be sufficient. Hence system will allocate extra
memory. The extra memory allocated will be in multiples of 8KB only. Hence in this case there
is a possibility that memory may be wasted. Since memory is allocated in pockets of 8KB the
performance of the program comes down. To avoid all these problems SAP has come up with a
concept of internal tables without header line. In this case internal table directly contains body
without header. When ever internal table is created without header line we have to create a work
area. Here work area acts like a header which can contain single record. The condition is the
structure of work area and internal table should be same or at least it should be compatible.
When ever it is needed to create an internal table without header line we use TYPES
keyword in the program. This keyword is used to create a structure. Using this structure create
an internal table as well as work area. We see a sample program which creates an internal
without header line.
REPORT YSELECT9 .
TABLES MARA.
SELECT-OPTIONS S_MATNR FOR MARA-MATNR.
types: begin of ty_itab,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of ty_itab.
select matnr
mbrsh
mtart from mara
into wa where
matnr in s_matnr.
append wa to itab.
endselect.
In this above program first I have created a structure ty_itab using types statement
( Structure internally contains fields). Using this structure I have created an internal table
(ITAB) and as well as work area (WA). When ever type standard table of syntax is used it
creates an internal table. If we use only type in the syntax it creates work area. But in the above
program first record by record is coming into work area and then it is appended into the body of
the internal. This program is not good performance wise. Because we have to get the records in
single shot from database and have to place directly into the body of internal table. For that
purpose we can write the program like this which improves the performance.
REPORT YSELECT9 .
TABLES MARA.
SELECT-OPTIONS S_MATNR FOR MARA-MATNR.
types: begin of ty_itab,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of ty_itab.
select matnr
mbrsh
mtart from mara
into table itab where
matnr in s_matnr.
* append wa to itab.
* endselect.
CLEAR:- This keyword clears/deletes the contents from the header of internal table or from the
variable memory.
REFRESH:- This keyword deletes the contents from the body of the internal table.
FREE:- This keyword is used to deallocate the memory allocated for internal table as well as
any variable .
Till now we are only getting the data from single table. If I want to get the data from
multiple tables, we have to go for joins. There are two types of joins available in SAP. They
are…
1. Inner join
2. Left outer join
It is very important to remember that when ever you want to join the tables it is
mandatory that there should be at least one common field between them. Without a common
field between the tables we can’t join the tables. For taking the common field we should not
consider MANDT field. Common field may be primary key field or other field.
INNER JOIN:- It is exactly like intersection or logical AND operator. This inner join will fetch
the data from the tables if the record is available in all the tables that you are joining. If it is not
available in one of the tables, the data will not be fetched.
LEFT OUTER JOIN:- It is like union. But it is not exactly like union. When ever left outer join
is used in select query, it will fetch all the records from left side table and the common records
from right side table.
Inner join between table 1 and table 2, where column D in both tables in the join condition is set the same:
Table 1 Table 2
|----|----|----|----| |----|----|----|----|----|
| A | B | C | D | | D | E | F | G | H |
|----|----|----|----| |----|----|----|----|----|
| a1 | b1 | c1 | 1 | | 1 | e1 | f1 | g1 | h1 |
| a2 | b2 | c2 | 1 | | 3 | e2 | f2 | g2 | h2 |
| a3 | b3 | c3 | 2 | | 4 | e3 | f3 | g3 | h3 |
| a4 | b4 | c4 | 3 | |----|----|----|----|----|
|----|----|----|----|
\ /
\ /
\ /
\ /
\/
Inner Join
|----|----|----|----|----|----|----|----|----|
| A | B | C | D | D | E | F | G | H |
|----|----|----|----|----|----|----|----|----|
| a1 | b1 | c1 | 1 | 1 | e1 | f1 | g1 | h1 |
| a2 | b2 | c2 | 1 | 1 | e1 | f1 | g1 | h1 |
| a4 | b4 | c4 | 3 | 3 | e2 | f2 | g2 | h2 |
|----|----|----|----|----|----|----|----|----|
Left outer join between table 1 and table 2 where column D in both tables set the join condition:
Table 1 Table 2
|----|----|----|----| |----|----|----|----|----|
| A | B | C | D | | D | E | F | G | H |
|----|----|----|----| |----|----|----|----|----|
| a1 | b1 | c1 | 1 | | 1 | e1 | f1 | g1 | h1 |
| a2 | b2 | c2 | 1 | | 3 | e2 | f2 | g2 | h2 |
| a3 | b3 | c3 | 2 | | 4 | e3 | f3 | g3 | h3 |
| a4 | b4 | c4 | 3 | |----|----|----|----|----|
|----|----|----|----|
\ /
\ /
\ /
\ /
\/
Left Outer Join
|----|----|----|----|----|----|----|----|----|
| A | B | C | D | D | E | F | G | H |
|----|----|----|----|----|----|----|----|----|
| a1 | b1 | c1 | 1 | 1 | e1 | f1 | g1 | h1 |
| a2 | b2 | c2 | 1 | 1 | e1 | f1 | g1 | h1 |
| a3 | b3 | c3 | 2 |NULL|NULL|NULL|NULL|NULL|
| a4 | b4 | c4 | 3 | 3 | e2 | f2 | g2 | h2 |
|----|----|----|----|----|----|----|----|----|
See the sample program to join two tables using inner join..
REPORT ZJOINS1 .
TABLES MARA.
SELECT-OPTIONS S_MATNR FOR MARA-MATNR.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MBRSH TYPE MARA-MBRSH,
MTART TYPE MARA-MTART,
WERKS TYPE MARC-WERKS,
END OF TY_MARA.
SELECT MARA~MATNR
MARA~MBRSH
MARA~MTART
MARC~WERKS
INTO TABLE IMARA
FROM MARA INNER JOIN MARC
ON MARA~MATNR EQ MARC~MATNR
WHERE MARA~MATNR IN S_MATNR.
REPORT ZJOINS1 .
TABLES MARA.
SELECT-OPTIONS S_MATNR FOR MARA-MATNR.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MBRSH TYPE MARA-MBRSH,
MTART TYPE MARA-MTART,
WERKS TYPE MARC-WERKS,
MAKTX TYPE MAKT-MAKTX,
END OF TY_MARA.
SELECT MARA~MATNR
MARA~MBRSH
MARA~MTART
MARC~WERKS
MAKT~MAKTX
INTO TABLE IMARA
FROM MARA INNER JOIN MARC
ON MARA~MATNR EQ MARC~MATNR
INNER JOIN MAKT
ON MARA~MATNR EQ MAKT~MATNR
WHERE MARA~MATNR IN S_MATNR
AND MAKT~SPRAS EQ 'EN'.
LOOP AT IMARA INTO WA_MARA.
WRITE: / WA_MARA-MATNR,
WA_MARA-MBRSH,
WA_MARA-MTART,
WA_MARA-WERKS,
WA_MARA-MAKTX.
ENDLOOP.
See the sample program joining two tables using left outer join.
REPORT ZJOINS1 .
TABLES MARA.
SELECT-OPTIONS S_MATNR FOR MARA-MATNR.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MBRSH TYPE MARA-MBRSH,
MTART TYPE MARA-MTART,
WERKS TYPE MARC-WERKS,
END OF TY_MARA.
SELECT MARA~MATNR
MARA~MBRSH
MARA~MTART
MARC~WERKS
INTO TABLE IMARA
FROM MARA LEFT OUTER JOIN MARC
ON MARA~MATNR EQ MARC~MATNR
WHERE MARA~MATNR IN S_MATNR.
Joining of many tables decreases the performance of the program. In real time you will
not be allowed to join more than 4 or 5 tables. FOR ALL ENTRIES is used as substitute for
joins. When ever for all entries is used it is needed to declare separate internal tables for each
data base table. After getting the data from each data base table into respective internal tables
move the records into another internal table which is containing the fields from both the tables.
REPORT ZFORALL .
tables mara.
select-options s_matnr for mara-matnr.
data: begin of imara occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of imara.
loop at final.
write: / final-matnr,
final-mbrsh,
final-mtart,
final-werks.
endloop.
else.
write: / 'no data to display'.
endif.
While using for all entries it is mandatory to check whether base internal table is empty
or not.. If base internal table is empty the second select query where for all entries is used will
fetch all the data from data base table for which select query is written. In the above program if I
don’t check the condition whether IMARA is empty or not , if IMARA is empty the complete
data from MARC table will come into IMARC internal table.
To check whether base internal table is empty or not use this syntax
if not imara[] is initial.
See the sample program where we need not use third internal table and as well as
how to change or modify the existing records of the internal table.
REPORT ZFORALL2 .
tables mara.
select-options s_matnr for mara-matnr.
types: begin of ty_mara,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
werks type marc-werks,
end of ty_mara.
types: begin of ty_marc,
matnr type marc-matnr,
werks type marc-werks,
end of ty_marc.
MODULARIZATION
It is a technique of splitting larger code into smaller block of code. It is done for easy
understanding, easy debugging and to decrease the code. We can achieve this in SAP using 4
concepts.
1. Events
2. Subroutines
3. Include programs
4. Function modules
TYPES OF REPORTS
1. Classical Reports
2. Interactive Reports
3. Drill down Reports
CLASSICAL REPORT:- A report which can generate only one list, that is Basic List, is
called classical report. First List of a report is called BASIC LIST.
INTERACTIVE REPORT:- A report which can generate one basic list and upto 20 interactive
lists is called an interactive report. First list is called Basic List and Interactive lists are called
Secondary Lists.
DRILL DOWN REPORTS:- In these reports the output will be in tree format.
1. INITIALIZATION
2. AT SELECTION-SCREEN
(a) AT SELECTION-SCREEN ON <FIELD>
(b) AT SELECTION-SCREEN OUTPUT
(c) AT SELECTION-SCREEN ON VALUE-REQUEST FOR <FIELD>
3. START-OF-SELECTION
4. TOP-OF-PAGE
5. END-OF-PAGE
6. END-OF-SELECTION
INITIALIZATION:- This is the first event to be triggered. This event is triggered before the
selection-screen is displayed. This event is used to give default vales to selection-screen fields.
START-OF-SELECTION:- The main logic of the program is written in this event. Usually
write statement is recognized from this event.
TOP-OF-PAGE:- This event is used to write something on top of every page. The first write
statement or output statement (SKIP) of a page triggers this event.
END-OF-PAGE: - This event is used to write something at end of every page. Last line of the
page triggers this event. We have to allocate the line for end of page.
END-OF-SELECTION:- Usually program output is written in this event. This event is used to
handle abnormal termination of the program. The stop statement written in start-of-selection
event takes the control to end-of-selection event.
See the following sample program which uses initialization event and write statement.
REPORT ZSAMPLE .
parameters p_matnr type mara-matnr.
initialization.
write : 'HELLO'.
I have written write statement in Initialization event and the program is having selection-
screen. The write statement can not be seen. Actually HELLO will be written but that is
immediately overwritten by selection-screen. That is the reason why we can not see the write in
output.
REPORT ZSAMPLE .
Initialization.
write : 'HELLO'.
If I execute the above program HELLO will be seen because this program is not having
selection-screen.
Now see the following program which is having all the above events.
select matnr
mbrsh
mtart from mara
into table itab
where matnr in s_matnr.
stop.
end-of-selection.
loop at itab.
write: / itab-matnr,
itab-mbrsh,
itab-mtart.
endloop.
count = sy-linct - sy-linno - 1.
skip count.
top-of-page.
write / 'data from mara table'.
end-of-page.
write: /45 'page number: ', sy-pagno.
MESSAGE CLASS:- Its is a collection of messages. Each message class can hold 1000
messages. (000 to 999). T-code for creation of message class is SE91.
There are 5 types of messages in SAP.
1. Error Message (E).
2. Warning Message (W).
3. Information Message (I).
4. Success Message (S).
5. Abend Message (A).
A Message created in a message class can be used as any of the above 5 types. What
ever the letter I have given in the brackets after each type of message should be used to
specify the system about which type message you are going to raise.
Error Message temporarily halts the program. That means once the errors are
corrected the control moves forward. But Abend message completely terminates the
program.
Information and Success messages give the message and move forward. They
won’t stop the control.
Warning message also temporarily terminates the program.
When ever you go for validations better you use Error message.
Now we look into statements that are new in the above program.
LINE-COUNT 10(1):- Here 10 specifies no of lines per a page. If this is not mentioned
system takes 65,000 lines as single page. So when ever you want to specify no of lines
per a page you have to mention here. 1 that is written in brackets indicates no of lines
allocated for end-of-page. That means the complete meaning is 10 lines per each page
and in those 10 lines 1 line is allocated for end-of-page.
LINE-SIZE 500:- Line-size specifies about how many characters should be printed in a
line. If it is not mentioned system will print only 255 characters in a page. Maximum
line-size allowed is 1023 characters. This is used in a program when you want to display
many fields in the output. Because in that case normal output of 255 characters per a line
may not be sufficient.
SY-LINCT:- This is a system variable which gives no of lines allocated per a page. In
the above program sy-linct value will be 10.
SY-LINNO:- This is also a system variable which gives no of lines printed in a page.
SY-LILLI:- It is a system variable which gives clicked line number in the list.
SY-LISEL:- It is a system variable which contains whole contents of clicked line.
INTERACTIVE REPORTS:- A report which can generate one basic and up to 20 interactive
lists is called an interactive report.
First list is called basic list and next lists are called interactive lists.
1. INITIALIZATION
2. AT SELECTION-SCREEN
a. AT SELECTION-SCREEN ON <FIELD>
b. AT SELECTION-SCREEN OUTPUT
c. AT SELECTION-SCREEN ON VALUE-REQUEST FOR <FIELD>
3. START-OF-SELECTION
4. TOP-OF-PAGE
5. END-OF-PAGE
6. AT LINE-SELECTION
7. AT USER-COMMAND
8. TOP-OF-PAGE DURING LINE-SELECTION
AT LINE-SELECTION:- This event is used to generate an interactive list when ever particular
in the out is double clicked.
TOP-OF-PAGE DURING LINE-SELECTION:- This event is used to write some thing on top
of every page of individual secondary lists. Top-of-page event is used
to write something on only basic list.
AT USER-COMMAND:- This is used to handle user action on the screen when ever standard
menu bar and application tool are changed.
In these interactive lists we take the help of HIDE table. Hide is an intermediate
memory area which is used to hold the values that are used for generation of next secondary list.
Hide statement should be used after write statement. Then only you will get correct results. Other
wise you will get wrong values. Hide table values you can’t see even in debug mode.
SY-LSIND:- This is also a system variable which gives present processing list number. It’s
value will be 0 for basic list and 1 to 20 for secondary lists. We will make use of this sy-lsind in
our next program to tell the system about which logic should be executed while going to a
particular list..
SY-LILLI:- This is a system variable which will hold the clicked line number.
SY-LISEL:- This is also a system variable which will hold all the contents of clicked line.
SY-UCOMM:- This system variable is used to hold the function code that is triggered for the
user actions on the screen.
start-of-selection.
select matnr
mbrsh
mtart from mara
into table itab
where matnr in s_matnr.
loop at itab.
write: / itab-matnr,
itab-mbrsh,
itab-mtart.
HIDE ITAB-MATNR.
endloop.
AT LINE-SELECTION.
CASE SY-LSIND.
WHEN 1.
SELECT MATNR
WERKS FROM MARC
INTO TABLE ITAB1
WHERE MATNR EQ ITAB-MATNR.
LOOP AT ITAB1.
WRITE: / ITAB1-MATNR,
ITAB1-WERKS.
HIDE ITAB1-MATNR.
ENDLOOP.
WHEN 2.
select MATNR
MAKTX FROM MAKT
INTO TABLE ITAB2
WHERE MATNR EQ ITAB1-MATNR.
LOOP AT ITAB2.
WRITE: / ITAB2-MATNR,
ITAB2-MAKTX.
ENDLOOP.
ENDCASE.
TOP-OF-PAGE.
WRITE / 'DATA FROM MARA TABLE'.
TOP-OF-PAGE DURING LINE-SELECTION.
CASE SY-LSIND.
WHEN 1.
WRITE ' DATA FROM MARC TABLE'.
WHEN 2.
WRITE 'DATA FROM MAKT TABLE'.
ENDCASE.
SE41 T-code is used to create user defined GUI status or PF status for a program. See the
following program which is written to handle the user actions on the screen for user defined GUI
status.
start-of-selection.
SET PF-STATUS 'ZPF1'.
select matnr
mbrsh
mtart from mara
into table itab
where matnr in s_matnr.
loop at itab.
write: / itab-matnr,
itab-mbrsh,
itab-mtart.
endloop.
AT USER-COMMAND.
CASE SY-UCOMM.
WHEN 'TCODE'.
CALL TRANSACTION 'SE11'.
WHEN 'DISPLAY'.
WRITE: 'LIST NUMBER IS', SY-LSIND.
WHEN 'WRITE'.
WRITE 'HELLO'.
ENDCASE.
SUBROUTINES
When ever same logic is needed to be executed many times in a program, create a
subroutine and call the subroutine when ever the logic is needed to be executed.
Perform statement is used to create the subroutine and to call the subroutine.
A subroutine can be created with passing the variables and without passing the variables. The
logic of the subroutine is written between form and endform. It is not required to start the
subroutine name with Z OR Y.
The variables that are passed into subroutine are called global variables or actual
variables. The variables that are used to receive the values in subroutine from main program are
called local variables or formal variables.
There are 3 ways to pass the variables into subroutine.
1. CALL BY VALUE
2. CALL BY REFERENCE
3. CALL BY VALUE AND RESULT
CALL BY VALUE:- In this new memory area is allocated for the local variables between form
and endform. When the values of local variables are changed , they are changed in only or
reflected in only newly created memory area. The original values of corresponding global
variables are not changed.
CALL BY REFERENCE:- In this no new memory area is created for the local variables
between form and endform. They make use of memory of the corresponding global variables.
Hence when the values of local variables are changed between form and endform immediately
the corresponding global variables values are changed.
CALL BY VALUE AND RESULT:- In this new memory area is created for local variables.
When ever the values of local variables are changed between form and endform, the changes are
reflected in only newly created memory area. But when the control is moving back from
subroutine to main program, the values present in local memory are copied back to the
corresponding global variables memory area.
Hence we can say that in CALL BY VALUE the values of global variables are never
changed when corresponding local variables values are changed, in CALL BY REFERENCE
the values of global variables are changed immediately. In CALL BY VALUE AND RESULT
the values of corresponding global variables are changed control moves from subroutine to main
program.
First we will see how to create the subroutine with passing the variables.
REPORT ZSELECT20 .
parameters: a type i,
b type i.
data c type i.
c = a + b.
ENDFORM. " add
No need to write the form and endform manually. When you double click the perform
name written in perform statement (ADD) automatically system will create form and endform.
Between the form and endform write the logic what ever you want.
Now we will see how to create a subroutine without passing the variables.
REPORT ZSELECT20 .
tables mara.
select-options s_matnr for mara-matnr.
data: begin of itab occurs 0,
matnr type mara-matnr,
mbrsh type mara-mbrsh,
mtart type mara-mtart,
end of itab.
perform select.
perform display.
*&---------------------------------------------------------------------*
*& Form select
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM select .
select matnr
mbrsh
mtart from mara
into table itab
where matnr in s_matnr.
loop at itab.
write: / itab-matnr,
itab-mbrsh,
itab-mtart.
endloop.
ENDFORM. " display
start-of-selection.
write 'hello'.
No statement after form and endform is accessible. After form and endform only form
and endform is accessible. If you want to access any statement after form and endform we have
to use start-of-selection event.
Usually subroutines are local to a program. They are not stored in database. They are not
self executable. If you want to call a subroutine ADD (say) created in some X program from Y
program , you have to write this syntax in Y program
These programs are also used for reusability. Include program name should start with either Z or
Y. There are two ways to create include programs.
1. Directly from SE38 we can create include program. Here we have to choose program
type as include program.
2. Directly from the program also we can create include program. In the program write
Include <include program name>. Keep cursor on the include program name and double
click. If the program is not available, it will ask for the creation, say yes and move
forward.
After creation of include program if it is needed to call the include program from another
program use the statement include <Include program name>.
FUNCTION MODULES.
In modularization this is very important concept. Function module has to be saved under
a function group. That means function group is a must for creation of function module. SE80 or
SE37 T-codes are used to create function group. Function group is a collection or pool of
function modules.
There are 3 types of function modules.
1. Normal Function module
2. Remote enabled Function module
3. Update function module
By default every function created will be normal function module.
Uxx include contains source code of the function modules. For each function module one
include will be added in Uxx include. On seeing the no of includes in Uxx include we can know
how many function modules are present in a particular function group.
SE37 T-code is used to create a function module. Function module name need not start
with z or y. The variables that are needed to be sent from program or the variables that are
needed to be populated at run time should be given under Import. The values that are needed to
be sent from function modules to program should be given under export or changing. When want
to move the internal table data from function module to program or vice versa it should be taken
in tables. Exceptions can be written under exceptions. The logic of the function module should
be written under source code.
See the following function module which is written to add two numbers.
Function modules are self executable. We can execute them by pressing F8 button. All
function modules are stored in TFDIR table.
If you want to call the function module from a program you have to use pattern button
which is present on application tool bar.
You see this sample program which is calling the previous function module written.
REPORT ZADDITION .
parameters: x type i,
y type i.
data z type i.