abap fundamentals

21
1 Chapter No 1 : FUNDAMENTALS This chapter explains the concepts of the SAP architecture and the main elements of the SAP development environment. Topics : 1. SAP R/3 ARCHITECHTURE 2. DATA DICTIONARY 3. DATA STRUCTURES 4. MODULARIZATION UNITS 5. MESSAGES

Upload: rajeshb4u

Post on 25-Oct-2014

148 views

Category:

Documents


17 download

DESCRIPTION

fundamentals for abap programming

TRANSCRIPT

Page 1: ABAP Fundamentals

1

Chapter No 1 : FUNDAMENTALS

This chapter explains the concepts of the SAP architecture and the main

elements of the SAP development environment.

Topics :

1. SAP R/3 ARCHITECHTURE

2. DATA DICTIONARY

3. DATA STRUCTURES

4. MODULARIZATION UNITS

5. MESSAGES

Page 2: ABAP Fundamentals

2

SAP R/3 ARCHITECHTURE

Topic 1 : SAP R/3 ARCHITECHTURE

This chapter explains the concepts of SAP R/3 client/ server

architecture, Role of each server, Types of work process and role of

each work process.

Server roles:

Presentation server: This layer has SAP GUI which is used to connect SAP. This layer is accessed by the user to make requests to other two layers Application server: This layer has the kernel of the SAP System all the application logic resides in this layer. The request from the presentation layer comes to application layer and if needed redirect it to the database layer Database server: This serves as data store consisting of tables.

SAP R/3 Client / Server Architecture

Page 3: ABAP Fundamentals

3

INTERVIEW QUESTIONS:

1. Explain client server architecture with example

2. What are the differences between 2-tier and 3-tier architecture and give example of both

3. What are the advantages of 3-tier over 2-tier architecture 4. What is the meaning of real-time with example 5. What are the other products from sap AG

Types of Work Processes:

Role of work processes

D Dialog Work process

Deals with the active user requests and executes dialog steps

V

Update Work Process

Executes database update requests.

B

Background Work process

Processes programs that can be executed without user interaction (background jobs).

E

Enqueue Work process

Administers a lock table in the shared memory area

S Spool Work process

Passes sequential datasets to a printer

M

Message work process

Used for load balancing in a scenario where there are more than one application servers

G

Gateway work process

Used whenever there is used to establish a connection between SAP and NON-SAP systems

Page 4: ABAP Fundamentals

4

DATA DICTIONARY

Chapter No 1 : Topic 2 : DATA DICTONARY

This Topic explains the concepts of Domain, Data element, Tables ,

Technical settings, Foreign key relationship, Views and types of views,

Search helps and types of search helps, Lock objects and Type groups

DATA FIELD FEATURES:

DOMAIN

Gives technical description (data type & length) of the field. The value range can be limited by specifying fixed values. Example: integer, 10

DATA ELEMENT

Gives the label or English name of the field. It contains primarily the field help (F1 documentation) and the field labels in the screen. Example: Customer name

NOTE: Data element and Domain are Meta data. One can share Data elements and Domains across fields. It is possible to create a field without data element but the domain is mandatory and is not recommended

TABLES:

It consists of columns (fields) and rows (entries). It has a unique name and attributes, such as delivery class and maintenance authorization. A field has a unique name and attributes. Every table must have a unique primary key which identifies a unique record of the table.

Page 5: ABAP Fundamentals

5

COMPOSITE PRIMARY KEY is Combination of two or more primary key fields. All the key fields are considered when the record is retrieved You must specify a reference table for fields containing a currency (data type CURR) or quantity (data type QUAN). It must contain a field (reference field) with the format for currency keys (data type CUKY) or the format for units (data type UNIT). The field is only assigned to the reference field at program runtime.

TABLE MAINTENANCE GENERATOR (SE54) is a utility which is used to maintain mass data in the tables. Referential integrity is checked in Table maintenance generator but not in single entry TECHNICAL SETTINGS OF TABLES:

1. You must maintain the technical settings when you define a

transparent table in the ABAP Dictionary. 2. The technical settings are used to individually optimize the storage

requirements and accessing behavior of database tables. 3. The technical settings can be used to define how the table should be

handled when it is created on the database, whether the table should be buffered and whether changes to entries should be logged.

4. The table is automatically created on the database when it is activated in the ABAP Dictionary.

5. The storage area to be selected (tablespace) and space allocation settings is determined from the settings for the data class and size category.

6. The settings for buffering define whether and how the table should be buffered. You can define whether changes to the table entries should be logged.

FOREIN KEY RELATIONSHIP:

1. One can create foreign key if the two fields of different tables have the same DOMAIN but field names or data elements can be different. Referential integrity in SAP is covered under the foreign

Page 6: ABAP Fundamentals

6

key relationship 2. CHECK TABLE is the table that has check field containing values

