Anda di halaman 1dari 10

ABAP Tables and Structures

Prepared by J Kreie
New Mexico State University
ABAP Tables and Structures
This lesson and others assume you are comfortable with relational database concepts and terminology.
Explanations for these terms and concepts are not covered. (If you are not familiar with relational database
concepts you should get book about database management or systems analysis and design and read the
chapter on data modeling.)
In this lesson you will learn about structures, internal tables, and external tables. You will create a program that
reads data from a table and displays it in an output screen.
Tables and Structures
Since youre familiar with SAP you understand that a lot of data support the SAP modules (Financial Accounting,
Logistics, etc.) and these modules were written in ABAP. From the outset ABAP offered features to facilitate
processing huge amounts of data.
External tables are tables in the database that support the SAP modules. For example, some customer data
are kept in a table called KNA1 and sales order data are kept in VBAK (header data) and VBAP (sales order
item data). When we talk about tables in ABAP, an external table is maintained in the SAP database, it used
indirectly through an internal table defined in an ABAP program, which is described in the next paragraph. It is
possible to manipulate data directly in external tables with SQL but that is not recommended. Typically, data is
retrieved from an external table and stored temporarily in an internal table, manipulated within an ABAP program
then written back to the external table, if changes to the data need to be kept. Transparent is another term
SAP uses for standard relational tables in the SAP database. Youll see this term again near the end of this
lesson. There are other types of tables within SAP which we will not use in these lessons.
When you work with table data in ABAP, typically, you will use structures/work areas and internal tables. In your
program you define a structure also called a work area variables that are grouped together and each variable
is like a column or field in a table. The combined variables in the work area are like a single row to hold data.
An internal table is defined by referencing the structure (group of variables) that makes up the columns in this
temporary table. The data in an internal table exists only during program execution. During execution the
internal table must be populated (data retrieved from an external table or input by a user) and the program must
take care of storing any data that should exist after the programs end, i.e. transferring the data from the internal
table to an external table.
Work area

One row only

Internal table

More than one row.

Just like a real table except that it only exits in RAM during program execution.

Page 1 of 10

ABAP Tables and Structures


Prepared by J Kreie
New Mexico State University

Page 2 of 10

ABAP Tables and Structures


Prepared by J Kreie
New Mexico State University
Flight data
Before you begin another program in which we use structures and internal tables, you should know that SAP has
a small set of tables that are often used for ABAP training. These tables are based on an airline/flight scenario
(help.sap.com flight model). These tables do not have anything to do with the tables that support SAPs ERP
modules. You will work with the flight tables some in these ABAP lessons but you will also work with some tables
that hold the kind of data you see in other courses that use SAP. Here are some of the flight tables:
Table Name
SCUSTOM
STRAVELAG
SBUSPART
SCARR
SPFLI
SFLIGHT
SBOOK

Description
Customers
Travel Agencies
Business Partners
Airline Carriers
Flight Schedule
Flights
Flight bookings

Data Modeler (SD11) View a Data Model


Using SAP Data Modeler tool in the ABAP
Workbench, you can see the flight data model.
With the ABAP Workbench folder expanded,
select Development Data Modeler or run
transaction code SD11. In the initial screen,
select the value search feature by the Data
Model box.

In the search dialog box enter *bc_* as the search string and
click the checkmark.

Select BC_Travel from the list and click enter.

Page 3 of 10

ABAP Tables and Structures


Prepared by J Kreie
New Mexico State University

In the Data Modeler: Initial Screen click the Display


button. In the next screen, which gives a definition of the
flight reservation data model, click the Graphic button
.
If prompted, click Run or OK to run the modeler program.
Only a small portion of the model is shown here. It is
lengthy. If you select some of the other BC_* models, they
will be simpler because they represent subsets of the flight
scenario.
You can close the model and return to the ABAP
Workbench menu.

Create a program that processes data in tables.


Open the Object Navigator. Create a program called z_$$$$_trvl_ag without the top include. The title is: Travel
Agencies in a Country. Assign this program to your lesson package and transport request (dont assign them to
your practice package and request if you created these in the PRACTICE section at the end of the previous
lesson).
The program you create here will prompt the
user for a 2 of 3 letter country code (FR =
France, IT = Italy, etc.) then retrieve the travel
agencies in that country. The program is
presented in screen shot segments along with
explanations of the coding.
Parameters, types, and data. First, you set up
what you need for capturing and holding data in
your program input by the user. The
PARAMETERS section defines a parameter for
storing a country code.
The TYPES section defines the data structures
used in the program. This section will define a
line type (grouping of defined variables) and a
table type based on the line type. (If you look at
example ABAP code in some books and online
you will likely see some different coding
examples but this is the recommended
programming style.) The line type is this
program defines each data type by referencing
an existing field/column in a standard table
(stravelag). The line type definition has
definitions for agency number, agency name, city and country. The table type is defined by referencing the line
type,
The data types for these variables could have been defined with basic data types, such as c for character, but
instead each one references a field in an external table. The variable takes on the data type of its like field.
The advantage of this is that the programmer doesnt have to know exactly what the data type or field size is
Page 4 of 10

ABAP Tables and Structures


