Sei sulla pagina 1di 31

ABAP stands for Advanced Business Application Programming.

It is a programming language developed by


SAP.
ABAP language syntax
ABAP is not case sensitive.
Every statement begins with a keyword and ends with a period.( WRITE is the keyword to print on
screen )
WRITE 'Hello World!'.
Chained statements. If consecutive statements have identical part at the beginning, then ABAP allows
you to chain these statements into a single statement. First write the identical part once and then place
a colon (:). Then write the remaining parts of the individual statements separated by commas.Normal
Statements:
WRITE 'Hello'.
WRITE 'ABAP'.
Chained Statement:
WRITE: 'Hello', 'ABAP'.
Comments.If you want to make the entire line as comment, then enter asterisk (*) at the beginning of
the line.
* This is a comment line
If you want to make a part of the line as comment, then enter double quote () before the comment.
WRITE 'COMMENT'. "Start of comment
SAP Transaction code is a short cut key attached to a screen. Instead of using SAP easy access menu we
can also navigate to a particular screen in SAP by entering the transaction code (T-code for short) in the
command field of the standard toolbar.


Some of the useful transaction codes for ABAP developers.
T CODE DESCRIPTION
SE11 ABAP Data Dictionary
SE16 Data Browser
SE37 Function Builder
SE38 ABAP Editor
SE41 Menu Painter
SE51 Screen Painter
SE71 SAP Script Layout
SE80 ABAP Workbench
SE91 Message Maintenance
SE93 Maintain Transaction

First ABAP Program

Let us write a Hello SAP ABAP program. Navigate to ABAP editor under Tools node in SAP easy access.


Double click on ABAP Editor to open the editor. ABAP editor can also be opened by entering t-code SE38 in the
command field.

This is the ABAP editors initial screen. Enter the name of the program you want to create and press create. All
the customer programs must begin with Y or Z.

In the next popup screen(Program attributes) enter the title for your program, select Executable program as
type and press save.

Press Local Object to store the program in the temporary folder.

This is the screen where you can write the ABAP code.

Write the code. Press save, then syntax check( Ctrl + F2 ).

If there are any syntax errors, it ill be displayed at the bottom of the screen as shown above. Correct the errors
and again check the syntax.

Successful syntax check message will be displayed in the status bar. Then activate( Ctrl + F3 ) the program.

In the following screen select your program and press continue. Then run(F8) the program.

The output will be displayed as shown above.
ABAP Data Types and Constants
Data Type describes the technical characteristics of a Variable of that type. Data type is just the blue print
of a variable.
Predefined ABAP Types
DATA TYPE DESCRIPTION DEFAULT LENGTH DEFAULT VALUE
C Character 1
N Numeric 1 0
D Date 8 00000000
T Time 6 000000
X Hexa Decimal 1 X0
I Integer 4 0
P Packed 8 0
F Float 8 0
User defined data types
Use TYPES keyword to define the data types.
TYPES: name(10) TYPE c,
length TYPE p DECIMALS 2,
counter TYPE i,
id(5) TYPE n.
Structured data types
Structured data type is grouping of several simple data types under one name.
Use the keywords BEGIN OF and END OF to create a structured data type.
TYPES: BEGIN OF student,
id(5) TYPE n,
name(10) TYPE c,
dob TYPE d,
place(10) TYPE c,
END OF student.
Constants
Constants are used to store a value under a name. We must specify the value when we declare a constant and
the value cannot be changed later in the program.
Use CONSTANTS keyword to declare a constant.
CONSTANTS: pi TYPE p DECIMALS 2 VALUE '3.14',
yes TYPE c VALUE 'X'.


ABAP Variables

ABAP Variables are instances of data types. Variables are created during program execution and destroyed
after program execution.
Use keyword DATA to declare a variable.
DATA: firstname(10) TYPE c,
index TYPE i,
student_id(5) TYPE n.
While declaring a variable we can also refer to an existing variable instead of data type. For that use LIKE
instead of TYPE keyword while declaring a variable.
DATA: firstname(10) TYPE c,
lastname(10) LIKE firstname. " Observe LIKE keyword
Structured Variable
Similar to structured data type, structured variable can be declared using BEGIN OFand END OF keywords.
DATA: BEGIN OF student,
id(5) TYPE n,
name(10) TYPE c,
dob TYPE d,
place(10) TYPE c,
END OF student.
We can also declare a structured variable by referring to an existing structured data type.
TYPES: BEGIN OF address,
name(10) TYPE c,
street(10) TYPE c,
place(10) TYPE c,
pincode(6) type n,
phone(10) type n,
END OF address.