that is checked by foreign key of FOREIGN KEY TABLE. 3. Check table can also be created on non-primary key field.

a. ADVANTAGES OF CHECK TABLE: (1)Ensures the data integrity (2)Provides F4 or INPUT HELP E.g. ZDEP-depid � CHECK TABLE ZEMP-empid � FOREIGN KEY TABLE

VIEWS: Data for an application object is often distributed on several database tables. Database systems therefore provide you with a way of defining application-specific views on data in several tables. These are called views. Types of views

1 DATABASE VIEWS

These can be created on more than one table and preferably these tables should be linked using check table. One can retrieve data from database views using select statement

2 PROJECTION VIEWS:

Exactly same as database view except that it can be created on only one table

3 MAINTENANCE VIEWS

These can be created on one or more tables but one cannot retrieve data using select statement

4 HELP VIEWS These can be created to be used in the search helps

Page 7: ABAP Fundamentals

7

SEARCH HELP

It is a mechanism used to search data within SAP

1

ELEMENTARY SEARCH HELPS: These has three components

A) Selection method: This can be a database table or help view B) Dialog behavior: This determines the way in which data should be exported back to the screen C) Parameter Interface: Import parameters form the input search help where as export parameters form the output of the search help

2 COLLECTIVE SEACH HELPS:

• It is a container of multiple elementary search helps.

• After the creation of search help, it should be assigned to the corresponding table field

LOCK OBJECTS: The R/3 system synchronizes simultaneous access of several users to the same data records with lock mechanism Apart from database lock mechanism, SAP provides with the help of lock objects. These should not be created using Z or Y. once it is created the system generates automatically two function modules ENQUEUE-<Table Name> DEQUEUE-<Table Name>

TYPE GROUPS: These are globally defined data types which can be accessed from any ABAP program. While referring type groups from a program, one must use the key word TYPE-POOLS

Page 8: ABAP Fundamentals

8

TYPES

There are three different type categories in the ABAP Dictionary and are available to all the programs

TABLE TYPES:

Describes the structure of an internal table.

DATA ELEMENTS: Describe an elementary type by defining the data type, length and possibly decimal places.

STRUCTURES:

Consist of components that can have any type.

LAB EXERCISES:

1. Create a employee table and a department table 2. Create a append structure and include structure 3. Create database, projection, maintenance, and help views 4. Create a search help that allows us to search emp numbers both

based on empid and depid (collective search help) INTERVIEW QUESTIONS:

1. What is buffering of tables and its types. 2. What do you understand by table index? 3. Difference between primary and secondary index 4. Different types of search helps and how they are used 5. Purpose of the following T-codes

a. SE11 b. SE12 c. SE13 d. SE14 e. SE16 f. SM30

6. What are two level domain concepts? 7. What is conversion routine and where it is assigned 8. What are fixed values? 9. Define the following

a. Data element b. Domain c. Check table d. Reference table e. Value table

Page 9: ABAP Fundamentals

9

10. Different types of tables and views 11. Difference between structure and table 12. Difference between include structure append structure 13. What is customer includes 14. What is lock object and purpose of it? 15. what is the purpose of table maintenance generator 16. Define type groups 17. Different types of tables 18. What are text tables

Page 10: ABAP Fundamentals

10

DATA STRUCTURES

Chapter No 1 : Topic 3 : DATA Structures

This Topic explains the concepts of work area, Internal tables, internal

table operations types of internal tables and Performance tuning

(1) Control goes to selection screen from ABAP Editor to get the user request

(2) User sends the request from selection screen to ABAP Editor (3) ABAP Editor sends the request to the Database (4) Database sends the request of data to ABAP Editor. (5a) Control goes to again selection screen if the user given data do not exist in Database. (5b) If the user given data exist in Database, it outputs the data in the list screen Or EMAIL.

It can hold only single record and works as interface between internal table and list output. It can also work as interface between database and internal table but it is a performance issue.

DATA DECLARATIONS

…………….

DATA SELECTIONS

……………..

DATA OUTPUT

………………

TABLES

STRUCTURES

VIEWS

List screen

EMAIL

DATABASE (SE11) ABAP EDITOR (SE38) Selection Screen

1

2 3

4 5a

5b

WORK AREA:

Page 11: ABAP Fundamentals

11

Internal tables can hold multiple records and are defined by using the syntax …TYPE TABLE OF… Internal tables can be defined either with an implicit work area or an explicit work area. There are two ways of filling internal tables either through work area or directly into the body. However, one has to make use of either an implicit or an explicit work area to output the data. Default memory of ITAB is 8 KB and can increase dynamically.

OPERATIONS ON INTERNAL TABLE: 1 COLLECT This statement helps in performing mathematical

operations on ITAB this statement works on ITAB both as modify statement if a record already exists with the same key and as append statement if a record exists with the different key.

