Anda di halaman 1dari 42

Inspire ABAP training

sessions
Training session # 3
Date: 16/06/2014
Contents
ABAP Data Dictionary
Data Elements
Domains
SAP Tables
Open SQL
Modularization
ABAP Data Dictionary
Create and manage data definitions (metadata)
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.
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.

Table Field
Data Element
Domain
Create a Domain in SAP
Create a Data Element in SAP
Create a SAP table
There are three types of SAP DDIC table that you can define.
1. Transparent Table
2. Cluster Table
3. Pooled Table

1. Transparent Table
A transparent table in the dictionary has a one-to-one relationship with a table in the database.
Its structure in R/3 Data Dictionary corresponds to a single database table.
For each transparent table definition in the dictionary, there is one associated table in the database.
The database table has the same name, the same number of fields, and the fields have the same names
as the R/3 table definition.
They are used to hold application data. Application data is the master data or transaction data used by an
application.

Transparent tables (BKPF, VBAK, VBAP, KNA1, COEP)

Allows secondary indexes (SE11->Display Table->Indexes)
Can be buffered (SE11->Display Table->technical settings) Heavily updated tables should not be buffered.

2. Cluster Table
Many-to-one relationship with a table in the database.
Many cluster tables are stored in a single table in the database called a table cluster
Tables have a part of their primary keys in common.

III. Cluster Tables (BSEG, BSED, BSEC)

Should be accessed via primary key - very fast retrieval otherwise very slow
No secondary indexes
Select * is Ok because all columns retrieved anyway. Performing an operation on multiple rows is more
efficient than single row operations. Therefore you still want to select into an internal table. If many rows
are being selected into the internal table, you might still like to retrieve specific columns to cut down on the
memory required.
Statistical SQL functions (SUM, AVG, MIN, MAX, etc) not supported
Can not be buffered


3. Pooled Table
Many-to-one relationship with a table in the database.
For one table in the database, there are many tables in the Data Dictionary. The table in the database
has a different name than the tables in the DDIC, it has a different number of fields, and the fields have
different names as well.

II. Pool Tables (match codes, look up tables)
A001, A004, A044
Should be accessed via primary key or
Should be buffered (SE11->Display Table->technical settings)
No secondary indexes
Select * is Ok because all columns retrieved anyway

SAP Table Maintenance Generator
SAP Table Maintenance Generator (TMG) is a tool to generate a table maintenance program i.e. it will
generate a program to maintain (Create, Edit & Delete) entries in a table.

Open SQL
Open SQL is a set of ABAP statements that performs operations like reads, modifies or deletes
data in the SAP database.
All open SQL statements are pass Open SQL is independent of the database system, so the
syntax of the open SQL is uniform for all the databases supported by SAP.
The DB interface converts the open SQL to native SQL and passes it on to the database.


OPEN SQL DESCRIPTION
SELECT Reads data from database
INSERT Inserts lines to database
UPDATE Changes the contents of lines in
database
MODIFY Inserts lines into database or
changes the contents of existing
lines
DELETE Deletes lines from database
Open SQL
All Open SQL statements fill the following two system fields:
SY-SUBRC After every Open SQL statement, the system field SY-SUBRC contains the value 0 if the
operation was successful, a value other than 0 if not.
SY-DBCNT After an open SQL statement, the system field SY-DBCNT contains the number of database
lines processed.
Reading Data using Open SQL
SELECT is the open SQL statement to read the data from the database. The general syntax for SELECT
statement is as follows.
SELECT <result>
INTO <target>
FROM <source>
[WHERE <condition>]

CLAUSE DESCRIPTION
SELECT <result> Specifies which columns you want to read,
whether one line or many lines needs to
selected, and whether duplicate entries are
allowed
INTO <target> Determines the target area into which the
selected data is to be placed
FROM <source> Specifies the database table from which the
data is to be selected
WHERE
<condition>
specifies which lines are to be read by
specifying conditions for the selection
Reading Data using Open SQL
Source Code
DATA: gwa_employee TYPE zemployee.

WRITE:/1 'Emp ID' color 5,9 'Name' color 5,17 'Place' color 5,
27 'Phone' color 5,39 'Dept' color 5.

