Vous êtes sur la page 1sur 10

ABAP: Tables and Data

Prepared by J Kreie
New Mexico State University
ABAP Tables and Data
This lesson requires that you first complete the lesson ABAP Tables, Domains, and Data Elements. The
program you create here will use the tables created in the previous lesson.
Add/View Data in Tables
Open the Object Navigator. Create a new program named Z_$$$$_students without the TOP INCL. For the
program title enter: $$$$ Student Information. Assign it to your package and transport request.
Below are screen shots of the code with comments between the screen shots. Comments are also embedded in
the ABAP program. You dont have to type them but it is recommended. It will help you read and understand the
program if you refer to it later on.
Selection-screen
Youll create three selection screens in this program:

1st: Get the user input for what they want to doadd data to a table or view data in a table.
2nd: Get user input for data to add to the EDULEVEL table.
3rd: Get user input for data to add to the STUDENT table.

You can create input screens in ABAP using the selection-screen statement. A selection-screen is given a
number such as the 110 shown below. As you can see this is a block of code that starts with begin and ends
with the end statement. Within a selection-screen there can be one or more blocks (different sections on the
screen). All the examples here have one block.
The title of the screen frame is given a text symbol (text-001, in this case). You fill in the actual text for that later
on.
The parameters
here use radio
buttons in a
group.
Assigning them
all to a group
ensures that
only one choice
can be made.
Also, note that a
default choice is
set.

Page 1 of 10

ABAP: Tables and Data


Prepared by J Kreie
New Mexico State University
The second selectionscreen gets user input if
they want to add a
record to the
EDULEVEL table. The
use of the
OBLIGATORY setting
makes sure the user
enters data for that
parameter. The
program will not proceed
until the required data is
entered. This is a very
handy feature for the
developer. It reduces
the amount of error-checking you have to do. Note that the parameters are defined by referencing fields in the
EDULEVEL table. This ensures data type capability.
This selection-screen sets up
parameters for student data.
The points made about the
previous selection-screen also
apply here. Some parameters
are required (obligatory) and
each parameter takes on the
definition of the field it
corresponds to in the table.

The main program calls the first


selection-screen then it evaluates
which choice the user made by using
an IF statement. The appropriate
subroutine (procedure or FORM) is
called based on the users selection.
PERFORM is the ABAP statement to
call subroutine.

Page 2 of 10

ABAP: Tables and Data


Prepared by J Kreie
New Mexico State University
The remainder of the code is comprised of subroutines. Each subroutine has a comment header to identify its
beginning. The subroutine code starts with FORM and terminates with ENDFORM. Dont type the comment
header because it is created automatically when you use Pretty Printer to format your code.
The first subroutine sets up a structure and internal table for processing data in the EDULEVEL transparent
table. The data is read from the transparent table into the internal table (a table in RAM that exists only during
program execution). Data in the internal table is moved row by row into a structure from which the WRITE
statement can display each field in the row.
Data is read from the
transparent table using
embedded SQL SQL
embedded in any
programming language
is referred to as
embedded SQL.
The SELECT
statement in this
subroutine retrieves all
records from the
external table and puts
the data in the internal
table. Later on youll
use select statements
that set criteria and
limit the data retrieved
from the transparent
table.

Page 3 of 10

ABAP: Tables and Data


Prepared by J Kreie
New Mexico State University
This subroutine retrieves and displays records from the STUDENT table.

Page 4 of 10

ABAP: Tables and Data


Prepared by J Kreie
New Mexico State University
The following subroutine calls the second selection-screen to get user input for adding a record to the
EDULEVEL table. Data for both parameters are required (obligatory).
Notice the INSERT statement that moves data from the programs structure into the transparent table.

Page 5 of 10

ABAP: Tables and Data


Prepared by J Kreie
New Mexico State University
This subroutine calls the third selection-screen to get data for a new student record.

This subroutine displays a message if the education level or student tables have no data.

Save and check your program. Youll probably need to do some debugging.
Save your changes, check again, and activate your program.

Page 6 of 10

ABAP: Tables and Data


Prepared by J Kreie
New Mexico State University
Test your program. The initial screen is a little hard to decipher but youll clear that up next.
Initial selection screen:

P_A_LVL option (Add row

to education level):

Add some data:


FR Freshman

P_A_STDT (Add a student):


Add a student.
Notice that you
have a list to
choose from for the
major and the
education level.

So, you can see that the text on the screens is based on parameters names. Use the text elements/selection
texts to clean up the initial input screen so regular English is shown instead of cryptic parameter names. Below
is example text for the screen text symbols and selection text. Remember to activate the symbols and text. Try
running your program again. Its easier to understand!

Page 7 of 10

ABAP: Tables and Data


Prepared by J Kreie
New Mexico State University
Of course, you can make this program multilingual by added translations for the selection texts and text symbols.
You can do this on your own if you want to practice. Translations are not provided here.

Database concepts: Domain value range versus reference table


When you run this program, notice that the p_major parameter has a value search with a list of options. The list
of options was created when a range of values was entered in the domain for the data element that the MAJOR
field is based on. This example shows another powerful feature of ABAP. A value search is generated
automatically if the metadata supports it. However, from a general database perspective, the value range
defined for the domain in this lesson would not be practical in the real world. Why? It is very likely that majors
will change over time; certainly some would be added. Defining the values in the domain makes it difficult to
maintain this data in a real-time, dynamic way. It would be better to have a reference table (check table in SAP
parlance) that can be maintained by certain users. To change the value list in the domain would require
intervention by a developer. To change the list in a reference tableadd a new major, for examplewould only
require data entry from an authorized end-user. A value range in a domain makes sense if the list is extremely
stable, i.e. it is not expected to change.
A few comments about creating tables in SAP
It is unlikely that youll create many, if any, tables in SAP using ABAP Workbench. In future lessons youll work
with SAPs tables that support the many business modules. The point of creating the tables in the previous
lesson and working with them here is to show how tables are constructed in SAP and how database concepts
such as domains can be applied in a real relational database environment.
It is also important to understand the distinction between SAPs transparent tables and the internal tables you
will often use in your programs. Internal tables exist in RAM only during program execution. Once the data from
a transparent table(s) has been loaded into an internal table, the access and processing of the data is much,
much faster than working directly with transparent tables. As a programmer and power SQL user, you should
use internal tables but you should think about how to populate them efficiently. In other words, dont pull every
record from a transparent table into an internal table (select * from mara, for example) if it makes sense to limit
the records through certain criteria, such as country code, customer ID, date, etc.

Practice
Create a program that allows you to add data to or list data from the tables you created in the practice section
of the previous lesson.

Page 8 of 10

ABAP: Tables and Data


Prepared by J Kreie
New Mexico State University

Terms to Know
check table............................................................................................................................................................. 4
embedded SQL...................................................................................................................................................... 3
external table.......................................................................................................................................................... 3
FORM..................................................................................................................................................................... 2
insert statement...................................................................................................................................................... 4
internal table........................................................................................................................................................... 3
PERFORM.............................................................................................................................................................. 2
reference table........................................................................................................................................................ 4
select statement..................................................................................................................................................... 3
selection-screen..................................................................................................................................................... 1
value search........................................................................................................................................................... 4

Page 9 of 10

References
ABAP Basics. Gnther Frber and Julia Kirchner, SAP Press - Galileo Press, 2007.
Discover ABAP. Karl-Heins Khnhauser, SAP Press, 2007.
ABAP Objects: An Introduction to Programming SAP Applications. Horst Keller and Sascha Kruger, SAP Press,
2002.

Vous aimerez peut-être aussi