Data: house_address type address,
office_address like house_address.
Each individual field of the structured variable can be accessed using hyphen (-). For example, name field of the
house_address structure can be accessed using housing_address-name.
Character is the default data type.
DATA: true. " By default it will take C as data type
ABAP System Variables

ABAP system variables is accessible from all ABAP programs. These fields are filled by the runtime
environment. The values in these fields indicate the state of the system at any given point of time.
The complete list of ABAP system variables is found in the SYST table in SAP. Individual fields of the SYST
structure can be accessed either using SYST- or SY-.
WRITE:/ 'ABAP System Variables'.
WRITE:/ 'Client : ', sy-mandt.
WRITE:/ 'User : ', sy-uname.
WRITE:/ 'Date : ', sy-datum.
WRITE:/ 'Time : ', sy-uzeit.
Output

Basic Operations

Assigning values to ABAP variables
Use = or MOVE keyword to assign a value to a variable.
DATA: a TYPE i,
b TYPE i,
c TYPE i,
d TYPE i.
a = 10.
b = a.
MOVE 20 TO c.
MOVE c TO d.
WRITE:/ a, b, c, d.
Output

Basic Arithmetic Operations
DATA: a TYPE i,
b TYPE i,
c TYPE i,
d TYPE i.
*Using Mathematical Expressions
a = 10 + 20.
b = 20 - 10.
c = 10 * 2.
d = 100 / 2.
WRITE:/ 'Using Expressions'.
WRITE:/ a, b, c, d.
*Using Keywords
add 10 to a.
subtract 5 from b.
multiply c by 2.
divide d by 2.
WRITE:/ 'Using Keywords'.
WRITE:/ a, b, c, d.
Output
:
Clear ABAP variables
Use keyword CLEAR to set the variables to default values.
DATA: a TYPE i,
b TYPE i.
a = 10 + 20.
b = 20 - 10.
WRITE:/ 'Before Clear'.
WRITE:/ a, b.
clear: a, b.
WRITE:/ 'After Clear'.
WRITE:/ a, b.
Output

Control Statements

To control the flow of the ABAP program use the following statements.
IF Branching Conditionally
IF statement The code between IF and ENDIF is executed only if the condition is true.
DATA: a TYPE i VALUE 10. " We can assign a value in the declaration

IF a > 5.
WRITE:/ 'Condition True'.
ENDIF.
Output

IF-ELSE statement The code between IF and ELSE is executed if the condition is true, the code between
ELSE and ENDIF is executed if the condition is False.
DATA: a TYPE i VALUE 1.

IF a > 5.
WRITE:/ 'Condition True'.
ELSE.
WRITE:/ 'Condition False'.
ENDIF.
Output

IF-ELSEIF statement Used to check multiple conditions.
DATA: a TYPE i VALUE 2.

IF a > 5.
WRITE:/ a, 'Greater Than', 5.
ELSEIF a > 4.
WRITE:/ a, 'Greater Than', 4.
ELSEIF a > 3.
WRITE:/ a, 'Greater Than', 3.
ELSE.
WRITE:/ a, 'Less Than', 3.
ENDIF.
Output

CASE-ENDCASE Branching based on the content of the variable.
DATA: a TYPE i VALUE 4.

CASE a.
WHEN 3.
WRITE:/ a, 'Equals', 3.
WHEN 4.
WRITE:/ a, 'Equals', 4.
WHEN OTHERS.
WRITE:/ 'Not Found'.
ENDCASE.
Output

When no condition is met, OTHERS will be executed. OTHERS is not mandatory.
Loops
DO ENDDO Unconditional Loop
DO can be used to execute a certain lines of codes specific number of times.
DO 5 TIMES.
WRITE sy-index. " SY-INDEX (system variable) - Current loop pass
ENDDO.
Output

WHILE ENDWHILE Conditional Loop
WHILE can be used to execute a certain lines of codes as long as the condition is true.
WHILE sy-index < 3.
WRITE sy-index.
ENDWHILE.
Output