2 READ This statement reads the given line of records at an index level. If duplicate is found, it retrieves first entry. To read multiple records, we keep it in loop and endloop.

INTERNAL TABLE:

Internal Table Operations

Page 12: ABAP Fundamentals

12

SINGLE RECORD READING Read Table IT_EMP INTO WA_EMP INDEX 5. WRITE: / WA_EMP-EMPID, / WA_EMP-EMPNM, / WA_EMP-SALARY, / WA_EMP-ADDRESS. MULTIPLE RECORDS READING LOOP Read Table IT_EMP INTO WA_EMP INDEX 5. WRITE: / WA_EMP-EMPID, / WA_EMP-EMPNM, / WA_EMP-SALARY, / WA_EMP-ADDRESS. ENDLOOP.

3 MODIFY This statement modifies the contents of one or more records of the ITAB. One can modify it using an index or key of the table. WA_EMP-EMPID = ‘21’ WA_EMP-EMPNM = ‘corp’ WA_EMP-OPERA = ‘APPEND IS MODIFIED’ INDEX 6. MODIFY IT_EMP from WA_EMP transporting OPERA.

4 APPEND This statement adds the record at the end of all the records in table. WA_EMP-EMPID = ‘21’ WA_EMP-EMPNM = ‘corp’ WA_EMP-OPERA = ‘APPEND ‘. APPEND WA_EMP to IT_EMP.

5 DELETE This statement deletes the record wherever we specify with index. DELETE TABLE IT_EMP INDEX 5.

6 SORT This statement sorts the table in ascending or descending order according to the given field.

Page 13: ABAP Fundamentals

13

7 INSERT This statement inserts the new record at the given index. DATA: OPERA (10) TYPE C. WA_EMP-EMPID = ‘25’ WA_EMP-EMPNM = ‘HNC’ WA_EMP-OPERA = ‘INSERT ‘. INSERT WA_EMP into IT_EMP INDEX 2.

8 DESCRIBE This statement describes the number lines of the ITAB at runtime.

9 REFRESH This statement removes all the contends of data from internal table

10 CLEAR This statement removes all the data from workarea.

TYPES OF TABLES : 1 STANDARD TABLE By default all ITABS are standard table. One

can perform ITAB operations with the help of either table index or key of the table.

2 SORTED TABLE one can perform ITAB operations with the help

Access Types: Overview

Page 14: ABAP Fundamentals

14

of either table index or key of the table As it is a sorted table, we need not to perform SORT operation.

3 HASHED TABLE This table has to be created only with an explicit key that has to be always unique.

Note: SY-TABIX: Every ITAB has a record index called as SY-TABIX. This index hence perform internal table operations To find out the access type of an internal table at runtime, use the statement DESCRIBE TABLE <itab> KIND <charfield>.

Comprises character fields or character set (CHAR and NUMC) of columns of leftmost of an ITAB. To do the collect statement operations, the processor uses the key of the ITAB. If the key is not specified, it always takes the leftmost character set of columns. This adds all the non-character fields by comparing all the character fields of the table (performance problem). LAB EXERCISES:

1. Write programs to demonstrate the following • Unstructured data type • Workarea • Single access (internal table) • Multiple access (internal table) • Implicit workarea

2. Write programs to demonstrate selection screen variants 3. Write a program using internal table operations 4. Write a program using database table operations 5. Write two programs one with inner join and another one with for

all entries

INTERNAL TABLE KEY:

Page 15: ABAP Fundamentals

15

INTERVIEW QUESTIONS:

1. What is Work Area? 2. Difference between work area and internal table. 3. Different types of internal tables. 4. What are the various operations possible on internal table? 5. Describe the following ABAP syntax:

• DO • LOOP • CLEAR • REFRESH • FREE • STOP • EXIT • REJECT • DESCRIBE • CONCATENATE • FIND • REPLACE • SHIFT • OVERLAY • TRANSLATE • CONVERSION ROUTINE • OFFSET • WRITE • SKIP

PERFORMANCE TUNING

1. While writing SELECT statement makes sure you SELECT only

relevant fields. 2. Avoid using SELECT *. 3. Always user WHERE clause. 4. Avoid using SELECT, ENDSELECT. 5. Always pass full primary key to get the data. 6. Avoid nested loops. 7. Avoid using more than 3 nested if, alternately use CASE

statement. 8. Never use OUTER JOIN. 9. Avoid using MOVE-CORRESPONDING. 10. Restrict the usage of GLOBAL Variables. 11. Do not define unnecessary variables.

Page 16: ABAP Fundamentals

16

MODULARIZATION UNITS

Chapter No 1 : Topic 4 : Modularization This Topic explains the concepts of modularization units like macros,

subroutines, function modules and includes.

Modularization techniques are used to break the program into small modules which are more readable, understandable and reusable. Techniques