Prepared by J Kreie
New Mexico State University
when writing the program. Notice the syntax for creating the types. It starts with BEGIN OF <structure name>
and ends with END OF<structure name>. The variable definitions are separated by commas, not periods.
The DATA section defines the two things we need to work with the data we will import from the travel agency
table. We define a work area that is based on the line type we created before. This work area will hold one row
of data for the four fields or variables. After defining the work area we also define an internal table that is based
on the table type. This internal table will hold as many rows of data as we want to put in the table.
Why do we need both a work area and an internal table? It is a characteristic of ABAP that we cannot work
directly with data in the internal table. We move a row of data from the internal table into the work area where
we can manipulate the dataedit it, display it, etc. Commands such as LOOP in ABAP will systematically move
through the rows of an internal table, putting one row at a time in the corresponding work area.
The next code segment evaluates what
the user entered for the country code
then branches to the appropriate SQL
statement which retrieves data from
STRAVELAG and puts it in the internal
table. The SQL should look familiar but
the syntax may not be exactly like what
you have used before.

Even though youre not finished save, check


and activate your program.
The last code segment writes text to an output
screen. First, there is a chained WRITE
statement with a newline forward slash, a text
symbol (TEXT-001), a colon space encased in
single quotes, the parameter p_cntry and,
finally, another newline forward slash.
Next is a LOOP statement that steps through
each row of the internal table. In each pass a
row from the internal table is moved to the
work area then that data is displayed with the
WRITE statement.

Page 5 of 10

ABAP Tables and Structures


Prepared by J Kreie
New Mexico State University
Save, check and activate your program.
Just as you did in the previous lesson, you need to enter some
text for the TEXT-001 text symbol. Use the menu bar path: Goto
text elements text symbols.
Click the Selection Text tab and add regular text for the parameter
p_cntry like the example shown here.
Be sure to activate the text symbol and selection text.
Test your program. Try FR, GB, or DE for the country code and try leaving it blank. You should have output
similar to the example shown here.
Example input/output

It would be nice not to have to guess the country code, wouldnt it? Make a small change to your program, as
shown here.

Save, activate, and test your program. Youll see the value search list option to the right of the input box. As it
turns out, this list box isnt all that useful in this program using the flight/airline data because there are very few
countries in this sample data. Many country codes you might try will not have any data to display for travel
agencies. However, this code modification shows how you can use data defined in the ABAP Data Dictionary to
provide search lists for input.
To finish off this program, create Spanish translations for your
program based on the examples shown below. (Goto
Translations .)
Remember, you dont have to worry about the message that
there is still text to be processed.

Page 6 of 10

ABAP Tables and Structures


Prepared by J Kreie
New Mexico State University
Here is example output from this program when Spanish
(ES) is specified as the language when the user logs in
to SAP.

Finally, create a transaction code for your program. Use the naming convention but, otherwise, you decide the
name. Add it to your Favorites.
ABAP Dictionary
The ABAP Dictionary is the interface to the metadata in SAP. Metadata is data about the data which means it
is information about how data is stored in the databases. Example metadata about a table would be its name, its
columns (each name, data type, field size, etc.), which column(s) is the primary key, which column(s) is a foreign
key. You can view existing transparent tables in the ABAP Dictionary.
The ABAP Dictionary is in the path: ABAP Workbench
Development ABAP Dictionary (SE11). Open this
transaction.
Click the value search for Database table then type
strav* as the search string.
This will bring up the STRAVELAG table we just worked
with. Accept that item then click the Display button back
in the Initial Screen.

Page 7 of 10

ABAP Tables and Structures


Prepared by J Kreie
New Mexico State University
You see a detailed list of the fields in
the STRAVELAG table, including their
data type, length, and short
description. In the next lesson you find
out what data elements are. The two
key fieldsMANDT and
AGENCYNUMare the primary key
for this table.
Click on the table name STRAVELAG
then click the where-used icon
.
In the next dialog box, uncheck
everything but programs then click the
checkmark. Accept the option shown
next and continue.

As you can imagine, the where-used


feature is a very powerful tool that helps
developers keep track of where data are
used in SAP modules and in customized
programs, such as the ones you are
creating in these lessons.

Return to the Dictionary: Display Table screen.


The text to the left of STRAVELAG is Transp. Table which is an abbreviation for transparent table. A
transparent table is what we think of a database tablea two-dimensional set of columns and rows. There are
other data structures in SAP but we do not cover them here.
Youll find the ABA Dictionary useful but even handier is the forward navigation feature in the ABAP Workbench.
To try forward navigation, return to the Object Navigator and open your most recent program.
Double-click on the STRAVELAG name in one of the lines of code. This
will take you to the table information in the Data Dictionary. With forward
navigation, you can double-click tables, function names, includes, etc.
and move directly to the information about that program element. Very
useful!

Practice
Create a program that displays customers in the travel database. Assign this program to your other package for
practice after a lesson. In this program get input for what country then show only customers in that country.
Show the customers name, ID, type, city and country. Make the program screens multilingual.

Page 8 of 10

ABAP Tables and Structures


Prepared by J Kreie
New Mexico State University

Terms to Know
ABAP Dictionary..................................................................................................................................................... 4
airline/flight scenario............................................................................................................................................... 1
external table.......................................................................................................................................................... 1
forward navigation.................................................................................................................................................. 5
internal table........................................................................................................................................................... 1
metadata................................................................................................................................................................. 4
structures................................................................................................................................................................ 1
transparent table..................................................................................................................................................... 5

Page 9 of 10

Anda mungkin juga menyukai