SELECT * FROM zemployee INTO gwa_employee.
WRITE:/1 gwa_employee-id,9 gwa_employee-name,
17 gwa_employee-place,27 gwa_employee-phone,
39 gwa_employee-dept_id.
ENDSELECT.
Table Output
Inserting Values using SAP Open SQL
INSERT is the open SQL statement to add values to the database table.
First declare a work area as the line structure of database table and populate the work area with the
desired values. Then add the values in the work area to the database table using INSERT statement.

The syntax for the INSERT statement is as follows.
INSERT <database table> FROM <work area>
or
INSERT INTO <database table> VALUES <work area>

EMPLOYEE table entries after INSERT
Inserting Values using SAP Open SQL
Source Code
DATA: gwa_employee TYPE zemployee.

gwa_employee-id = 6.
gwa_employee-name = 'MARY'.
gwa_employee-place = 'FRANKFURT'.
gwa_employee-phone = '7897897890'.
gwa_employee-dept_id = 5.

INSERT zemployee FROM gwa_employee.
Table
Changing Values using SAP Open SQL
UPDATE is the open SQL statement to change the values in the database table. First declare a work area
as the line structure of database table and populate the work area with the desired values for a specific
key in the database table. Then update the values for the specified key in the database table using
UPDATE statement.
The syntax for the UPDATE statement is as follows.
UPDATE <database table> FROM <work area>
If the database table contains a line with the same primary key as specified in the work area, the operation
is completed successfully and SY-SUBRC is set to 0. Otherwise, the line is not inserted, and SY-SUBRC is
set to 4.

EMPLOYEE table entries before UPDATE EMPLOYEE table entries after UPDATE
Changing Values using SAP Open SQL
Source Code
DATA: gwa_employee TYPE zemployee.

gwa_employee-id = 6.
gwa_employee-name = 'JOSEPH'.
gwa_employee-place = 'FRANKFURT'.
gwa_employee-phone = '7897897890'.
gwa_employee-dept_id = 5.

UPDATE zemployee FROM gwa_employee.
EMPLOYEE table entries after UPDATE
Changing Values using SAP Open SQL
Source Code
UPDATE zemployee SET place = 'MUMBAI' WHERE dept_id = 2.
Deleting Entries using SAP Open SQL
DELETE is the open SQL statement to delete entries from database table. First declare a work area as the
line structure of database table and populate the work area with the specific key that we want to delete
from the database table. Then delete the entries from the database table using DELETE statement.
The syntax for the DELETE statement is as follows.
DELETE <database table> FROM <work area>
If the database table contains a line with the same primary key as specified in the work area, the operation
is completed successfully and SY-SUBRC is set to 0. Otherwise, the line is not deleted, and SY-SUBRC is
set to 4

EMPLOYEE table entries before DELETE EMPLOYEE table entries after DELETE
Deleting Entries using SAP Open SQL
Source Code
DATA: gwa_employee TYPE zemployee.

gwa_employee-id = 6.
gwa_employee-name = 'JOSEPH'.
gwa_employee-place = 'FRANKFURT'.
gwa_employee-phone = '7897897890'.
gwa_employee-dept_id = 5.

DELETE zemployee FROM gwa_employee.
EMPLOYEE table entries after DELETE
Deleting Entries using SAP Open SQL
Source Code
DELETE FROM zemployee WHERE dept_id = 2.
Inserting or Changing Values using SAP Open SQL
MODIFY is the open SQL statement to insert or change entries in the database table. If the database table
contains no line with the same primary key as the line to be inserted, MODIFY works like INSERT, that is,
the line is added. If the database already contains a line with the same primary key as the line to be
inserted, MODIFY works like UPDATE, that is, the line is changed.
The syntax for the MODIFY statement is as follows.
MODIFY <database table> FROM <work area>
If the database table does not already contain a line with the same primary key as specified in the work
area, a new line is inserted. If the database table does already contain a line with the same primary key as
specified in the work area, the existing line is overwritten. SY-SUBRC is always set to 0.

