16 - Intro To ABAP Exercises
16 - Intro To ABAP Exercises
0
Exercises: Introduction to
ABAP Programming
The following exercises are designed to help you understand the basics of the ABAP
programming language.
The solutions for the programs can be found in package YABAP47TRG.
solutions can be found in your ABAP manuals.
Written
Before you begin the Introduction to ABAP exercises, you must create a new Package and
a new Change Request, so your work is stored as it might be on a client project.
To do this, find the sheet used for your first Package and Change Request Exercise and use this
if you need it to help you through the abbreviated instructions below:
Create a new Package, by going to the Repository Browser (transaction SE80), and typing
YAPXXB in the package field (where XX is the last two digits of your logon id). Click on the
display pushbutton, and create the new Package, calling it something like Intro to ABAP
Package for <your name>.
NOTE: When you hit the save button, do not use your previous Change Request. Instead,
when the Change Request Query Screen appears, click on the Create Request pushbutton,
enter a short description, such as Intro to ABAP Change Request for <your name>, and save.
Write down the number of your change request here: ____________________________
Use the green arrow to go back to your new Package, and ensure that all your programs in the
Introduction to ABAP section of the course are stored in this Package.
Page 1
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 2, Exercise 1
The purpose of this exercise is for you to learn how to create an ABAP report program. There
are many ways for you to create a program.
To create a program from within the Repository Browser, perform the following steps:
1.
2.
3.
4.
Page 2
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 3, Exercise 1
Program Name: YAPxx03_1 (where xx is the last two digits of your logon id)
The purpose of this exercise is for you to create a simple ABAP report using system fields, icons,
and numbered text elements (text symbols).
Using the program you created in the previous example, code the appropriate statements to
generate the report shown in the sample output:
For the current date, time, and user name (logon id), use the following system fields: SYDATUM, SY-UZEIT, and SY-UNAME. To display all the available system fields, click on the
Help pushbutton in a program and enter the table structure SY.
Use the following icons: ICON_DATE, ICON_TIME, and ICON_ADDRESS. To display all the
available icons, execute the program SHOWICON from the initial screen of the ABAP Editor
(transaction code SE38).
You should not use any text literals in the program. To display your name and address lines, use
the numbered text elements (text symbols).
Use the Titles and Headers section of the text elements to maintain the reports list header
(User Address Information).
Page 3
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 4, Exercise 1
Program Name: YAPxx04_1 (where xx is the last two digits of your logon id)
The purpose of this exercise is for you to become familiar with creating data objects in an ABAP
program.
You are to create a program that acts as an adding machine. Two integers should be entered as
parameters. These integers and the result should be used to generate the report (or list).
Because you already have a program in your package, the first six steps in the chapter 2
exercise can be changed to:
1. Go to your Intro to ABAP package in the Repository Browser.
2. Click on the Programs branch in the hierarchy list.
3. Click on the Create pushbutton (or choose menu path Development object > Create).
4. In the dialog box that is displayed, enter the program name YAPxx04_1 (where xx is the
last two digits of your logon id) and turn off the With TOP INCL. option.
5. Press the Enter key (or click on the Continue [green check] pushbutton) to go into the
Program Attributes screen.
When you are defining the programs variables, use the LIKE option wherever possible.
Again, you should not use any text literals in the program. To display the text on the list, use the
numbered text elements (text symbols).
Use the Titles and Headers section of the text elements to maintain the reports list header
(Adding Machine).
Use the Selection texts section of the text elements to maintain the parameter texts on the
selection screen.
See the sample selection screen and list on the next page.
Page 4
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 5
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 4, Exercise 2
Program Name: YAPxx04_2 (where xx is the last two digits of your logon id)
The purpose of this exercise is for you to become familiar with creating data objects and
performing date calculations.
You are to create a program that calculates how many days old you are and when your 10,000th
day birthday will be (or was). Your birthday should be entered as a parameter (set your birthday
as the default). See the sample output to determine the contents and layout of the report.
The birthday parameter should be created TYPE SYDATUM.
Do not maintain any text elements. Instead of using text symbols, you can use constants or text
literals. In practice, you would not use hard-coded text literals. However, in your remaining
exercises, these text literals are acceptable because we want you to focus on the new concepts
in each exercise, not creating text elements every time.
Page 6
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 4, Exercise 3
Program Name: YAPxx04_3 (where xx is the last two digits of your logon id)
The purpose of this exercise is for you to become familiar with creating data objects and userdefined data types.
You are to create a program that calculates the temperature in Celsius from a Fahrenheit
temperature entered as a parameter. See the sample output to determine the contents and
layout of the report. Notice that the standard page header with an underline is not on the report
(hint: get help on the REPORT statement).
The equation for this temperature conversion is: Celsius = 5/9 * (Fahrenheit - 32). Use constants
to define the conversion values (i.e. 5/9 and 32).
When declaring the variables for Fahrenheit and Celsius, you should use a user-defined data
type named DEGREES. This data type should be a length 7 packed field that will store the
temperature with two decimal places.
Do not maintain any text elements. Instead of text symbols, you can use constants or text
literals.
Page 7
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 5, Exercise 1
Program Name: YAPxx05_1
The purpose of this exercise is for you to be able to use the basic Open SQL SELECT
statement to read data from a database table.
You are to create a program that reads the data in the YMOVIE table and generates a report with
this information. The YMOVIE table contains Academy Award information. This table is not a
standard SAP table (notice that its name starts with a Y). We created this table for the purpose
of your ABAP exercises. Keep in mind that the concepts you learn using the YMOVIE table can
be applied to any ABAP Dictionary table.
Before coding this program, you should examine the structure of the YMOVIE table. Also, you
should view the table contents. You can display a tables contents using the Data Browser
(transaction code SE16). You can also display a tables contents from the ABAP Dictionary by
choosing the Utilities > Table contents menu path.
Maintain the column headings for the report using the text elements; however, do not maintain
any text symbols.
See the sample output to determine the contents and layout of the report. Make sure that you
can scroll to the right to see the entire Academy Award note (hint: get help on the REPORT
statement).
Be sure to check SY-SUBRC to determine if any records exist in YMOVIE. If not, write an
appropriate message. The SY-SUBRC check should always come immediately after the
ENDSELECT. Do you know why it should always occur at this point and not within the
SELECT ENDSELECT loop?
Please note that the actual contents of the YMOVIE table may be different when you create this
program from the contents at the time of the following screen print. This is true for all screen
prints displaying database records. You can always execute the solution programs to determine
the exact contents that your reports should display.
Page 8
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 5, Exercise 2
Program Name: YAPxx05_2 (where xx is the last two digits of your logon id)
The purpose of this exercise is for you to be able to restrict the records retrieved from the
database table.
You are to create a program that reads the data in the YMOVIE table for a specific category
entered as a parameter. See the sample output to determine the contents and layout of the
report. Again, make sure that you can scroll to the right to see the entire Academy Award note.
You can copy your previous program and make the necessary changes. To copy a program,
select it in the Repository Browser and click on the Copy pushbutton (or choose the
Development object > Copy menu path). You can also copy a program from the initial screen
of the ABAP Editor (transaction code SE38).
Maintain the column headings for the report using the text elements; however, do not maintain
any text symbols.
Be sure to check SY-SUBRC to determine if any records exist in YMOVIE for the specified
category. If not, write an appropriate message. The SY-SUBRC check should always come
immediately after the ENDSELECT.
Page 9
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Notice the drop-down arrow on the category parameter. You should not create a search help for
this parameter. The drop-down arrow to get a list of possible values is on this parameter
because it was defined LIKE YMOVIE-CATEGORY. The category field in the YMOVIE table
points to a domain that has a set of valid values. Because of the valid values at the domain
level, any parameter defined LIKE the category field will have a drop-down arrow to get a list of
possible values.
Chapter 6, Exercise 1
Page 10
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Program Name: YAPxx06_1 (where xx is the last two digits of your logon id)
The purpose of this exercise is for you to become familiar with control statements in ABAP.
You are to create a program that acts as a calculator to perform addition, subtraction,
multiplication, or division. On the selection screen, the user should enter two integers and an
operator (+, -, *, or /).
The report generated should show the two integers, the operator, and the result. See the sample
output to determine the contents and layout of the report. If an invalid operator is entered, an
appropriate message should be displayed on the report. If trying to divide by zero, an
appropriate message should be displayed on the report.
Do not maintain any text elements.
variables wherever possible.
Page 11
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 12
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 6, Exercise 2
Program Name: YAPxx06_2 (where xx is the last two digits of your logon id)
The purpose of this exercise is for you to become familiar with control statements in ABAP.
Control break processing is common to all programming languages, including ABAP. You are to
create a program that calculates the total seats occupied for each artist in the YCONCERT table.
The YCONCERT table contains information about music concerts. This table is not a standard
SAP table (notice that its name starts with a Y). We created this table for the purpose of your
ABAP exercises. Keep in mind that the concepts you learn using the YCONCERT table can be
applied to any ABAP Dictionary table.
Before coding this program, you should examine the structure of the YCONCERT table. Also,
you should view the table contents. You can display a tables contents using the Data Browser
(transaction code SE16). You can also display a tables contents from the ABAP Dictionary by
choosing the Utilities > Table contents menu path.
See the sample output to determine the contents and layout of the report. The following fields
will be used in the report: YCONCERT-ARTIST, YCONCERT-EVENTDATE, YCONCERTSEATSMAX, YCONCERT-SEATSOCC, and YCONCERT-LOCATION.
The total seats occupied should be printed at each artist break. CAUTION: ABAP may not
understand the difference between the first record selected and a normal control break. If this
does not make sense now, it will when you begin testing your program. (hint: the system field
SY-DBCNT indicates the records selected in a SELECT loop and it is incremented with each loop
pass).
Because you are grouping the data in the YCONCERT table by artist, you need to be sure that
the table is sorted on this field. Because you cannot assume that it is sorted in this manner, you
can specify the order in which to retrieve records. Code your SELECT statement as follows:
SELECT * FROM YCONCERT ORDER BY ARTIST.
We will cover the ORDER BY addition (and others) in a later chapter.
Maintain the column headings for the report using the text elements; however, do not maintain
any text symbols.
Page 13
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 14
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 7, Exercise 1
Program Name: YAPxx07_1 (where xx is the last two digits of your logon id)
The purpose of this exercise is for you to become familiar with internal table processing.
You are to create a program that generates a report of the top five concerts with the highest
attendance (seats occupied).
You should create an internal table (with a header line) with fields for the following information:
artist, date, seats occupied, and location.
Read the YCONCERT table entries into the internal table and list the top five concerts on the
report. If no entries exist in the table, an appropriate message should be displayed. See the
sample output to determine the contents and layout of the report.
Do not maintain any text elements.
Page 15
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 7, Exercise 2
Program Name: YAPxx07_2 (where xx is the last two digits of your logon id)
The purpose of this exercise is for you to become familiar with internal table processing.
You are to create a program that generates a report of the concerts with attendance over 18,000
(seats occupied). The report should also display the total combined attendance for these
concerts.
You should create an internal table (with a header line) with fields for the following information:
artist, date, seats occupied, and location.
Read the YCONCERT table entries into the internal table and list the concerts with attendance
over 18,000 (note: the internal table should contain all the concert entries, but the report should
be limited to those concerts with attendance over 18,000). If no concerts meet this criteria, an
appropriate message should be displayed. This list should be sorted by concert attendance in
descending order. See the sample output to determine the contents and layout of the report.
Do not maintain any text elements.
Page 16
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 7, Exercise 3
Program Name:YAPxx07_3
The purpose of this exercise is for you to become familiar with internal table processing.
You should copy the previous program (YAPxx07_2) to create this new program (YAPxx07_3).
Change the program to use an internal table without a header line.
The output is identical to the previous exercise.
Page 17
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 8, Exercise 1
Program Name: YAPxx08_1 (where xx is the last two digits of your logon id)
The purpose of this exercise is for you to become familiar with passing parameters to
subroutines.
You are to create a program that generates a report with Academy Award information (from the
YMOVIE table) for a particular year entered as a parameter. The main processing of this
program should be in two subroutines.
The first subroutine should be passed the year entered as a parameter. This parameter should
be passed by value. In this subroutine, all records for the specified year should be selected. If
no records exist for the year, an appropriate message should be displayed. For each record
selected, a second subroutine should write out the records information.
The second subroutine should be passed the field string (structure) that contains the record
selected. The year, category, and winner should be written to the report in this subroutine. See
the sample output to determine the layout of the report. Do not maintain any text elements.
Page 18
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 8, Exercise 2
Program Name: YAPxx08_2 (where xx is the last two digits of your logon id)
The purpose of this exercise is for you to become familiar with using function modules.
You are to create a program that generates a report with concert information (from the
YCONCERT table). This report should contain the artist, date, attendance (seats occupied),
price of ticket, and total sales. See the sample output to determine the layout of the report.
Use the Y_SALES_XX function module to calculate each concerts total sales. Before coding
this program, you should examine the interface of this function module to determine the
parameters being passed to/from the function module. Also, you should check out the
exception(s) that could be raised in the function module.
Remember to use the menu path Edit > Insert statement (or the Pattern pushbutton) to code
the CALL FUNCTION statement. You should never manually type in the CALL FUNCTION
statement.
You should only write an entry to the report if a total sales amount was calculated (i.e. no
exceptions were raised).
Do not maintain any text elements.
Page 19
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 20
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 8, Exercise 3
Program Name:
Function Group:
Function Module
The purpose of this exercise is for you to become familiar with creating function modules.
You should copy the previous program (YAPxx08_2) to create this new program (YAPxx08_3).
Change the program to use your own function module, Y_SALES_xx (where xx is the last two
digits of your logon id).
Before creating the new function module, you must create your own function group called YFxx
(where xx is the last two digits of your logon id).
The function module you create should calculate the total sales for a concert. This total sales is
based on an attendance number (seats occupied) and a ticket price. Before calculating the total
sales, this function module should check to see that the concert has already taken place (i.e. the
concert date is before the current date). If not, an exception should be raised.
The output is identical to the previous exercise.
Page 21
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Chapter 9, Exercise 1
Program Name: YAPxx09_1 (where xx is the last two digits of your logon id)
The purpose of this exercise is for you to become familiar with selecting a single record.
You are to create a program that generates a report with Academy Award information (from the
YMOVIE table) for a specific year and category. The year and category should be entered as
parameters. Define the year parameter LIKE YMOVIE-AAYEAR and the category parameter
LIKE YMOVIE-CATEGORY.
The report should display the year, category, winner, and nominees for the specified year and
category. If an entry does not exist, an appropriate message should be displayed. The report
will contain information in the following YMOVIE fields: AAYEAR, CATEGORY, WINNER,
NAME1, NAME2, NAME3, NAME4, NAME5. See the sample output to determine the contents
and layout of the report. Do not maintain any text elements.
Page 22
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Remember that the actual information you will see on your report depends on the data in the
YCONCERT table at the time you execute your program. If your report looks different than the
above screen print, execute the solution program to verify the actual contents of the table. This
should be done for any report accessing information from a database table.
Page 23
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 24
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Exercise 3
Program Name: YAPxx10_3 (where xx is the last two digits of your logon id)
Understanding the concept of inner and outer joins will be an important part of your ABAP
training. Projects will generally discourage the use of nested selects and/or logical databases.
For all the exercises that follow, you should use joins wherever possible.
A) Using an inner join, write a report that gives the following information about sales documents
created by Thompson":
Sales document #
Date of Creation
Creator
Description
Item Category
B) Comment out your inner join statement. Using an outer join, write a report that returns the
same fields.
Questions:
Which table is the driving table, and why?
Does the OUTER JOIN return a different list? Why or why not?
Page 25
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Exercise 4
Program Name:YAPxx10_4
Exersise 5
Program Name:YAPxx10_5
Page 26
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
This exercise will familiarize you with the concept of subqueries. This is a very brief exercise,
and should only take you a few minutes.
Using a sub-query, find all the records in the SCARR table, that do not exist in SFLIGHT. Write
out the name of the airline carrier, and their carrier Ids.
You report will return 17 records.
Question:
How is your subquery related to the inner join found in chapter 11?
Page 27
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
The purpose of this exercise is for you to become familiar with using authority checks.
You are to create a program that checks the activities allowed (e.g. create, change, display, and
delete) on vendor master records for the user executing the program. The activity should be
entered as a parameter. This parameter should be a length 2 numeric character field.
Some of the valid activities on the vendor master records are:
01 - create
02 - change
03 - display
06 - mark for deletion
The appropriate authority object for this authorization check is F_LFA1_APP. This authority
object has two fields:
ACTVT (activity)
APPKZ (application)
You should suppress the check for the APPKZ field. The check for the ACTVT field should be
on the activity parameter.
To code the AUTHORITY-CHECK OBJECT statement, use the menu path Edit > Insert
statement (or the Pattern pushbutton). You need to enter the name of the authority object in
the Authority-Check field and the system will automatically code the appropriate AUTHORITYCHECK OBJECT statement. By using this method, you cannot make a mistake typing in the
proper authority object name and field names.
The report generated by this program is simple. The line written to the report will indicate
whether the authority for the selected activity was passed or failed. See the sample output for
the content and layout of the report in both scenarios.
Although the processing logic of this program is not complex, imagine this authority check being
used in a program that is maintaining vendor master records. Without the authority check, any
user executing the program may be able to change or delete records.
Pass Authority Check:
Page 28
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 29
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 30
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Top of Report:
End of Report:
Page 31
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 32
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 33
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 34
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Top of Report:
End of Report:
Page 35
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 36
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 37
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Limit the total number of vendors displayed on the report by specifying the range of vendors
from VEND040 to VEND050 on the vendor account of the selection screen.
The following fields from LFA1, LFB1, and BSIK should appear on the report:
LFA1-LIFNR
(vendor number)
LFA1-NAME1 (vendor name 1)
LFB1-BUKRS (company code)
BSIK-GJAHR (fiscal year)
BSIK-BELNR (accounting document number)
BSIK-DMBTR (amount in local currency)
Do not maintain any text elements.
Top of Report:
Page 38
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
End of Report:
Page 39
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 40
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 41
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Page 42
Dec-2008
ECC 6.0
Exercises: Introduction to
ABAP Programming
Exercise 2
Program Name: YAPxx16_2 (where xx is the last two digits of your logon id)
Copy the program YXX16_2 to your own package. Change the XX to your ABAP number.
Correct the code so that it compiles. Then use the Debugger to correct any other errors.
Exercise 3
Program Name: Yxx16_3 (where xx is the last two digits of your logon id)
Copy the function module Y_SALES_DEBUG_XX (can be found at the very bottom of the
objects in package Y170) to your own function group that you created in the exercises for chapter
7. Change the XX to your ABAP number. Copy the program YXX16_3 to your own package.
Change the XX to your ABAP number. In the program, change the function call to the function
module you just copied. Correct the code so that it compiles. Then use the Debugger to correct
any other errors.
Page 43
Dec-2008