CONTINUE Terminate a loop pass unconditionally.
After continue the control directly goes to the end statement of the current loop pass ignoring the remaining
statements in the current loop pass, starts the next loop pass.
DO 5 TIMES.
IF sy-index = 2.
CONTINUE.
ENDIF.
WRITE sy-index.
ENDDO.
Output

CHECK Terminate a loop pass conditionally.
If the condition is false, the control directly goes to the end statement of the current loop pass ignoring the
remaining statements in the current loop pass, starts the next loop pass.
DO 5 TIMES.
CHECK sy-index < 3.
WRITE sy-index.
ENDDO.
Output

EXIT Terminate an entire loop pass unconditionally.
After EXIT statement the control goes to the next statement after the end of loop statement.
DO 10 TIMES.
IF sy-index = 2.
EXIT.
ENDIF.
WRITE sy-index.
ENDDO.
Output

String Operations

CONCATENATE Combines 2 or more strings into one string.
DATA: s1(10) VALUE 'Hello',
s2(10) VALUE 'ABAP',
s3(10) VALUE 'World',
result1(30),
result2(30).
CONCATENATE s1 s2 s3 INTO result1.
CONCATENATE s1 s2 s3 INTO result2 SEPARATED BY '-'.
WRITE / result1.
WRITE / result2.
Output


If the the concatenated string fits in the result string, then the system variable sy-subrc is set to 0. If the result has
to be truncated then sy-subrc is set to 4.
SPLIT Splits a string into 2 or more smaller strings.
DATA: s1(10), s2(10), s3(10),
source(20) VALUE 'abc-def-ghi'.
SPLIT source AT '-' INTO s1 s2 s3.
WRITE:/ 'S1 - ', s1.
WRITE:/ 'S2 - ', s2.
WRITE:/ 'S3 - ', s3.
Output

If all target fields are long enough and no target fields has to be truncated then sy-subrc is set to 0, else set to 4.
SEARCH Searches for a sub string in main string. If found then sy-subrc is set to 0, else set to 4.
DATA: string(30) VALUE 'SAP ABAP Development',
str(10) VALUE 'ABAP'.

SEARCH string FOR str.
IF sy-subrc = 0.
WRITE:/ 'Found'.
ELSE.
WRITE:/ 'Not found'.
ENDIF.
Output

REPLACE Replaces the sub string with another sub string specified, in the main string. If replaced successfully
then sy-subrc is set to 0, else set to 4.
DATA: string(30) VALUE 'SAP ABAP Development',
str(10) VALUE 'World'.
REPLACE 'Development' WITH str INTO string.
WRITE:/ string.
Output

ABAP Data Dictionary
What is ABAP Data Dictionary?
SQL can be divided into following 2 parts.
DML Data Manipulation Language
DDL Data Definition Language
DML part consists of query and update commands like SELECT, UPDATE, DELETE, INSERT etc. ABAP
programs handle DML part of SQL.
DDL part consist of commands like CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX etc.
ABAP Dictionary handles DDL part of SQL.
So ABAP Dictionary is used to create and manage data definitions (metadata). ABAP Dictionary is used to
create Tables, Data Elements, Domains, Views, Lock Objects etc.
What are Data Elements and Domains?
While creating a table in data dictionary each table field is assigned to a data element. Each data element is in
turn assigned to a domain.
Domain describes the technical attributes such as data type and length of a table field.
Data Element gives the field labels and documentation for the table field.
Consider the following code. PARAMETERS is the keyword to create input fields.
PARAMETERS: p_matnr TYPE matnr.
Output

In order to get the field labels use the menu in the ABAP editor GOTO->Text Elements->Selection Texts.

All the input fields will be displayed in a table.

Just check the Dictionary Ref checkbox and press enter.

The Text ( Field Label ) will be automatically read from the Data Element. Now save, activate, go back and run
the program again.

Now place the cursor on the input field and press F1 key to get the documentation from the data element.
Actually this field label and documentation are maintained in the Data Element MATNR.
So if we want to create a table with two fields as FirstName and SecondName, then only one domain is
sufficient with data type as CHAR and length say 20. But we need two different data elements in order to
display different field labels and documentation for both the fields.

How to create a domain in SAP?
To create a domain go to t-code SE11.

Select the Domain radio button, enter the the name of the domain that you want to create and press create.
Enter the short description. Place the cursor in the data type and press F4 to get the list of SAP data types.