ZEMPLOYEE table entries before MODIFY ZEMPLOYEE table entries after MODIFY
Source Code
DATA: gwa_employee TYPE zemployee.

gwa_employee-id = 6.
gwa_employee-name = 'JOSEPH'.
gwa_employee-place = 'FRANKFURT'.
gwa_employee-phone = '7897897890'.
gwa_employee-dept_id = 5.

MODIFY zemployee FROM gwa_employee.
Inserting or Changing Values using SAP Open SQL
Output
Source Code
DATA: gwa_employee TYPE zemployee.

gwa_employee-id = 6.
gwa_employee-name = 'JOHNNY'.
gwa_employee-place = 'LONDON'.
gwa_employee-phone = '7897897890'.
gwa_employee-dept_id = 3.

MODIFY zemployee FROM gwa_employee
Inserting or Changing Values using SAP Open SQL
Modularization in ABAP
Modularization is breaking down the application code into smaller units, so that it is easy to maintain.
Advantages of Modularization are as follows.
Easier to read, maintain and debug.
Eliminates redundancy of code and increases reusability of code .

Types of Modularization
Macros If you want to reuse the same set of statements more than once in a program, you can
include them in a macro. You can only use a macro within the program in which it is defined.
Include Programs Include programs allow you to use the same source code in different programs.
They are mainly used for modularizing source code and have no parameter interface.
Subroutines Subroutines are normally called internally i.e. called from the same program in which it
is declared. But subroutines can also be called from external programs.
Function Modules Function Modules are stored in the central library and can be called from any
program. Even the function modules(RFC enabled) can be called from non-SAP systems.
Methods Methods used ABAP object oriented programming

If we want to reuse the same set of statements more than once in a program, we can include them in a
macro. We can only use a macro within the program in which it is defined. Macro definition should occur
before the macro is used in the program.

Syntax
DEFINE <macro name>.
...
END-OF-DEFINITION.
Program
*Macro definition
DEFINE print.
write:/ 'Hello Macro'.
END-OF-DEFINITION.

WRITE:/ 'Before Using Macro'.
print.
Output
Macros
Include programs are not standalone programs and cannot be executed independently.
Container for ABAP source code.
They are used to organize the ABAP source code into small editable units which can be inserted at any place in
other ABAP programs using the INCLUDE statement.
The include statement copies the contents of the include program into the main program.
ABAP Include Program
If we want to reuse the same set of statements more than once in a program, we can include them in a
macro. We can only use a macro within the program in which it is defined. Macro definition should occur
before the macro is used in the program.

Syntax
INCLUDE <Include Program Name>
Source Code
Source code of ZINCLUDE_DATA.
DATA: g_name(10) TYPE c.

Source code of ZINCLUDE_WRITE.
WRITE:/ 'Inside include program'.

Source code of main program.
REPORT zmain_program.
INCLUDE zinclude_data.
WRITE:/ 'Main Program'.
INCLUDE zinclude_write.
Output
ABAP Include Program
Subroutines are procedures that we define in an ABAP program
Can be called from any program..

ABAP Subroutine
Function Group
When you create a function module, you must assign it to function group
The function group is the main program in which a function module is
embedded
is a container for function modules
Function Library
Admin
Import/Export Parameter
Source Code
Main Program
Documentation

ABAP Function Modules
ABAP Function Modules
ABAP Function Modules
is a code that can be called from any ABAP program, therefore making it a globally accessible object
ABAP program pass data to function module from import parameters or internal tables
Function module receives data from a program, process the information in its own code, and then sends
back information in the export parameters or internal tables

ABAP Function Modules
ABAP Function Modules
ABAP Function Modules
ABAP Function Modules



FUNCTION Z_FMTEST.
result = number1 ** number2.
ENDFUNCTION.




ABAP Function Modules
REPORT ztest.
PARAMETERS: no1 TYPE I,
no2 TYPE I.
DATA result TYPE I.
START-OF-SELECTION.
CALL FUNCTION Z_FMTEST
EXPORTING
number1 = no1
number2 = no2
IMPORTING
result = result.
write: / result.
Exercise : Function Module
?
ABAP Program
Function Module
ABAP Practice

Anda mungkin juga menyukai