In this article I will discuss the various aspects of ABAP programming. I will try and
come out with subequent articles to add to the content in this section. If you have any thoughts or comments please put them
in the comments section below.
Data Dictionary
A data dictionary is a centralized storage location for
information about the data that is stored in a database. This information is often called metadata (data about
data).
A data dictionary provides answers to questions such as:
- What data is contained in the database?
- What are the attributes of this data: name, length, format, etc.?
- What relationships exist among
different data objects?
SAP’s data dictionary is called the ABAP Dictionary. As an ABAP
developer, it is unlikely that you will be responsible for creating and maintaining objects within the ABAP Dictionary.
The ABAP Dictionary:
- Enforces data integrity
- Manages data definitions
without redundancy
- Is tightly integrated with the rest of the ABAP Workbench
The ABAP Workbench
supports the development and modification of client/server applications written in ABAP. It is the integrated graphical
development environment of the R/3 System.
The ABAP Workbench can be used to:
- Write ABAP code
- Design screens
- Create user interfaces
- Test applications for efficiency
- Check applications for errors
- Use predefined functions
- Access development objects
- Access database information
Enforcing data integrity is the process of ensuring that data
entered into the system is logical, complete, and consistent.
When data integrity rules are defined in the ABAP
Dictionary, the system automatically prevents the entry of invalid data.
Defining the data integrity rules at the
dictionary level means they only have to be defined once, rather than in each program that accesses that data.
The
following are examples of data lacking integrity:
- A date field with a month value of 13
- An order
assigned to a customer number that doesn’t exist
- An order not assigned to a customer
Managing
data definitions without redundancy is the process of linking similar information to the same data definition. For example,
a customer database is likely to contain a customer’s ID number in several places. The ABAP Dictionary provides the
capability of defining the characteristics of a customer ID number in only one place. That central definition then can be
used for each instance of a customer ID number.
The ABAP Dictionary’s integration with the rest of the development
environment enables ABAP programs to automatically recognize the names and characteristics of dictionary objects.
Additionally, the system provides easy navigation between development objects and dictionary definitions. For example, as a
programmer you can double-click on the name of a dictionary object in your program code and the system will take you directly
to the definition of that object in the ABAP Dictionary.
When a dictionary object is changed, a program that
references the changed object will automatically reference the new version the next time the program runs. Because ABAP is
interpreted, it is not necessary to recompile programs that reference changed dictionary objects.
The basic objects of
the ABAP Dictionary are tables, data elements, and domains. Tables are the structures that actually store the information
that is in the database (i.e., customer information). Tables are composed of rows (called records) and columns (called
fields).
Data elements and domains are used to describe the characteristics of fields.
The database utility is
a program that provides the interface between the ABAP Dictionary and the underlying database management system (DBMS). It
allows you to create ABAP-defined objects such as tables and database views physically in the database. It can be executed in
foreground and background.
Whenever you make a change to a dictionary object that affects the underlying database,
the database utility is activated. Usually it works silently behind the scenes, but occasionally the database utility will
prompt you for information. This occurs when an error is encountered or when existing data must be converted.
The
database utility provides the interface to the DBMS by automatically generating the Data Definition Language (DDL) that the
DBMS understands.
There are several path to accessing the dictionary. Below is a list some of the ways to access the
Dictionary.
To access the ABAP Dictionary:
Use the menu path:
Tools > ABAP Workbench >
Development > ABAP Dictionary
Follow the menu path:
Tools > ABAP Workbench > Overview > Object
Navigator
Pull down the Workbench menu and select Other objects… Select the Dictionary tab from the pop up screen
and enter the appropriate table name in the Database table field.
Use transaction code
SE11
The basic objects of the ABAP Dictionary are tables, data elements, and domains. These form
the active part of the ABAP Dictionary and contain all the field-related metadata of the R/3 System. A field is not a
dictionary object, but rather is a component of a table. A field cannot exist without a table and only has meaning within
that particular table.
Data elements and domains are dictionary objects. Therefore, they can be used by many tables.
They specify the characteristics of fields. Data element and domain names are limited to 30 characters. Table names are
limited to 16 characters in length.
Tables are the objects that actually hold the information in a database. They
consist of rows (records) and columns (fields).
For example, table KNA1 stores information about customers. Some of
the columns in KNA1 are KUNNR (customer ID number), NAME1 (customer name), and ORT01 (customer city). Each row in KNA1 stores
this information for a different customer.
Certain fields in a table are specified as the primary key of that table. The
primary key is that field or combination of fields that uniquely identifies a row in the table. In table KNA1, the SAP R/3
client (MANDT) and the customer number (KUNNR) form the primary key.
Custom tables must start with a Y or a
Z and can be only 16 characters in length.
The Object Navigator is used to organize your
programming in an integrated development environment. It can be used to create, change, and manage objects. It is the center
point of entry into the ABAP Workbench.
You can get to the Object Navigator by way of transaction
SE80 or the menu path:
Tools > ABAP Workbench > Overview > Object
Navigator
Creating a Table
Now, let’s take a look at creating a table in the ABAP
Dictionary. We will create a table called ZXXFIRSTTABLE (where XX stands for the last two digits of your logon id). This
table will hold information about your ABAP class.
Follow the menu path:
Tools > ABAP Workbench
> Overview > Object Navigator or Transaction Code SE80.
Pull down the Workbench menu and select Other objects…
Select the Dictionary tab from the pop up screen and enter the appropriate table name in the Database table field. Click the
Create button (F5).
Remember, custom table names must begin with a Y or Z and are restricted to 16
characters.
Enter a description (short text) and a delivery class. If the table is to be maintained via SAP R/3-
provided table maintenance screens, click on the Table Maintenance Allowed checkbox.
Click the Fields tab to begin
adding Fields to the new table.
Defining the Table FIelds
Table fields are defined in SAP R/3
by data elements and domains. This is a key difference between SAP R/3 and many other systems. In other systems, a field is
defined only by its type (i.e., character, numeric) and length. These characteristics are assigned directly to each
field.
In SAP R/3, a field is defined by being assigned to a data element, which is in turn assigned to a domain.
These assignments determine the field’s type and length in addition to other characteristics.
SAP R/3 refers to this
method of field definition as the Two-Level Domain Concept.
In addition, you can assign the field type (data
type) characteristics in different ways:
- you can directly assign the field a data type, length, and short text
when defining the table.
- You can assign a data element to a field so that the field will inherit the data
element’s characteristics.
Data Elements
The meaning of a field name is not always
obvious. For example, KUNNR may not immediately bring the image of a customer to your mind. Data elements help resolve this
problem.
A data element provides a meaningful description for a field. You will hear it called a semantic domain. The
data element description appears beside a field in a table definition. These descriptions are language-dependent (unlike
field names).
Even more importantly, the data element provides field headings for use on screens. When you paint a
dictionary field on a screen, you can automatically have the data element field headings appear. This permits the end user to
see meaningful field descriptions.
The advantage of using a data element to describe fields and provide field headers
is that the data element can be used more than once.
For example, SAP R/3 contains many tables that have the field
KUNNR. In cases like this, it is not necessary to enter the description for KUNNR many times. Each instance
of KUNNR can be assigned to the same data element, and the field description only needs to be specified once. Additionally,
if the description needs to be changed, it must be changed only once, and all fields referring to that data element
automatically use the new description.
Pre-defined Data Elements
SAP R/3 comes delivered with
many pre-defined data elements. Whenever the semantic description of a field you are creating matches with an existing SAP
R/3- supplied data element, use the one that SAP R/3 provides. Otherwise, you must create your own data element. When using
an SAP R/3-supplied data element, you must also use the corresponding SAP R/3-supplied domain. If you wish to use a different
domain, you must create your own data element.
Creating a Data Element
Now, let’s take a
look at creating a data element in the ABAP Dictionary. We will create a data element called ZXXFIRSTNAME (where XX stands
for the last two digits of your logon id). This data element will be used in the table ZXXFIRSTTABLE which we just created.
To create a data element within the ABAP Dictionary use transaction SE80 or the menu path:
Tools > ABAP
Workbench > Overview > Object Navigator
- Pull down the Workbench menu and select Other objects… Select the
Dictionary tab from the pop up screen and enter the appropriate Data Element name in the Data Element field. Click the Create
button (F5).
- Enter a description (short text) and a Domain. Enter ZXXNAME as the Domain (where XX stands for the
last two digits of your logon id). We will create this Domain later in the chapter. Click the icon
(Ctrl+S).
- Type in your development class, YXXA (where XX stands for the last two digits of your logon
id).
- Next, click the Field Label tab.
- Provide a descriptive text for each of the following labels;
Short, Medium, Long, and Heading. These labels are used as templates in the Screen Painter as a choice between the different
label lengths.
- The Heading field is used as the column heading for tabular record entries and its length should
correspond with the length of the field.
- Click the Save icon (Ctrl+S).
Data Element
Documentation
In addition to providing field descriptions and headers, data elements can provide detailed
field documentation. This documentation is maintained at the data element level and is available to a user who presses F1
when the cursor is in a field assigned to the data element.
To maintain this documentation, navigate to the
Change Data Element screen within the ABAP Dictionary and press the Documentation push-
button.
Domains
A domain specifies the technical characteristics and the allowed values of a
field.
Domains are linked to fields via data elements. In other words, fields are assigned to data elements, and data
elements are assigned to domains. You cannot directly assign fields to domains.
The technical characteristics of a
field are the field’s type and length. These are also referred to as the field’s format. The allowed values of a field
define what is acceptable input for that field. These may be listed explicitly or may be specified by reference to a value
table.
Valid values for a domain can be explicitly listed. To create or maintain an explicit list of valid values,
click the Value range tab on the Change Domain screen in the ABAP Dictionary.
Explicit values are used when the list
of valid values is not likely to change over time. For example, if a domain were meant to contain the names of the months of
the year, it would be possible to explicitly list JANUARY through DECEMBER as the allowed values (note that this would not
work well in a multi-language environment).
Valid values for a domain can also be specified by reference to a value
table. When this is done, the records in the value table specify the valid values for the domain. In order to specify a value
table for a domain, one of the key fields of that table must point to that domain. That field is the one that determines the
valid values for the domain. A value table is used when the list of valid values may change over time.
For example, a
car maker may want to use a value table to list the valid colors for certain car models. As new colors are developed, the
value table could be updated without changing the domain itself.
By defining valid values for a domain, automatic
validation is provided for any fields that point to that domain. This includes validation at the ABAP Dictionary level (if a
program tries to insert invalid data an error will occur) as well as at the screen level (users will not be permitted to
enter invalid data).
Also, users can obtain a list of possible entries (for fields pointing to domains with allowed
values) by pressing function key F4.
Creating a Domain
Now, let’s take a
look at creating a Domain in the ABAP Dictionary. We will create a Domain called ZXXNAME (where XX stands for the last two
digits of your logon id). This Domain will be attached to the Data Element ZXXFIRSTNAME which we just created.
To
create a data element within the ABAP Dictionary use transaction SE80 or the menu path:
Tools > ABAP Workbench >
Overview > Object Navigator
- Pull down the Workbench menu and select Other objects… Select the Dictionary tab
from the pop up screen and enter the appropriate Domain name in the Domain field. Enter ZXXNAME as the Domain name (where XX
stands for the last two digits of your logon id). Click the Create button (F5).
- Enter a description (short text),
a data type, and the appropriate number of characters for this Domain. Click the icon (Ctrl+S).
- Type in
your development class, YXXA (where XX stands for the last two digits of your logon id)
- A domain can define a
value range containing the valid values for fields that refer to that domain.
- Explicit values or a value table
can be defined for the Domain ZXXNAME.
- Click the Value range tab. Under both sections of this screen, we can
assign valid values to the Domain ZXXNAME. This means that SAP will only allow the values specified on this screen to be
entered into any field that references the Domain ZXXNAME.
- A Value Table can also be assigned to the Domain
ZXXNAME. If a Value Table is assigned to ZXXNAME, SAP will only allow the values contained within the Value Table to be
entered into any field that references the Domain ZXXNAME.
- Once valid values are assigned, click the icon
(Ctrl+F3).
- Navigate back to the Data Element ZXXFIRSTNAME. Click the icon (Ctrl+F3).
- Navigate
back to the Table ZXXFIRSTTABLE. We will now add a custom field to our table.
Add
Fields
- Now you are able to add custom fields to the table ZXXFIRSTTABLE.
- Enter
FIRSTNAME as the first field in the field list.
- Enter ZXXFIRSTNAME as FIRSTNAME’s Field Type (Data Element).
- Assigning the Data Element to the field FIRSTNAME means that the field FIRSTNAME will inherit the same data type
and characteristics that we previously assigned to its Data Element, ZXXFIRSTNAME. The field FIRSTNAME will be a Character
based field with a length of 30.
- Check the Primary Key checkbox next to the field FIRSTNAME. (This will be
discussed in later chapters)
- Continue to add relevant fields (such as LASTNAME, HOMETOWN, PWCOFFICE, etc.) to
this table. Remember to create your own Data Elements and Domains when SAP does not provide fields that meet your exact
specifications.
- Click the icon (Ctrl+F3) to activate the new table when you are
done.
Maintain Technical Settings
- Maintain the table’s Technical Settings
by entering a Data Class and a Size Category. Both of these fields will depend on what the table will be used for and how
much data you expect the table to hold. For this example, ZXXFIRSTTABLE will hold a small amount of Master
Data.
- Select APPL0 for the Data Class.
- Select 0 for the Size Category.
- Click the Activate
icon (Shift+F4) and then the Green Back Arrow button (F3).
- The table ZXXFIRSTTABLE should now be in an active
status and it should contain one field - FIRSTNAME.
How to Use a Table in ABAP
Program
The TABLES statement:
- Identifies the table to the ABAP program.
- Creates a work
area in memory to store records retrieved from the database (the work area only holds one record at a
time).
The SELECT statement retrieves records one at a time from the database and puts them into the work
area.
The WRITE statement writes data to the screen.
The SELECT-ENDSELECT syntax creates a looping structure. In
other words, all statements between SELECT and ENDSELECT are executed once for each record retrieved from the
database.
Developer Access Keys vs. Object Access Keys
The first time you try to create an
object in SAP, the system will respond with a dialog box that says, "You are not a registered developer." The dialog box will
prompt you for a 20-digit Developer Access Key. This is the access key that appears on the same sheet of paper where you
found your logon ID and password. The request for a developer access key will only appear once. After you enter the key
correctly, you will not be prompted for it again.
However, if you try to change an SAP-delivered object (or if you
forget to name your object beginning with a Y or a Z), you will be prompted for an object access key. This is because SAP
registers and controls all customer changes to SAP-delivered objects. If you see this dialog box during this course, you have
made a mistake; click the cancel button in the dialog box.
It is easy to confuse a request for a developer access key
with a request for an object access key. One way to remember the difference is to remember the phrase, "You are not a
registered developer." If the request contains this phrase, it is asking for a developer access key. If it does not contain
this phrase, it is asking for an object access key.
Foreign Keys
Relationships between tables
are represented in the ABAP Dictionary by Foreign Keys.
Foreign Key: A field (or combination of fields) that
represents the Primary Key of another table.
For example, if table YORDERS has a field CUSTID indicating
which customer placed the order, that field could be established as a Foreign Key (assuming that CUSTID was the primary key
of the YCUSTOMERS table). Note that CUSTID is not necessarily part of the primary key of the table YORDERS.
The table
that is referenced by the Foreign Key (in our example, YCUSTOMERS) is called the Check Table. The Check Table is also known
as the referenced or parent table. The table that contains the Foreign Key fields (in our example, YORDERS) is called the
Foreign Key Table. The Foreign Key Table is also known as the dependent or child table.
Foreign Keys are used
for:
- Maintaining data integrity
- Providing additional texts in the online help
system
- Creating other dictionary objects that are defined over multiple tables (such as views)
Maintaining data integrity is probably the most important reason to use Foreign Keys. With Foreign Keys, it
is possible to prevent values from being entered into one table that do not already exist in another table. For example, it
becomes impossible to enter orders that do not have valid customer numbers. In this way, Foreign Keys are similar to a
domain’s Value Table.
In order to establish a Foreign Key relationship in SAP between a field in Table A and a field
in Check Table B, both fields must have the same domain.
Sometimes when you define a domain, you already know that
all fields that use the domain will need to be checked against a particular table. You can store this information in a
Domain’s Value Table when the domain is created. If you try to define a Foreign Key for a field that points to a domain
with a Value Table, SAP will propose that Value Table as the Check Table for the Foreign Key Field.
Note:
Prior SAP versions required that Foreign Keys must have a Value Table
specified.
Foreign Keys: Terminology
Let’s review some of the key terminology relating to
foreign keys.
Value Table: The table containing the set of allowed values attached to a
domain.
Check Table: The table that is referenced by a Foreign Key. A Check Table is either identical to
a Value Table, or it is another table containing a subset of the records in a Value Table.
Foreign Key
Table: The Foreign Key Table is the table containing fields that are the primary key of the parent table. The
Foreign Key Table is also known as the dependent or child table.
Using Foreign Keys, you can:
- Create
value checks for input fields.
- Link several tables in a view or in a lock object.
To see a full
list of the allowed values for a screen field, place the cursor inside the field and press the F4 key.
Check
Table and Value Table
Suppose a Foreign Key relationship was established between an ORDERS table and a
PRODUCTS table to ensure that orders contained valid product numbers. In this case, it is likely that the Value Table and the
Check Table would both be the PRODUCTS table.
In most cases, the Value Table and the Check Table will be the same.
However, it is important to understand the distinction between the two.
The Value Table is optional in the foreign
key definition. Foreign Key definitions must always have a Check Table assigned.
Suppose a telephone company stores
general customer information in the CUSTOMERS table and stores specialized information about business customers in the
BUSINESS_CUSTOMERS table.
Suppose that there was also a BUSINESS_OWNERS table that listed the owners of all business
customers. The BUSINESS_OWNERS table might consist of two fields: Customer ID and the name of each individual owner.
In this case, we would establish a Foreign Key relationship between the BUSINESS_OWNERS table and the BUSINESS-
CUSTOMERS table.
The Value Table for the customer ID field in the BUSINESS_OWNERS table would be the CUSTOMERS table,
but the Check Table would be the BUSINESS_CUSTOMERS table.
The Foreign Key Table would be the BUSINESS_OWNERS
table.
Establishing Foreign Key in ABAP Dictionary
To establish a Foreign Key in the ABAP
Dictionary, begin by navigating to the field definition of the Foreign Key Table and entering change mode.
The Foreign
Key can then be established by placing the cursor on the Foreign Key Field, and then by pressing the
color="#FF0000">FILL IN
Based on the Value Table for the domain of the field you
selected, the system will suggest a Foreign Key Relationship for you.
Creating a Foreign Key
Relationship
Using the Object Navigator edit the Table ZXXFIRSTTABLE which we created in Chapter 2.
Place
the cursor on the field for which you wish to create a Foreign Key Relationship. In this example, we will add the Check Table
T005S as a Foreign Key for the STATE field so that users will be forced to enter a valid state into the Table
ZXXFIRSTTABLE.
Click the FILL IN icon to create the Foreign Key Relationship for
the STATE field.
Remember the criteria for establishing a Foreign Key Relationship: the Foreign Key Field and Check Table
Field must share the same domain.
Since the field STATE’s domain (REGIO) has a Value Table (T005S), SAP will propose
T005S as the Check Table for the field STATE when we click the Foreign Key pushbutton.
Click Yes when the Create
Foreign Key window appears.
Enter a description (short text).
Maintain the semantic attributes (see Data
Dictionary Appendix for information on setting up the Cardinality)
Cardinality n : m
- n = 1 or C
- m =
1, C, N, CN
Foreign Key Field Type (see Data Dictionary Appendix for information on setting up the Foreign
Key Field Type)
- Not Specified
- Non-Key Field Candidates
- Key Field / Key Field
Candidates
- Key Fields of a Text Table
Click the Copy button at the bottom of the
screen.
SAP Table Types
There are 5 different table types in the SAP ABAP
Dictionary:
- Transparent tables
- Structures
- Pool tables
- Cluster
tables
- Views
Data in the SAP R/3 system is stored in a relational database management system
(RDBMS). All tables in the SAP R/3 system are created using the ABAP Dictionary and are stored in the relational database.
Structures and views are also created in the ABAP Dictionary but are not stored in the RDBMS.
The ABAP Dictionary is
SAP’s tool for developers to maintain any type of table that exists in the physical database.
Transparent
Tables
SAP uses the term Master Data to refer to control tables and the traditional files that are necessary
to run a business such as personnel files, general ledger accounts, customer files, etc. There is a 1:1 correlation between
the master data tables in the Dictionary and the tables in the physical database. For each master data table in the
Dictionary, the same master data table exists in the physical database.
Commercial Data or Transaction Data is
another SAP term used to refer to data created as a result of performing SAP business transactions such as creating:
invoices, orders, production schedules, purchase requisitions, stock transfers, etc.
SAP stores both Master Data and
Transaction Data exclusively in Transparent tables (TRANSP).
Transparent (TRANSP) tables have a 1:1
correlation between the ABAP Dictionary and the physical database. For each TRANSP table in the dictionary, the same table
name exists in the physical database.
Since TRANSP tables exist in the physical database, you can use either Open SQL
or Native SQL to access them.
Transparent tables are created automatically in the database after you specify the table’s
technical settings and activate the table using the Database Utility. The Database Utility gets invoked automatically when
you activate the table by pressing the icon.
Mapping Table Type INTTAB
INTTAB tables
are field strings (structures) which do not hold data. Therefore, they are not mapped to the database and have no underlying
database tables.
INTTAB tables must be activated just like TRANSP tables. However, no technical settings are
required and the Database Utility is not automatically invoked when you press the FILL IN
icon since INTTAB tables do not exist in the database.
Structures can be used in multiple tables. They help avoid
redundant field definitions throughout the system. Structures can be nested up to nine levels and can contain a maximum of
one table. They are similar to the copybook function of other programming languages.
align="center">===============
them
ABAP Program Syntax
* Declaration of the program name
REPORT
CHAP0301.
* Displaying the words 'Customer list' on the screen
WRITE / 'Customer list'.
REPORT
ZRSYNTAX.
* Using an addition of the write command
WRITE AT /10 'Customer list'.
* Using single quotation
marks within the text of a literal
WRITE / 'Customer''s Name'.
* Here is a comment with an asterisk in the
first column
WRITE / 'Ms O''Connor'. "This is a comment at the end of the line
* A field of type character and
length 40
DATA TARGET_STRING(40) TYPE C.
* Statements may extend over several lines
* (e.g., copying fields
using the move command):
MOVE 'Source string'
TO
TARGET_STRING.
WRITE / TARGET_STRING.
* Combining
Statements
WRITE: / 'Customer list',
'Bookings'.
Numbers
*
Numbers
*
REPORT ZNUMBERS.
* Fields of type i (integer) are mainly used for counting
DATA: CUSTOMER_NUMBER
TYPE I,
LOOP_COUNTER TYPE I.
* Integers have a fixed length of 4 bytes.
* The initial value is zero, if it is
not specified explicitly.
DATA WORD_LENGTH TYPE I VALUE 17.
* Packed numbers (type p) are a way to store numbers
internally
* in a compressed form. Therefore, they cover a wide range of possible
* values can be used for all kinds of
computations.
DATA NUMBER_OF_MOSQUITOES TYPE P.
* Decimal handling is supported for packed numbers
DATA
AIRBAG_PRICE TYPE P DECIMALS 2 VALUE '333.22'.
WRITE / AIRBAG_PRICE.
* Default length of type p fields is 8, and
the maximum length is 16,
* which can represent numbers of up to 31 digits plus the sign
DATA: PACKED_NORMAL TYPE
P,
PACKED_16(16) TYPE P.
* Floating point numbers (type f) occur in complex arithmetic
* operations.
Possible values range from 1E~-307 to 1E307.
* The standard output length of fields of type f is 22.
DATA AGE_OF_EARTH
TYPE F VALUE '123E+8'.
WRITE / AGE_OF_EARTH.
* The values of floating point numbers can be represented in
*
different ways, but they are all equivalent:
DATA: F1 TYPE F,
F2 TYPE F,
F3 TYPE F.
F1 = 1.
F2 =
'-12.34567'.
F3 = '-765E04'.
Defining Data Types
*
REPORT REP0101.
1. Elementary
types
DATA CUSTOMER_NAME(25) TYPE C.
DATA: CUSTOMER_NAME_1(25) TYPE C,
VENDOR_NAME_1(25) TYPE
C.
*2.Non-elementary Type & Reference to a non-elementary type
* Non-elementary type
TYPES T_NAME(25)
TYPE C.
* Reference to a non-elementary type
TYPES T_NAME(25) TYPE C.
DATA: CUSTOMER_NAME_3 TYPE T_NAME,
VENDOR_NAME_3 TYPE T_NAME.
* Record
DATA: BEGIN OF BOOKING,
ID(4) TYPE C,
FLIGHT_DATE
TYPE D,
NAME LIKE CUSTOMER_NAME,
END OF BOOKING.
* Internal table
DATA BOOKING_TABLE LIKE
BOOKING OCCURS 100.
* 3.data object & Reference to an existing field
* Data Object
DATA VENDOR_NAME LIKE
CUSTOMER_NAME.
* Reference to an existing field
DATA: CUSTOMER_NAME_2(25) TYPE C,
VENDOR_NAME_2 LIKE
CUSTOMER_NAME_2.
Simple Examples how you use Data Object.
* Copying the content of one data object to
another
DATA: SOURCE(10) TYPE C,
TARGET LIKE SOURCE.
MOVE SOURCE TO TARGET.
* Displaying the
contents of fields
WRITE 'ABAP/4 is easy.'.
NEW-LINE.
WRITE 'This text is displayed on a new line.'.
WRITE /
'After the symbol /, text also appears on a new line.'.
* Standard control structures (conditions and loops)
IF
SOURCE = TARGET.
WRITE / 'Fields source and target have the same content'.
ELSE.
WRITE / 'Fields source and
target do not have the same content'.
ENDIF.
DO 3 TIMES.
WRITE / SY-INDEX.
ENDDO.
* Local
subroutine of a single program
DATA: A1 TYPE I,
A2 TYPE I.
PERFORM CALC USING A1
CHANGING A2.
WRITE / A2.
FORM CALC USING F1 LIKE A1
CHANGING F2 LIKE A2.
F2 = F1 + ( F2 * 17
).
ENDFORM.
* Event for drill-down facilities (reacts when a user selects a line)
AT LINE-SELECTION.
WRITE
'This is displayed after double-clicking a line'.
Types, Data and Constants
REPORT
ZTYPDCONST.
* Type flag defines an abstract type
TYPES FLAG TYPE C.
* Field address_flag will allocate space
in main memory at runtime
DATA ADDRESS_FLAG TYPE FLAG VALUE 'X'.
* Constants are defined like fields and cannot
be changed
CONSTANTS: COMPANY_NAME(3) TYPE C VALUE 'SAP',
MAX_COUNTER TYPE I VALUE 9999.
* Using
constants to define initial values
DATA COUNTER TYPE I VALUE MAX_COUNTER.
Reporting
REPORT
CUSTPM.
** Declaration of a work area for a Dictionary table
TABLES CUSTOMERS.
* Internal table used as
snapshot of the database table
DATA ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 100
WITH HEADER
LINE.
* Reading the entries of the database table into an internal table
SELECT * FROM CUSTOMERS INTO TABLE
ALL_CUSTOMERS.
* Displaying each line of an internal table
LOOP AT ALL_CUSTOMERS.
WRITE: /
ALL_CUSTOMERS-NAME.
ENDLOOP.
Character Type
REPORT ZCHARACTER.
* Type c is the
default type when no type is specified.
* Initial value is space, if it is not specified explicitly.
DATA: NAME(25)
TYPE C,
CITY(25),
FLAG,
SINGLE_CHARACTER VALUE 'A'.
* If the field and the initial value
have different lengths, the
* initial value is either truncated or padded with blanks on the right:
DATA LANGUAGE(2)
VALUE 'ABAP/4'.
WRITE / LANGUAGE.
* Maximum length 64KB
DATA MAX_CHARACTER_FIELD(65535).
* Variables of
type n (numeric texts) contain strings of digits
DATA CUSTOMER_ID(8) TYPE N VALUE '87654321'.
* The default
length for a field of type n is 1,
* and the default initial value is a string of zeros
DATA ZIP_CODE(5) TYPE
N.
WRITE / ZIP_CODE.
* Type n fields pad the left side with zeroes
CUSTOMER_ID = '1234'.
WRITE /
CUSTOMER_ID.
Internal Flow of Control
REPORT ZFLOWCONTL.
* Declarations for later
use
TABLES CUSTOMERS.
DATA: COLOR(10) VALUE 'yellow',
N(4) TYPE N VALUE '123',
P TYPE P,
C4(4) VALUE '124',
C5(5) VALUE '00124',
SQUARE_NUMBER TYPE I,
X TYPE I,
Y TYPE
I.
* Using a condition (e.g., business class or not)
IF CUSTOMERS-CUSTTYPE = 'B'.
* book business class
WRITE 'B'.
ELSE.
* book economy class
WRITE 'Something else'.
ENDIF.
* Nested if clauses
IF N >
0.
N = N + 1.
ELSE.
IF N = 0.
WRITE / 'zero'.
ELSE.
N = N - 1.
ENDIF.
ENDIF.
* Using elseif instead of a nested if clauses
IF N > 0.
N = N + 1.
ELSEIF N = 0.
WRITE / 'zero'.
ELSE.
N = N - 1.
ENDIF.
* Using a case clause
CASE COLOR.
WHEN 'red'. WRITE
'color is red'.
WHEN 'green'. WRITE 'color is green'.
WHEN 'yellow'. WRITE 'color is yellow'.
WHEN
OTHERS. WRITE 'non-standard color'.
ENDCASE.
* Some logical expressions in if clauses
IF N IS INITIAL.
WRITE 'initial'.
ELSEIF N LT 0
OR N GT 5.
WRITE / 'less than zero or greater than 5'.
ELSE.
WRITE
/ 'something else'.
ENDIF.
IF N > P.
WRITE / 'n is greater than p'.
ENDIF.
* Conversion in an
expression
IF C4 = C5.
WRITE / 'c4 and c5 are equal'.
ENDIF.
* Comparing character strings
DATA: A(6)
VALUE 'ABAP/4',
RESULT(6).
IF A CA 'XP'.
* CA means "contains any"
RESULT = A+SY-FDPOS(2).
*in
string A where-ever you find P put cursor before it and then count 2 positions and write that
WRITE /
RESULT.
ENDIF.
IF A CO 'ABP'.
* CO means "Contains Only" - since not true it will fail
WRITE / 'a only
contains A,B, and P'.
ENDIF.
IF A CS 'BAP'.
* CS means "Contains STring"
WRITE / 'a contains the string
BAP'.
ENDIF.
IF A CP '*AP++'.
*contains plus - or AP followed by two characters
WRITE / 'a contains AP
followed by two more characters'.
ENDIF.
* Unconditional loop
DO 100 TIMES.
SQUARE_NUMBER = SY-INDEX **
2.
WRITE / SQUARE_NUMBER.
ENDDO.
* Terminating a loop
DO.
* terminate loop after 5 steps or when the
color is red
IF SY-INDEX > 5 OR COLOR = 'red'. EXIT. ENDIF.
* main loop step
WRITE / SY-
INDEX.
ENDDO.
* Using a conditional loop.
X = Y - 2.
WHILE X <> Y.
X = Y + 1.
WRITE / X.
IF X
> Y. EXIT. ENDIF.
ENDWHILE.
Date and Time
REPORT ZDATETIME.
* Date fields are
type d with the fixed length 8 and the internal
* representation YYYYMMDD (year, month, and day).
* The initial value
of a date field is 00000000.
DATA TODAY TYPE D.
* The write command formats dates according to personal settings
of
* the end user.
TODAY = SY-DATUM.
WRITE (10) TODAY.
* Using date fields to perform computations
DATA
ULTIMO TYPE D.
* Set variable to first day of current month.
ULTIMO = SY-DATUM.
ULTIMO+6(2) = '01'.
* Set
variable to last day of previous month.
SUBTRACT 1 FROM ULTIMO.
WRITE / ULTIMO.
* Time fields are type t with
the fixed length 6
* and the format HHMMSS (hours, minutes, and seconds)
DATA MY_TIME TYPE T.
WRITE /(8)
MY_TIME.
Hexadecimal
* Hexadecimal (or binary) data is stored in fields of type x.
* A type
x field of length n contains 2n digits
* and its output length is also equal to 2n.
* For example, the bit stream
1111000010001001 can be defined as
* follows (remind that 1111 = F, 0000 = 0, 1000 = 8, 1001 = 9):
DATA XSTRING(2)
TYPE X VALUE 'F089'.
Internal Tables
What is an Internal Table?.
- It is a dynamic data
object as it saves dynamic memory management of ABAP Program.
-The data is stored line by line in memory and each line has
same structure as that of data base table.In ABAP Internal table fulfill the function of arrays.
When do you use
Internal Table?.
Whenever ABAPer wants to process a dataset with a fixed structure within a program.Important use is
for storing and formatting data from a database table within a program.
Maximum memory that can be occupied by
internal table is 2 GB.
Creating Internal Tables:
1.Internal table types.
2.Internal table
objects.
Syntax for Internal table types.
TYPES
[WITH
[INITIAL SIZE
TYPE/LIKE - Reference
Standard Table, Sorted Table, Hashed Table.
- Standard Table appropriate if you are going to
address individual entries.
- Sorted Table is appropriate if you need a table which is sorted as
you fill it.
- Hashed Table is suitable for processing large amount of data.Like database table
Hashed tables always have a unique key.
[]
- Optional
- 0 Represents dynamic growth.
Example of Internal table concept:
* Declaration of a work area
for a Dictionary table
TABLES CUSTOMERS.
* Internal table used as snapshot of the database table
DATA
ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 100
WITH HEADER LINE.
* Reading the entries of the database
table into an internal table
SELECT * FROM CUSTOMERS INTO TABLE ALL_CUSTOMERS.
* Displaying each line of an
internal table
LOOP AT ALL_CUSTOMERS.
WRITE: / ALL_CUSTOMERS-NAME.
ENDLOOP.
--------------------------
---------------------------------------------
* Records (or structures) consist of a fixed number of
components
DATA: BEGIN OF CUSTOMER,
ID(8) TYPE N,
NAME(25),
TELEPHONE(12),
END
OF CUSTOMER.
* Working with the different components and the structure itself
DATA VENDOR LIKE
CUSTOMER.
CUSTOMER-ID = '87654321'.
CUSTOMER-NAME = 'Edison'.
CUSTOMER-TELEPHONE = '111-111-1111'.
MOVE
CUSTOMER TO VENDOR.
WRITE / VENDOR-NAME.
* Defining an internal table each entry having the structure of
* the
record customer
DATA ALL_CUSTOMERS LIKE CUSTOMER OCCURS 100.
* Using a reference to a non-elementary
type.
TYPES: BEGIN OF PERSONAL_DATA,
NAME(25),
CITY(25),
STREET(30),
END OF
PERSONAL_DATA.
DATA PEOPLE TYPE PERSONAL_DATA OCCURS 300.
* Internal table with a header line, which is used as a
default record
* to hold the record currently being added to the table
DATA NEW_CUSTOMERS LIKE CUSTOMER OCCURS 100
WITH HEADER LINE.
-----------------------------------------------------------------------
*&-----
----------------------------------------------------------------*
*& Chapter 4: Complex Non-Elementary Types and Data
Objects
*&---------------------------------------------------------------------*
REPORT CHAP0408.
* Nested
records
TYPES: BEGIN OF ADDRESS,
CITY(25),
STREET(30),
END OF ADDRESS,
BEGIN
OF PERSON,
NAME(25),
ADDRESS TYPE ADDRESS,
END OF PERSON.
DATA RECEIVER TYPE
PERSON.
RECEIVER-NAME = 'Smith'.
RECEIVER-ADDRESS-CITY = 'Big City'.
RECEIVER-ADDRESS-STREET = 'Main
street'.
* Nested internal tables
TYPES: BEGIN OF PHONE_FAX_NUMBERS,
COUNTRY_CODE(3) TYPE N,
AREA_CODE(3) TYPE N,
NUMBER(10) TYPE N,
END OF PHONE_FAX_NUMBERS,
BEGIN OF EMPLOYEE,
NAME(25),
PHONE TYPE PHONE_FAX_NUMBERS OCCURS 10,
FAX TYPE PHONE_FAX_NUMBERS OCCURS 5,
END OF EMPLOYEE.
DATA EMPLOYEES TYPE EMPLOYEE OCCURS 100.
Data Output
* Converting number
fields
REPORT ZCONNUM.
* Using numeric texts and packed numbers
DATA: NO_EMPLOYEES(4) TYPE N,
NO_ROOMS TYPE P,
EMPLOYEES_PER_ROOM TYPE P DECIMALS 2.
EMPLOYEES_PER_ROOM = NO_EMPLOYEES /
NO_ROOMS.
* Rounding with integers and packed numbers
DATA: INCOME TYPE I VALUE
'10000',
TAX TYPE P DECIMALS 2 VALUE '0.2',
NET_INCOME TYPE P DECIMALS 2,
ROUNDED_NET_INCOME TYPE I.
NET_INCOME = INCOME * ( 1 - TAX ).
ROUNDED_NET_INCOME = INCOME * ( 1 - TAX
).
WRITE: / NET_INCOME, ROUNDED_NET_INCOME.
------------------------------------------------
* Specifying a
format template
DATA TIME TYPE T VALUE '154633'.
WRITE AT (8) TIME USING EDIT MASK '__:__:__'.
* Using
decimals
DATA PACKED_NUMBER TYPE P VALUE 123.
WRITE PACKED_NUMBER DECIMALS 2.
INCOME =
'10002'.
NET_INCOME = INCOME * ( 1 - TAX ).
ROUNDED_NET_INCOME = INCOME * ( 1 - TAX ).
WRITE: / NET_INCOME,
ROUNDED_NET_INCOME.
Parameters
* Classifying parameters
REPORT
ZPARAMETERS.
* Data declarations for later use
DATA: A1 TYPE P VALUE 2,
A2 TYPE P VALUE 4,
A3
TYPE P VALUE 8.
* Calling a form with different parameter types
PERFORM CALC USING A1
A2
CHANGING A3.
* Displaying the result
WRITE A3.
* Defining a form with different
parameter types
FORM CALC USING VALUE(F1) LIKE A1
F2 LIKE A2
CHANGING VALUE
(F3) LIKE A3.
F3 = F1 + ( F2 * F3 ).
ENDFORM.
------------------------------------------------------------------
----
* Using table parameters
REPORT ZTABPARM.
* Work area of database table and internal table for
later use
TABLES CUSTOMERS.
DATA ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 50 WITH HEADER LINE.
* Calling a form with
a table parameter
PERFORM READ_CUSTOMERS TABLES ALL_CUSTOMERS.
LOOP AT ALL_CUSTOMERS.
WRITE / ALL_CUSTOMERS-
NAME.
ENDLOOP.
* Defining a form with a table parameter
FORM READ_CUSTOMERS TABLES F_CUSTOMERS STRUCTURE
ALL_CUSTOMERS.
SELECT * FROM CUSTOMERS INTO TABLE F_CUSTOMERS.
ENDFORM.
String
Operations
*String Operations
REPORT ZSTRING.
* Concatenating strings without
delimiter
DATA: FIRST_NAME(25), MIDDLE_NAME(2), LAST_NAME(25),
FULL_NAME(54).
FIRST_NAME =
'John'.
MIDDLE_NAME = 'F.'.
LAST_NAME = 'Kennedy'.
CONCATENATE FIRST_NAME MIDDLE_NAME LAST_NAME INTO
FULL_NAME.
WRITE / FULL_NAME.
* Concatenating strings with delimiter
DATA: DIRECTORY_1(2), DIRECTORY_2(10),
FILE_NAME(10),
PATH(24).
DIRECTORY_1 = 'a:'.
DIRECTORY_2 = 'usr'.
FILE_NAME =
'programs'.
CONCATENATE DIRECTORY_1 DIRECTORY_2 FILE_NAME
INTO PATH
SEPARATED BY
'\'.
WRITE / PATH.
* Splitting strings
DATA: LIST(40),
NAME_1(25), NAME_2(25), NAME_3
(25).
LIST = 'Edison,Smith,Young'.
SPLIT LIST AT ',' INTO NAME_1 NAME_2 NAME_3.
WRITE: / NAME_1, NAME_2,
NAME_3.
* Splitting strings with result in an internal table
DATA NAMES LIKE NAME_1 OCCURS 10 WITH HEADER
LINE.
LIST = 'Edison,Smith,Young,Edwards'.
SPLIT LIST AT ',' INTO TABLE NAMES.
LOOP AT NAMES.
WRITE /
NAMES.
ENDLOOP.
* Shifting strings by a fixed number of places
NAME_1 = 'Edison'.
NAME_2 =
'Smith'.
NAME_3 = 'Young'.
SHIFT NAME_1.
SHIFT NAME_2 BY 3 PLACES.
SHIFT NAME_3 RIGHT.
WRITE: / NAME_1,
NAME_2, NAME_3.
* Shifting strings up to a substring
NAMES = 'Alexander Bill Charles'.
SHIFT NAMES UP TO
'Bill'.
WRITE / NAMES.
* Shifting strings deleting blanks
NAMES = 'Joanne___'.
SHIFT NAMES RIGHT DELETING
TRAILING SPACE.
WRITE / NAMES.
* Replacing and translating characters in strings
DATA: STRING(80),
EXPRESSION(30).
STRING = 'Variable: &. The variable & is substituted later.'.
REPLACE '&' WITH 'X' INTO
STRING.
WRITE / STRING.
TRANSLATE STRING USING '&X'.
WRITE / STRING.
EXPRESSION = 'a ** 2 + b ** 2 = c **
2'.
TRANSLATE EXPRESSION USING 'axbycz'.
WRITE / EXPRESSION.
* Searching for strings in fields or internal
tables
DATA TEXT(100) VALUE 'Texas California New Mexico Louisiana Oregon'.
SEARCH TEXT FOR 'California'.
IF
SY-SUBRC NE 0. WRITE 'Not found'. ENDIF.
SEARCH TEXT FOR 'cAliforniA'.
IF SY-SUBRC NE 0. WRITE 'Not found'.
ENDIF.
SEARCH TEXT FOR 'New M'.
IF SY-SUBRC NE 0. WRITE 'Not found'. ENDIF.
* Working with parts of fields -
VERY IMPORTANT FUNDA!
* output is 123cde8
DATA: S(8) VALUE 'ABCDEFGH',
T(8) VALUE '12345678',
OFF1 TYPE I, OFF2 TYPE I,
LEN1 TYPE I, LEN2 TYPE I.
OFF1 = 2.
LEN1 = 3.
OFF2 = 4.
LEN2 = 3.
MOVE
S+OFF1(LEN1) TO T+OFF2(LEN2).
WRITE / T.
Symbols and Icons
* Using symbols and
icons
REPORT ZSYMBOL.
* Declaring symbols and icons
INCLUDE:
* Displaying symbols
and icons
WRITE: / SYM_PHONE AS SYMBOL, 'telephone',
/ SYM_FAX AS SYMBOL, 'fax machine',
/ SYM_LEFT_HAND AS SYMBOL, 'hand pointing left',
/ SYM_CAUTION AS SYMBOL, 'caution',
/ ICON_CHECKED
AS ICON, 'checked; okay',
/ ICON_DELETE AS ICON, 'delete',
/ ICON_PRINT AS ICON,
'print'.
System Fields
WRITE: / 'Current date', SY-DATUM,
/ 'Current
table index', SY-TABIX,
/ 'Loop counter', SY-INDEX,
/ 'System return code', SY-
SUBRC.