Call

Called

Number of parameters

Re-usability

MAX = 9

MACROS

MACRO <macro>

DEFINE <macro> ------------------- ENE-OF- DEFINITION

A = F

LIMITED

NO LIMIT

SUB-ROUTINES

PERFORM<sub>

FORM <sub> ----------------- ENDFORM

A = F

BETTER

NO LIMIT

FUNCTION MODULES

CALL FUNCTION <func>

FUNCTION <func> ---------------------- ---------------------- ENDFUNCTION

Need not be A = F

BEST

NO LIMIT

CLASSES

CLASS<cls> DEFINITION ----------------- ----------------- ENDCLASS

CLASS <cls> IMPLEMENTATION -------------------- -------------------- ENDCLASS

Need not be A = F

BEST

INCLUDES

INCLUDE <inc>

INCLUDE <inc>

No parameters

BETTER

Page 17: ABAP Fundamentals

17

ACTUAL PARAMETERS (A) are global variables that store the actual values. Actual parameters are defined in the call program FORMAL PARAMETERS (F) are local variable that are defined in the interface of the modularization technique or called program

(1) MACROS:

In macros, the called should always be first and call follows it. Nested macros are possible. If we want to use macros in another program, we have to define call program again in that program. Macros are stored in TRMAC table

(2) SUB-ROUTINES:

USING parameters are input parameters CHANGING parameters are output parameters

Methods of Passing Data

Page 18: ABAP Fundamentals

18

CALL BY REFERENCE: The actual and formal parameters point to the same memory address hence any change to the either of the parameters is reflected in the other parameters. By default all subroutines are call by reference CALL BY VALUE: The actual and formal parameters have the different memory address hence any change to the either of the parameters is not reflected in the other parameters. One can make a parameter or parameters as call by value by adding value key word to the using set of parameters CALL BY VALUE and RESULT: One can make a parameter or parameters as call by value and result by adding value key word to the changing set of parameters

(3) FUNCTION MODULES: (SE37)

FUNCTION MODULE PARAMETERS:

ATTRIBUTES parameters describes what type of function module it is IMPORT parameters are input parameters EXPORT parameters are output parameters CHANGING parameters can be input or output parameters of WA TABLES parameters can be input or output parameters of ITABS EXCEPTIONS: Information is passed to handle runtime errors and these are generally negative SOURCE CODE: In this we write code for the function module

Page 19: ABAP Fundamentals

19

THERE ARE 3 TYPES OF FUNCTION MODULES (1) NORMAL (2) REMOTE-ENABLED (3) UPDATE SY-SUBRC EQ 0: The function module is executed successfully SY-SUBRC EQ 1: The function module is not executed due to the exception number 1 Function pool = Collection of function modules STEP TO IMPLEMENT STANDARD FUNCTION MODULES: (1) Identify the FM using search helps that will give one or more function modules. Study the documentation provided and select which is the closest to our Requirement (2) Understand the parameters of the interface of the FM (3) Test the function module that will again help us identifying whether this FM will suit our requirement or not (4) Finally call the function module into the program

(4) CLASSES:

These are based on object oriented programming concepts. Classes created locally using ABAP editor (SE38) are available to that program only and Classes created globally using class builder (SE24) will be available to all the programs

(5) INCLUDES:

It is used to hold another programs code that is subroutines, function modules

Page 20: ABAP Fundamentals

20

LAB EXERCISES:

1. Write a program to demonstrate macros 2. Write a program to demonstrate subroutines with the following

methods i. Call by reference ii. Call by value iii. Call by value and result

3. Write a program to demonstrate a function module 4. Create two function modules, First function module with I_DEPID

and T_EMP parameters and Second function module with I_EMPID and C_EMP parameters, Fetch data into first function module from the employee database table. Using first function module data, give EMP details from second first function module

INTERVIEW QUESTIONS:

1. Differences among various Modularization Techniques. 2. What is a Function Group 3. What are exceptions in Function Module 4. Types of Function Modules 5. Name Few Function Modules. 6. How do you call a subroutine from another program?

Page 21: ABAP Fundamentals

21

MESSAGES

Topic 5: MESSAGES

Messages are created in SE91 and stored in table T100. Messages can either be displayed in modal dialog boxes, or in the status bar of the screen. You can send messages using the ABAP statement MESSAGE. The statement specifies the message class, number, and type of the message. MESSAGE TYPES���� E � ERROR W � WARNING A � ABEND S � SUCCESS I � INFORMATION X � SHORT DUMP MESSAGE Class���� (ZHNC) MESSAGE Number���� (FROM 000 TO 999) Example: message s220(ZHNC) with text. &���� PLACE HOLDERS that means we can use the same message number with different messages dynamically at runtime. Example: message s220(ZHNC) with ‘ Enter Vendor Number ’