In the popup window select the correct data type.

Enter the number of characters. Enter the decimal places if it applicable to data type that you have selected.
Save and activate the domain.
How to create a Data Element in SAP?
To create a data element, go to t-code SE11.

Select the Data type radio button, enter the name of the data element and press create.

Select the Data Element radio button and press continue.

Enter short description. Assign a domain to the data element. Press field label tab to maintain the field labels for
the data element.
Enter the field labels, Save and activate the data element.
How to create a SAP table?
Every table has a unique name and consists of rows and columns. The number of columns in a table is fixed but
can have any number of rows.
ABAP dictionary handles the DDL part of SQL in SAP. Go to ABAP dictionary (SE11) to create a SAP table.
Enter the name of the table to be created and press enter.

Enter a proper short description for the table and maintain delivery class as A(Application Table). Now press on
Fields tab to maintain the fields of the table.
Enter the fields of the table and maintain the proper data elements for the table fields. You can use the standard
data elements or you can create your own data elements.
Maintain the primary key and press save. To maintain the technical attributes of table like tablespace, size etc.
press the Technical attributes button on application toolbar.

Enter Data class, Size category and Save.

Now press back, save and activate the table.
Maintain test data in SAP table
After creating a table you can test it by maintaining a couple of entries in the table. Follow the below mentioned
procedure to maintain entries in the table. But this procedure must be used only in testing or development
environment not in production environment.
Display the table in ABAP Dictionary (SE11). Use the menu path Utilities->Table Contents->Create Entries.

If this menu path is disabled then go to Delivery and Maintenance tab and
enter Display/Maintenance Allowed in Data Browser/Table View Maint. listbox.

Save and activate the table. Then go back to menu path Utilities->Table Contents->Create Entries.

Enter some test values in the fields and press save.

Observe the message in the status bar. To display the values in the table go to Utilities->Table Contents-
>Display
In the selection screen enter the selection criteria if you want to filter the records you want to display and press
execute.

All the records in the table will be displayed if no selection criteria specified
in the selection screen.
What are Primary Keys and Foreign Keys?
A primary key is a field or group of fields that uniquely identify a record in a table. Primary key fields cannot be
NULL and cannot contain duplicate values.
If you want to link two tables, then primary key of one table will be added to another table where primary key of
first table will be become the foreign key of second table.
Consider the following two tables.
Department Table
DEPARTMENT_ID (PRIMARY
KEY)
DEPARTMENT_NAME
01 Computers
02 Physics
03 Electronics
Employee Table
EMPLOYEE_ID
(PRIMARY KEY)
NAME PLACE
001 Jim New York
002 Jack London
003 Robin Sydney
004 Raj Bangalore
If you want to link department table and employee table, then add the primary key of department table i.e.
Department_ID to employee table. Department_ID becomes the foreign key of employee table and Department
table becomes the check table.
Employee table after creating foreign key.
EMPLOYEE_ID
(PRIMARY KEY)
NAME PLACE DEPARTMENT_ID
(FOREIGN KEY)
001 Jim New York 01
002 Jack London 01
003 Robin Sydney 02
004 Raj Bangalore 03
The main purpose of the foreign key is data validation. This will not allow entering a Department_ID in the
employee table that is not there in the department table. For example if you try to create a record in employee
table with Department_ID as 04, it will throw an error.
How to create a Foreign Key in SAP table?
The purpose of the foreign key is to validate the data that is being entered into a table by checking entries in a
check table. Foreign keys are checked by the front end user interface only and it is not checked if you issue a
direct a SQL statement to update the database.
Follow the steps given below to create a foreign key in SAP table.
Step 1: Open the table in Data Dictionary (SE11) for which you want to create a foreign key. Select the field for
which you want to create the foreign key and press Foreign Keys button.
Step 2: In the popup window enter the check table name and press Generate proposal button.
Step 3: The system proposes the foreign key relation based on the domain. Check that the foreign key
relationship proposed by the system is correct and press copy.
Foreign key is created, now save and activate the table. To check the foreign key go to menu path Utilities-
>Table Contents->Create Entries.


Try to create an entry in Zemployee table with Department ID that is not there in the Department table. Enter
the values and try to save the entry.

Observe the error message in the status bar. The user interface does the foreign key validation before creating
the entries.

Potrebbero piacerti anche