BC411 Advanced ABAP Programming
BC411
R/3 System Release 46B 24.06.2002
BC411 Advanced ABAP Programming ... 0-1 Copyright... 0-2 R/3 Integration Model ... 0-3 ABAP Workbench... 0-4 Prerequisites ... 0-5 Target Group ... 0-6 Introduction: Contents ... 1-1 Course Goal ... 1-2 Course Objectives ... 1-3 Contents I ... 1-4 Contents II ... 1-5 Overview Diagram ... 1-6 Overall Business Scenario... 1-7 Development Process and Tools: Contents ... 2-1 Unit Objectives ... 2-2 Development Process for an ABAP Program ... 2-3 Types of Program Analysis... 2-4 Syntax Check ... 2-5 Syntax Check ... 2-6 Syntax Errors... 2-7 Runtime Errors ... 2-8 Short Dump Administration... 2-9 Dynamic Analysis ... 2-10 Debugging Mode ... 2-11 The Most Important Debugging Functions ... 2-12 The Most Important Debugging Functions II ... 2-13 Debugger: Overview... 2-14 Expanding Substructures... 2-15 Breakpoints in Programs ... 2-16 Breakpoints in the Debugger ... 2-17 Watchpoints... 2-18 Runtime Measurement ... 2-19 Runtime Analysis I... 2-20 Runtime Analysis II... 2-21 Measurable Functions I ... 2-22 Measurable Functions II ... 2-23 Gross and Net Times... 2-24 The GET RUN TIME Statement... 2-25 Test Frame I ... 2-26 Test Frame II ... 2-27 Test Frame III ... 2-28 Test Frame IV... 2-29 Database Access... 2-30
SQL Trace ... 2-31 SQL Trace - Basic List... 2-32 SQL Trace Function: Explain SQL... 2-33 Index Range Scan/Access by Rowid ... 2-34 Summary ... 2-35 Types: Contents ... 3-1 Unit Objectives ... 3-2 Data Types ... 3-3 Predefined ABAP Types ... 3-4 Predefined Dictionary Types I... 3-5 Predefined Dictionary Types (2) ... 3-6 Calculations Using Numeric Types ... 3-7 Summary of Declarative Statements ... 3-8 Type Conversion ... 3-9 Conversion Rules for Elementary Types ... 3-10 Conversion Rule for Structure types ... 3-11 Information About an Object at Runtime ... 3-12 Summary ... 3-13 Field Symbols: Contents ... 4-1 Unit Objectives ... 4-2 Pointers and Field Symbols ... 4-3 Reference and Value Semantics ... 4-4 Using Field Symbols - Overview ... 4-5 Using Field Symbols (1)... 4-6 Using Field Symbols (2)... 4-7 Assigning Parts of Strings Dynamically ... 4-8 Processing Individual Characters in a String ... 4-9 Dynamic Field Assignment ... 4-10 Referring to Components of a Structure ... 4-11 Changing the Field Type... 4-12 Memory Protection ... 4-13 Summary ... 4-14 Internal Tables: Contents ... 5-1 Unit Objectives ... 5-2 Internal Tables... 5-3 Internal Table Attributes... 5-4 Table Types... 5-5 Response Times ... 5-6 Access Times ... 5-7 Defining Internal Tables... 5-8 Internal Tables in Memory (1) ... 5-9 Internal Tables in Memory (2) ... 5-10 Internal Tables in Memory (3) ... 5-11 Internal Tables in Memory (4) ... 5-12 Internal Tables in Memory (5) ... 5-13
Internal Tables in Memory (6) ... 5-14 Special Internal Table Operations... 5-15 Operations on all Data Objects ... 5-16 Index Access Using Read... 5-17 Index Operations on Internal Tables... 5-18 Linear Index (1) ... 5-19 Linear Index (2) ... 5-20 Linear Index (3) ... 5-21 Accessing a Single Generic Entry with READ ... 5-22 Generic Key Operations ... 5-23 Operations on Standard Tables (Example)... 5-24 Internal Tables With and Without Header Line ... 5-25 Declaring an Internal Table With Header Line ... 5-26 Sorted Table (Example)... 5-27 Partial Sequential Processing: Sorted Tables... 5-28 Hashed Tables (Example) ... 5-29 Filling an Internal Table with Cumulative Values ... 5-30 Summary of Internal Table Operations ... 5-31 Standard Table Definitions (History) ... 5-32 Summary ... 5-33 Object-Oriented Programming with ABAP Objects: Contents ... 6-1 Objectives... 6-2 What are Objects?... 6-3 Objects ... 6-4 Encapsulation ... 6-5 Advantages of Object-Oriented Programming ... 6-6 From Function Groups to Objects I... 6-7 From Function Groups to Objects II... 6-8 Classes and Objects... 6-9 What is ABAP Objects? ... 6-10 Classes I... 6-11 Classes II... 6-12 Declaring Attributes ... 6-13 Static Attributes ... 6-14 References: Reference Variables ... 6-15 References: Creating Objects... 6-16 References: Assigning References... 6-17 References: Garbage Collection... 6-18 Classes and Instances: Example... 6-19 Declaring Methods... 6-20 Implementing Methods ... 6-21 Calling Methods... 6-22 Constructor ... 6-23 Interfaces... 6-24 Interfaces: Model ... 6-25
Defining Interfaces... 6-26 Implementing Interfaces ... 6-27 Generic Programming Using Interface References... 6-28 Interfaces - Example... 6-29 Events ... 6-30 Events ... 6-31 Defining Events ... 6-32 Defining an Event Handler... 6-33 Registering and Triggering Events... 6-34 Events: Handler Table ... 6-35 Summary ... 6-36 Preview: Inheritance ... 6-37 Contexts: Contents... 7-1 Unit Objectives ... 7-2 What is a Context? ... 7-3 Using Contexts ... 7-4 Structure of a Context... 7-5 Testing Contexts... 7-6 Derivation Schema of a Context ... 7-7 Buffering Contexts ... 7-8 Context Buffer and Context Instances ... 7-9 Creating a Context Instance ... 7-10 Supplying a Context Instance with Key Values I ... 7-11 Supplying a Context Instance with Key Values II ... 7-12 Retrieving Data from a Context Instance I ... 7-13 Retrieving Data from a Context Instance II ... 7-14 Message Handling in Contexts: System ... 7-15 Message Handling in Contexts: Program... 7-16 Clearing the Context Buffer ... 7-17 Creating and Maintaining Contexts... 7-18 Summary I ... 7-19 Summary II ... 7-20 ABAP Open SQL: Inhalt ... 8-1 Unit Objectives ... 8-2 ABAP Open SQL: Overview ... 8-3 Business Transaction Performance ... 8-4 ABAP Open SQL: Overview ... 8-5 R/3 Architecture... 8-6 General DBMS Architecture ... 8-7 The Optimizer ... 8-8 ABAP Open SQL: Overview ... 8-9 ABAP SQL... 8-10 From SQL Statement to Result Set ... 8-11 Efficient ABAP Database Programming... 8-12 Five Rules of Open SQL Programming ... 8-13
Basis for the Five Rules... 8-14 Rule 1 ... 8-15 Exercise: Read all Flights for a Year... 8-16 1. Keep the Result Set Small ... 8-17 Benefits of Rule 1 ... 8-18 Rule 2 ... 8-19 Exercise: Reading the First 10 Flights ... 8-20 2. Minimize Data Transfer I... 8-21 2. Minimize Data Transfer... 8-22 Client/Server Architecture and ABAP Runtime I ... 8-23 Client/Server Architecture and ABAP Runtime II ... 8-24 2. Minimize Data Transfer II... 8-25 Exercise: Summation... 8-26 2. Minimize Data Transfer 4... 8-27 2. Minimize Data Transfer 5... 8-28 Benefits of Rule 2 ... 8-29 Rule 3 ... 8-30 3. Minimize the Number of Transfers 1 ... 8-31 Exercise: Reading from More than One Table... 8-32 3. Minimize the Number of Transfers 2 ... 8-33 3. Minimize the Number of Transfers 3 ... 8-34 3. Minimize the Number of Transfers 4 ... 8-35 3. Minimize the Number of Transfers 5 ... 8-36 3. Minimize the Number of Transfers 6 ... 8-37 3. Minimize the Number of Transfers 7 ... 8-38 Exercise: Reading Maximum and Minimum Values ... 8-39 3. Minimize the Number of Transfers 8 ... 8-40 Exercise: Reading Grouped Data with Conditions ... 8-41 3. Minimize the Number of Transfers 9 ... 8-42 Benefits of Rule 3 ... 8-43 Rule 4 ... 8-44 4. Minimize the Search Overhead 1... 8-45 4. Minimize the Search Overhead 2... 8-46 4. Minimize the Search Overhead 3... 8-47 4. Minimize the Search Overhead 4... 8-48 4. Minimize the Search Overhead 5... 8-49 4. Minimize the Search Overhead 6... 8-50 4. Minimize the Search Overhead 7... 8-51 4. Minimize the Search Overhead 8... 8-52 Rule 5 ... 8-53 5. Reduce the Database Load 1 ... 8-54 5. Reduce the Database Load 2 ... 8-55 5. Reduce the Database Load 3 ... 8-56 5. Reduce the Database Load 4 ... 8-57 5. Reduce the Database Load 5 ... 8-58
5. Reduce the Database Load 6 ... 8-59 Synchronizing the buffer II ... 8-60 Synchronizing the buffer III ... 8-61 5. Reduce the Database Load 9 ... 8-62 When should you buffer tables? ... 8-63 Buffering tables... 8-64 SQL statements that bypass the buffer... 8-65 5. Reduce the Database Load 13 ... 8-66 5. Reduce the Database Load 14 ... 8-67 5. Reduce the Database Load 15 ... 8-68 5. Reduce the Database Load 16 ... 8-69 ABAP Tuning Checklist - I ... 8-70 ABAP Tuning Checklist - II ... 8-71 ABAP Tuning Checklist - III ... 8-72 Summary I ... 8-73 Summary II ... 8-74 Summary III ... 8-75 Exceptions and Message Handling: Contents ... 9-1 Unit Objectives ... 9-2 Exceptions... 9-3 Catching Runtime Errors ... 9-4 System Exceptions ... 9-5 Nested CATCH… ENDCATCH Structures ... 9-6 Summary ... 9-7 ABAP Modularization Units ... 10-1 Unit Objectives ... 10-2 Modularization Units ... 10-3 Memory Areas: Logical Model I ... 10-4 Memory Areas: Logical Model II ... 10-5 Visibility Area of a Program ... 10-6 Internal Session... 10-7 Table Work Areas / Common Parts ... 10-8 The Dangers of Working with Common Data Areas, Call Hierarchies I ... 10-9 The Dangers of Working with Common Data Areas, Call Hierarchies II ... 10-10 Subroutines, Function Modules, and Methods... 10-11 Calling a Function Module, Method, or External Subroutine ... 10-12 Interfaces in Subroutines ... 10-13 Methods for Passing Parameters... 10-14 Interfaces in Function Modules / Methods ... 10-15 Screens, Modules, and Events ... 10-16 Interfaces of Screens, Modules, and Events... 10-17 Screen/List/Selection Screen... 10-18 CALL SCREEN... 10-19 Implicit Exit / LEAVE SCREEN ... 10-20 List System... 10-21
Several List Systems ... 10-22 Several List Systems - Example I ... 10-23 Several List Systems - Example II ... 10-24 LEAVE TO SCREEN ... 10-25 CALL SELECTION-SCREEN ... 10-26 Transaction / Program ... 10-27 CALL TRANSACTION ‘TA01’ /SUBMIT PROG01 AND RETURN ... 10-28 Implicit Exit / LEAVE PROGRAM... 10-29 LEAVE TO TRANSACTION ‘TA01’ ... 10-30 SUBMIT PROG01 ... 10-31 Context ... 10-32 Structuring Source Code ... 10-33 Include Programs ... 10-34 Macros... 10-35 Summary ... 10-36 Dynamic Program Generation: Contents... 11-1 Unit Objectives ... 11-2 Dynamic ABAP Programming... 11-3 Dynamic Programming - Examples ... 11-4 Specifying Names at Runtime ... 11-5 Generating Programs from Within Progams ... 11-6 Generating Temporary Programs ... 11-7 Generating Persistent Programs... 11-8 Summary ... 11-9 Appendix ... 12-1 Further Information about Internal Tables and Extracts ... 12-2 Internal Tables and Extracts (1)... 12-3 Internal Tables and Extracts (2)... 12-4 Internal Tables and Extracts (3)... 12-5 Accessing Table Entries Directly (Release 4.5) Contents ... 13-1 Structure of Deep Tables... 13-2 Accessing Table Entries Directly (Release 4.5A)... 13-3 Further Information About ABAP Open SQL: Contents ... 14-1 Rule 3: Minimize the Number of Transfers 1... 14-2 Rule 3: Minimize the Number of Transfers 2... 14-3 Rule 4: Minimize the Search Overhead ... 14-4 Rule 4: Minimize the Search Overhead ... 14-5 Rule 5: Reduce the Database Load 1... 14-6 Rule 5: Reduce the Database Load... 14-7 Further Information About Modularization Units: Contents ... 15-1 Comparison Criteria I... 15-2 Comparison Criteria II... 15-3 Comparison Criteria III... 15-4 Internal Subroutine I ... 15-5 Internal Subroutine II ... 15-6
Internal Subroutine III ... 15-7 External Subroutine I ... 15-8 External Subroutine II ... 15-9 External Subroutine III ... 15-10 Function Module I ... 15-11 Function Module II ... 15-12 Function Module III ... 15-13 Screen, CALL SCREEN, CALL SELECTION-SCREEN I ... 15-14 Screen, CALL SCREEN, CALL SELECTION-SCREEN II ... 15-15 Screen, CALL SCREEN, CALL SELECTION-SCREEN III ... 15-16 Module I... 15-17 Module II... 15-18 Module III... 15-19 Event I ... 15-20 Event II ... 15-21 Event III ... 15-22 Dialog Module I... 15-23 Dialog Module II... 15-24 Dialog Module III... 15-25 Transaction I... 15-26 Transaction II... 15-27 Transaction III... 15-28 Program, SUBMIT I ... 15-29 Program, SUBMIT II ... 15-30 Program, SUBMIT III ... 15-31 LEAVE TO LIST PROCESSING I... 15-32 LEAVE TO LIST PROCESSING II... 15-33 LEAVE TO LIST PROCESSING III... 15-34 String Processing: Contents... 16-1 Unit Objectives ... 16-2 String Processing ... 16-3 Searching in a String ... 16-4 Manipulating Strings ... 16-5 Joining and Splitting Strings ... 16-6 String Comparisons ... 16-7 Summary ... 16-8 Bit Processing: Contents ... 17-1 Unit Objectives ... 17-2 Sets as Bit Patterns ... 17-3 SET BIT and GET BIT ... 17-4 Assigning Values to X Fields ... 17-5 Bit Calculations... 17-6 Bit Comparisons ... 17-7 Example: Representing Flights with an X Field... 17-8 Summary ... 17-9
Data Clusters: Contents ... 18-1 Unit Objectives ... 18-2 Data Clusters: Overview ... 18-3 Syntax: EXPORT to a Cluster Database ... 18-4 Syntax: IMPORT and DELETE ... 18-5 Key of the Database Table INDX... 18-6 ABAP Cluster Databases... 18-7 Example: Catalog for INDX... 18-8 ABAP Memory - Overview: Communication Between Internal Sessions ... 18-9 Transporting Data Between Programs (ABAP Memory) ... 18-10 Shared Buffer - Overview ... 18-11 Cluster Tables and Transparent Tables... 18-12 Summary ... 18-13 Exercises ... 19-1 Data used in the exercises: ... 19-2 - ... 19-6 - ... 19-7 - ... 19-9 - ... 19-13 - ... 19-24 - ... 19-26 - ... 19-33 - ... 19-34 Solutions ... 20-1 - ... 20-2 - ... 20-6 - ... 20-8 - ... 20-12 - ... 20-17 - ... 20-40 - ... 20-45 - ... 20-57 - ... 20-59 Glossary... 21-1 ABAP Glossary... 21-2
©SAP AG 1999
BC411
Advanced ABAP Programming
Advanced ABAP Programming
©SAP AG
BC 411
BC 411
Advanced ABAP
Advanced ABAP
Programming
Programming
BC 411 Advanced ABAP Programming Release 4.0 B
Material number: 5002 0252 August 1998
©SAP AG
R
Copyright
Copyright 1998 SAP AG. All rights reserved.
Neither this training manual nor any part thereof may
be copied or reproduced in any form or by any means,
or translated into another language, without the prior
consent of SAP AG. The information contained in this
document is subject to change and supplement
without prior notice.
All rights reserved.
• Trademarks
SAP and the SAP logo are registered trademarks of SAP AG. MS-DOS and EXCEL are registered trademarks of Microsoft.
OS/2, CICS, MVS, ACF/VTAM, VSE, AIX, OS/400 and AS/400 are registered trademarks of IBM.
X Window System is a registered trademark of MIT University. SINIX, UTM and BS2000 are registered trademarks of Siemens. UNIX is a registered trademark of AT&T.
HP-UX is a registered trademark of Hewlett Packard.
DECnet, DECstation, DECsystem, VAXstation and VMS are registered trademarks of DEC. Ethernet is a registered trademark of the Xerox Corporation.
ORACLE SQL*net, SQL*+ PRO*C are registered trademarks of the ORACLE Corporation. INFORMIX-OnLine and INFORMIX-ESQL/C are registered trademarks of the INFORMIX Corporation.
©SAP AG R
R/3 Integration Model
R/3
R/3
Client/Server
ABAP
Client/Server
ABAP
CO Controlling CO Controlling AA Asset Accounting AA Asset Accounting PS Project System PS Project System WF Workflow WF Workflow IS Industry Solutions IS Industry Solutions MM Materials Mgmt MM Materials Mgmt HR Human Resources HR Human Resources SD Sales & Distribution SD Sales & Distribution PP Production Planning PP Production Planning QM Quality Mgmt QM Quality Mgmt FI Financial Accounting FI Financial Accounting PM Plant Maintenance PM Plant MaintenanceThe SAP R/3 System continues to set new standards for universal standard software. Based on progressive development techniques, the SAP R/3 System allows comprehensive integration of business administration and data processing.
The SAP R/3 System components are characterized by comprehensive business functions with the most modern technology. The integration of the applications ensures that all the functions are directly available in the system and thus in the enterprise.
©SAP AG R
ABAP Workbench
Level 3
BC420 2 days Interface Programming for Data TransferBC430 2 days ABAP Dictionary BC405 5 days Techniques of List Processing BC410 5 days Transaction Processing BC415 3 days Communication Interface Programming in ABAP BC460 3 days SAPscript: Forms Design and Text Management in R/3 BC425 2 days Enhancements and Modifications BC440 5 days Developing Internet Application Components (IAC) BC411 5 days Advanced ABAP Programming
Level 2
BC400 5 days ABAP Workbench Basics©SAP AG
R
Prerequisites
Required:
BC400 Introduction to the ABAP Workbench
BC410 Transaction programming
ABAP programming experience
Recommended:
BC405 Techniques of List Processing
BC430 ABAP Dictionary
©SAP AG
R
Target Group
Project team
Technical developers and application programmers
This course is aimed at experienced ABAP
programmers writing complex programs, who want to
improve their knowledge of ABAP.
Participants will gain hands-on experience of ABAP
programming through the exercises.
Duration: 5 days
Notes to the user
The training materials are not teach-yourself programs. They complement the course
instructor's explanations. On the sheets, there is space for you to write down additional
©SAP AG R
Introduction: Contents
Course goal
Course objectives
Contents
Overview diagram
(C) SAP AG BC411 1-2 ©SAP AG
R
Course Goal
Programming in ABAP in the R/3 System
ABAP language constructions, with
particular emphasis on using them
correctly and in appropriate situations
This course teaches you about :
(C) SAP AG BC411 1-3 ©SAP AG
R
Course Objectives
Description of the development process and
using the development tools
Using types and field symbols
Understanding and using the different kinds of
internal tables
Understanding and using the syntax of ABAP
Objects
Programming exception and message handling
Using ABAP and Open SQL
Using appropriate modularization units and
dynamic programs
(C) SAP AG BC411 1-4 ©SAP AG
R
Contents I
Unit Introduction Unit Contexts
Unit Development process and Unit ABAP Open SQL
development tools
Unit Types Unit Exceptions and message handling
Unit Field symbols Unit Modularization units
Unit Internal tables Unit Generating dynamic programs
Unit Object-oriented programming in ABAP
Unit ABAP Objects
(C) SAP AG BC411 1-5 ©SAP AG
R
Contents II
Appendix A Further information about internal tables and extracts Appendix B Accessing table lines directly (from Release 4.5A) Appendix C Further information about ABAP Open SQL Appendix D Further information about modularization units Appendix E String processing
Appendix F Bit processing Appendix G Data clusters
Exercises Solutions Glossary
(C) SAP AG BC411 1-6 ©SAP AG
R
Overview Diagram
Complex problems:
What ABAP constructions
can I use to solve the
problem?
(C) SAP AG BC411 1-7 ©SAP AG
R
Overall Business Scenario
This course contains no overall scenario
that is successively developed by the
participants
©SAP AG
R
Development Process and Tools: Contents
Development process for an ABAP program
Static checks
Syntax check
Extended program check
Runtime errors
Debugger
Performance tools
ABAP runtime analysis GET RUN TIME SQL TRACE
(C) SAP AG BC411 2-2 ©SAP AG
R
Unit Objectives
At the end of this unit, you will be able to: Explain the development process for an ABAP program
Check the syntax of a program
Run an extended program check for a program Analyze a short dump
Use the most important functions of the Debugger Determine the flow and runtime of a program using runtime analysis
Measure runtime using the GET RUN TIME statement Analyze database access using the main functions of
(C) SAP AG BC411 2-3 ©SAP AG
R
Development Process for an ABAP Program
TRDIR TRDIR attributes attributes Program ABAP Development Source code Source code Syntax check Extended program check
Syntax check Extended program check
Errors Warnings
Load
Generated form(C) SAP AG BC411 2-4 ©SAP AG
R
Types of Program Analysis
Error Error Static analysis Static analysis Warning Warning Syntax check Syntax check Database access Database access Dynamic analysis Dynamic
analysis Memory contentsMemory contents
General memory allocation General memory allocation
Program interruption Program interruption
Debugging Debugging Dump analysis
Dump analysis Runtime errorRuntime error
Extended program check
Extended program check Error Error Warning Warning Message Message SQL Trace SQL Trace Program flow Program flow Runtime measurement Runtime measurement Memory requirements Memory requirements ABAP runtime analysis ABAP runtime analysis
In the static check, the system checks both the syntax of a program and its semantics (for example, whether the number and type of parameters passed to a subroutine is correct).
The dump analysis allows you to investigate runtime errors. Dynamic program analysis occurs while the program is running.
Runtime analysis allows you to analyze the program flow, its runtime, and the memory it requires. You can analyze SQL statements using both the runtime analysis and the SQL Trace tool.
(C) SAP AG BC411 2-5 ©SAP AG
R
Syntax Check
*& REPORT sapsyntaxcheck. INCLUDE syntaxchecktop. INCLUDE syntaxcheckf01. *& REPORT sapsyntaxcheck. INCLUDE syntaxchecktop. INCLUDE syntaxcheckf01.
*INCLUDE SYNTAXCHECKF01
PERFORM subprog.
*Form Routine not defined *in program.
*INCLUDE SYNTAXCHECKF01
PERFORM subprog.
*Form Routine not defined *in program.
*& INCLUDE SYNTAXCHECKTOP. REPORT syntaxcheck.
*& INCLUDE SYNTAXCHECKTOP. REPORT syntaxcheck.
Syntax error
Syntax error in: SYNTAXCHECKF01 Line 5
PERFORM SUBPROGRAM.
The form "SUBPROGRAM" does not exist, but there is a FORM with the similar name "SUBPROG"
Edit Correct error
When you check the syntax of an include, the system combines the current contents of the Editor with the contents of the TOP include to form a unit whose syntax and semantics can be checked sensibly. If an error occurs, the system interrupts the check and generates an error message, often with a proposed correction that you can adopt by choosing 'Correct error'. The system then restarts the syntax check.
The system only checks all includes for completeness and consistency (for example, to ensure that all subroutines that you call are actually defined) when you run the syntax check for the main program.
(C) SAP AG BC411 2-6 ©SAP AG
R
Syntax Check
*& INCLUDE SAPBC411D_SYNTAXCHECKF01.
PERFORM subprogram. FORM subprogram. SELECT ... ENDSELECT. ENDFORM. PERFORM u_pgm.
*& INCLUDE SAPBC411D_SYNTAXCHECKF01.
PERFORM subprogram. FORM subprogram. SELECT ... ENDSELECT. ENDFORM. PERFORM u_pgm. Syntax warning:
Statement (PERFORM u_pgm) is not accessible.
The syntax check may display warnings. The check does not end when a warning occurs, and the program can, in principle, be generated. However, as a programmer, you should always correct syntax warnings, since they can often lead to unforeseeable errors.
(C) SAP AG BC411 2-7 ©SAP AG R
Syntax Errors
Extended program check Extended program checkWarnings from the syntax check
Warnings from the syntax check
PERFORM/FORM interfaces
PERFORM/FORM interfaces
CALL FUNCTION interfaces
CALL FUNCTION interfaces
Screen consistency
Screen consistency
STATUS and TITLEBAR
STATUS and TITLEBAR
Strings
Strings
Field attributes
Field attributes
Conversions
Conversions
The standard syntax check leaves out many checks to save runtime. Only the extended program check provides a complete examination of your program that includes the interfaces to other programs that it calls.
Errors in the extended program check (other than inaccessible coding) cause runtime errors, and must therefore be corrected. You should also correct warnings. If warnings occur that you want to ignore (for example, they occur because of dynamic calls in your program), you can use pseudo-comments to suppress them ("+EC*). For further information, refer to the extended help for the extended program check transaction.
As a programmer, you should always run the extended program check as part of your development process. A program is only finished once you have corrected all of the errors and warnings (although a few non-critical messages may remain).
The extended syntax check, like the standard check, is a static check. This means that runtime errors may still occur in your program. For example, if your program contains the statement CALL FUNCTION field, the static check cannot tell whether the value of field corresponds to a valid function module name. However, if you write CALL FUNCTION 'JOHN_SMITH', the extended program check will look to see if the function module 'JOHN_SMITH' exists.
(C) SAP AG BC411 2-8 ©SAP AG R
Runtime Errors
Runtime errors Runtime errors What happened? What happened? What can you do? What can you do? Error analysis Error analysisSystem environment System environment How to correct the error How to correct the error
Info: Where error occurred Info: Where error occurred
Chosen variables Chosen variables Active calls/ events Active calls/ events
... ... Overview Debugger Internal errors Internal errors Installation and environment/resource errors Installation and environment/resource errors Errors in application programs Errors in application programs Short text Short text
There is a range of errors that can occur during an ABAP program that the runtime system
recognizes. When one occurs, the program terminates, and the system generates and displays a short dump.
The short dump is divided into various blocks that document the error. You can use the overview to find out exactly what categories of information are available, such as the contents of data objects, active calls, control structures, and so on.
There are various categories of error: Internal errors
The kernel has recognized an error. In this case, inform SAP. Installation and resource errors
An error occurred due to incorrect system installation or missing resources (for example, database not available). Errors in application programs. Typical causes include: Contents of a type P field not in packed format
Arithmetic overflow
(C) SAP AG BC411 2-9 ©SAP AG R
Dump Analysis
Dump Analysis
Material Management Overview Statistics Display Print Keep Release Reorganization ReorganizationShort Dump Administration
ST22 is the transaction for short dump administration.
By default, short dumps are retained in the system for 14 days. However, you can retain a short dump indefinitely using the Keep function.
You can use the Reorganize function to delete all short dumps older than a certain date.
If you encounter problems in ABAP programs that you cannot solve yourself, you can print out the short dump and send it to SAP. The information it contains is important for the SAP Hotline and remote consulting services.
(C) SAP AG BC411 2-10 ©SAP AG R
Dynamic Analysis
Dynamic analysis Debugging SQL Trace ABAP runtime analysisDynamic analysis occurs while a program is running.
The runtime analysis utility provides an overview of your ABAP program. The Call hierarchy function shows you the sequence in which the individual program steps (subroutines and other modularization units, input and output statements) are processed.
You can use the SQL Trace to analyze database accesses.
In the Debugger, you can use the Calls function to display the sequence in which subroutines are called.
(C) SAP AG BC411 2-11 ©SAP AG R
Debugging Mode
BREAK-POINT <value>. BREAK-POINT <value>. 1. Program statement Repository Browser Repository Browser Program ? ?Goto System Help Edit
Test/Execute SAPBC411 SAP Training: BC411
Object type development class Programs Context_I_C_GEOGR_DISTANCE Context_I_C_GEOGR_DISTANCE BREAK-POINT <Value>. BREAK-POINT <Value>. 1. Program statement ABAP Editor: Initial Screen
ABAP Editor: Initial Screen
Program Edit Goto System Help
Debugging SAPBC411D_TEST Program Create Execute Sub-objects Source code Variants Attributes Documentation /H ... ... Utilities > ... Debugging ...
There are four different ways of starting the Debugger: - From the Repository Browser, by choosing Test/Execute. - From the ABAP Editor by choosing Debugging.
- From a transaction, by entering /h in the command field. - From any screen by choosing System -> Utilities -> Debugging. - By setting breakpoints in the source code and starting the program.
(C) SAP AG BC411 2-12 ©SAP AG
R
The Most Important Debugging Functions
Single step Continue Breakpoint Hexadecimal display Replace function
Processes the next line of the program.
Continues processing until the next breakpoint or the end of the program occurs.
Displays a screen on which you can set breakpoints.
Use this button to display the field contents in
hexadecimal form.
Allows you to change the contents of a field.
Processes the next line of the program.
Continues processing until the next breakpoint or the end of the program occurs.
Displays a screen on which you can set breakpoints.
Use this button to display the field contents in
hexadecimal form.
Allows you to change the contents of a field.
In the Debugger, you can use the scroll buttons to scroll through the program code. The Breakpoint function allows you to define points at which your program is interrupted. The Hexadecimal display button allows you to toggle between hex and character display.
You can change the value of a field in the field display during a debugging session. To do this, enter the new contents in the field contents column of the display and choose 'Change field contents'. The system copies the new value into the contents of the field.
(C) SAP AG BC411 2-13 ©SAP AG
R
The Most Important Debugging Functions II
Up to five watchpoints linked with relational operators.
You can expand structured fields by double-clicking.
You can display complex internal tables and expand them by double-clicking.
Up to five watchpoints linked with relational operators.
You can expand structured fields by double-clicking.
You can display complex internal tables and expand them by double-clicking. Pushbutton for expanding substructures Displaying complex internal tables Watchpoint
You can create watchpoints in the Debugger. These allow you to interrupt your program when the contents of a particular field change. You can link watchpoints using logical expressions containing either constant values or field contents.
You can expand a data object that contains a structure as one of its components by double-clicking it. The Debugger also allows you to display complex internal tables.
(C) SAP AG BC411 2-14 ©SAP AG R
Debugger: Overview
SAP R/3 Debugging ? ? BezeichnungenGoto Breakpoint Settings Development System Help
Main program Test Source code of Test start-of-selection Field names carrid Field contents Crew-Flight Table[2x272] Fldate 19980810 Crew ********************************************
Select Carrid From Spfli
Crew_Wa-Function = 'PILOT'. Crew_Wa-Flight = '0400'. Move-Corresponding Crew_Wa To Flight_Table-Crew. Append Flight_Table-Crew. Break-point. Form f. Double-click 2 - 5 77 Watchpoint
Fields Table Watchpoint Calls
-Fixed point arith.
SY-SUBRC= 0 SY-TABIX= 2 SY-DBCNT= 30 LH
Breakpoint
68 77
In debugging mode, the screen is divided into two areas. In the upper area, the system displays the program code. The lower area contains different information about the source code, depending on the settings that you have chosen. You can change the information displayed using the pushbuttons in the application toolbar.
You can expand the amount of source code displayed by choosing Increase prog. section size. You can move a line to the top of the displayed area by double-clicking it.
You can display the statement that is currently being processed by choosing Display current statement. This is particularly useful when you are processing internal tables.
You can display the contents of up to eight fields in the field display.
The bottom line of the Debugger screen displays the three most important system fields and their contents.
(C) SAP AG BC411 2-15 ©SAP AG R
Expanding Substructures
Structured field No LengthComponent name Type Length Contents
1 Carrid C 3 LH 2 Connid N 4 3 Crew h 8 4 Crew-Name C 30 Miller 4 5 6 7 Component name 272 FLIGHT_TABLE Crew-Function Crew-Flight Crew-Flight-No1 C h N 20 4 Crew-Flight-No2 8 Fldate 19980509 0400 Pilot 0400 0402 N 4 D 8
To display a complex structure, choose Goto -> Structured field.
You can navigate deeper into the structure by double-clicking an individual component. The system displays the length of the structured field, along with the name, type, length, and contents of its components.
(C) SAP AG BC411 2-16 ©SAP AG R
Breakpoints in Programs
BREAK-POINT <value>. BREAK-POINT <value>. 1. ABAP statement Set Delete Display breakpoint Set Delete Display breakpoint 2. Editor function BREAK-POINT <value>. BREAK-POINT <value>. 2. Function Breakpoints BREAKPOINTS anzeigen setzen löschen BREAKPOINTS anzeigen setzen löschen SAP R/3 SAP R/3 Program ? ? BezeichnungenGoto System Help Edit
1 PROGRAM sapbc411d_debugger. 2 INCLUDE bc411d_debuggertop.
3 SELECT carrid connid FROM sflight INTO
4 CORRESPONDING FIELDS OF TABLE itab
5 WHERE carrid = 'LH' AND connid = '0400'. 6 BREAK-POINT 1.
7 LOOP AT itab.
8 WRITE: / itab-carrid, itab-connid. 9 ENDLOOP.
10
Stop
There are various ways of setting breakpoints in programs. From the Editor:
In the ABAP Editor, you can set breakpoints before you even start the program, either by using the BREAK-POINT statement, or by choosing Utilities → Set Breakpoint. This sets line-oriented breakpoints. The lines for which they are set are marked with STOP symbols. You can also use the second method to set breakpoints in display mode.
You can also set breakpoints that are based on field contents (watchpoints). For further information, see Watchpoints.
(C) SAP AG BC411 2-17 ©SAP AG
R
Breakpoints in the Debugger
Breakpoints
No Program Line Statement/Event/... Counter
2 SAPBC411D_DEBUG 10 start-of-selection 3 SAPBC411D_DEBUG 70 select 4 5 6 7 8 9 10 1 SAPBC411D_DEBUG 1 append Stop Stop Stop
Breakpoints in the Debugger
- To set a breakpoint within the Debugger, position the cursor on a line of code and double-click. A stop sign appears next to the lines for which a breakpoint has been set.
As well as setting line-oriented breakpoints, you can also set statement-oriented breakpoints in the Debugger. The Breakpoint menu allows you to set breakpoints at ABAP keywords, events, subroutines, function modules, and system exceptions.
You can display an overview of the breakpoints that you have set by choosing Breakpoints. To delete a breakpoint, position the cursor on it and double-click.
(C) SAP AG BC411 2-18 ©SAP AG
R
Watchpoints
Watchpoints
No Program Field name Comparsion field/value Logical operator between watchpoints
L Op Field 1x SAPBC411D_DEBUG str-c 2x str-i = 3x i > 4 x sy-tabix = 5x str-c <
Current contents of the last watchpoint reached:
SAPBC411D_DEBUG i 4 x sy-index 1 5 or and SAPBC411D_DEBUG SAPBC411D_DEBUG SAPBC411D_DEBUG SAPBC411D_DEBUG
Watchpoints allow you to interrupt your programs either when the contents of a field change or on the basis of a comparison with a constant or the contents of another field.
You can set up to five watchpoints.
You can link watchpoints together using the 'AND' and 'OR' operators.
Note: Debugging in production system can cause data inconsistencies, since the system functions /N
and /I lead to a database commit. Similarly, the system triggers implicit commits in normal mode (see Transaction SM50).
(C) SAP AG BC411 2-19 ©SAP AG
R
Runtime Measurement
Runtime Analysis
GET RUN TIME FIELD <f>.
The runtime analysis utility allows you to analyze how your ABAP program runs. The most important information that it provides is the real time required by the program. The programs that you measure using runtime analysis are not changed by the process.
Note that the runtime required by a program depends on the current system load.
You can measure the runtime of a particular sequence of statements within a program using the GET RUN TIME statement. In this case, you modify the program that you want to measure, because the GET RUN TIME statement has to be inserted in the program code.
(C) SAP AG BC411 2-20 ©SAP AG R
Runtime Analysis I
Runtime analysis Runtime analysis Report: RSDEMO00 Report: RSDEMO00 MeasureMeasure AnalysisAnalysis
Start End
Performance data in file
Dialog List
Analysis
The runtime analysis utility allows you to measure the time required by an ABAP program and store the performance data in a file.
The system profile parameter ABAP/ATRASIZEQUOTA specifies the amount of space allocated in the file system for storing performance data.
For further information about runtime analysis, refer to the ABAP Workbench Tools online documentation.
(C) SAP AG BC411 2-21 ©SAP AG
R
Runtime Analysis II
Analysis screen
Group hit list Overall call hierarchy
Analysis screen Group hit list
Detail selection Call hierarchy Subtree Overall hit list Table hit list Runtime analysis
You can display various analyses, including the entire program, or individual program sections such as subroutines.
(C) SAP AG BC411 2-22 ©SAP AG R ABAP programs ABAP programs
Modularization Units
Modularization Units
Program/Report Program/Report Modules (screens)Modules (screens) MODULEMODULE
Subroutines
Subroutines PERFORMPERFORM
Methods
Methods CALL METHODCALL METHOD
Events
Events RAISE EVENTRAISE EVENT
Function modules
Function modules CALL FUNCTIONCALL FUNCTION
Contexts
Contexts SUPPLY/DEMANDSUPPLY/DEMAND
Reports
Reports SUBMITSUBMIT
Measurable Functions I
ABAP program analysis is based on:
(C) SAP AG BC411 2-23 ©SAP AG R SAP SQL SAP SQL
Database/file access
Database/file access
SELECT/INSERT/UPDATE/ SELECT/INSERT/UPDATE/ DELETE DELETE Direct SQL interfaceDirect SQL interface EXEC SQLEXEC SQL
Data cluster
Data cluster EXPORT/IMPORTEXPORT/IMPORT
Sequential files
Sequential files TRANSFER/READTRANSFER/READ
DATASET
DATASET
Extending
Extending APPEND/COLLECTAPPEND/COLLECT
Processing
Processing LOOPLOOP
Sorting
Sorting SORTSORT
Internal tables
Internal tables
System times
System times
Load/generate programs Load/generate programsMeasurable Functions II
Input/Output statementsStatements for internal table operations and system times
The Call hierarchy function displays a detailed list of measurable functions. The group hit list summarizes these functions into groups. Within the groups, the calls are listed in descending order of gross runtime, allowing you to identify immediately the most expensive call within a group.
(C) SAP AG BC411 2-24 ©SAP AG R PROGRAM SAPBC411D_... SELECT PERFORM SORT
Gross and Net Times
Gross Net . . . . . . . . . . . . . .
Gross time = Total time required to execute the relevant functions
Net time = Gross time less the time required for modularization units and other selected statements. The net time is the time that is not otherwise accounted for.
You can display times either as a percentage, or as absolute times in microseconds.
For further information about the procedure, refer to the online documentation in the ABAP Editor for the SET RUNTIME ANALYZER ON/OFF statement.
(C) SAP AG BC411 2-25 ©SAP AG
R
The GET RUN TIME Statement
GET RUN TIME FIELD <f>.
PROGRAM xxx.
DATA: t1 TYPE i,
t2 TYPE i,
run_time TYPE i.
GET RUN TIME FIELD t1.
GET RUN TIME FIELD t2. run_time = t2 - t1.
Statement block
The first time the GET RUN TIME statement is executed, the field <f> is initialized. Each
subsequent time the statement is executed, the system places the runtime in microseconds since the first call into field <f>.
The measurement results depend on the overall system load, and the system may return differing runtimes for the same block of statements. Time differences can also be caused, for example, by a data record being read from the buffer on one occasion, but from the database another time.
(C) SAP AG BC411 2-26 ©SAP AG R
Test Frame I
REPORT sapbc411d_testframe. TYPES: BEGIN OF result_line, text(60) TYPE c, time TYPE i, END OF result_line. DATA: t1 TYPE i, t2 TYPE i, base_time TYPE i, res_time(8) TYPE p DECIMALS 1, i TYPE i, result TYPE result_line OCCURS 10 WITHHEADER LINE. PARAMETERS:
in_cnt TYPE i DEFAULT 100, out_cnt TYPE i DEFAULT 20.
* Specifc data for the measurement ... REPORT sapbc411d_testframe. TYPES: BEGIN OF result_line, text(60) TYPE c, time TYPE i, END OF result_line. DATA: t1 TYPE i, t2 TYPE i, base_time TYPE i, res_time(8) TYPE p DECIMALS 1, i TYPE i, result TYPE result_line OCCURS 10 WITH
HEADER LINE. PARAMETERS:
in_cnt TYPE i DEFAULT 100, out_cnt TYPE i DEFAULT 20.
* Specifc data for the measurement ...
(C) SAP AG BC411 2-27 ©SAP AG
R
Test Frame II
* Start measurement
PERFORM fill_result USING:
’Base time’, "#EC NOTEXT ’..Variant 1...’, "#EC NOTEXT ’..Variant 2...’. "#EC NOTEXT LOOP AT result.
i = sy-tabix.
result-time = 99999999. DO out_cnt TIMES.
GET RUN TIME FIELD t1. DO in_cnt TIMES.
PERFORM i OF f0 f1 f2. ENDDO.
GET RUN TIME FIELD t2. t2 = t2 - t1.
IF t2 < result-time. result-time = t2. ENDIF.
ENDDO.
* Start measurement
PERFORM fill_result USING:
’Base time’, "#EC NOTEXT
’..Variant 1...’, "#EC NOTEXT
’..Variant 2...’. "#EC NOTEXT
LOOP AT result. i = sy-tabix.
result-time = 99999999. DO out_cnt TIMES.
GET RUN TIME FIELD t1.
DO in_cnt TIMES.
PERFORM i OF f0 f1 f2. ENDDO.
GET RUN TIME FIELD t2.
t2 = t2 - t1.
IF t2 < result-time. result-time = t2. ENDIF.
ENDDO.
You use two loop counters to determine the number of test runs.
If the statement(s) that you want to measure have a very short runtime, choose a high value for the inner loop (for example, the statement MOVE c1 to c2 has a shorter runtime than the
measurement accuracy of 1µs. In this case, you would run the statement several times and divide the total runtime by the number of loop passes).
You use the outer loop to determine the number of test runs, from which you then take the minimum runtime. This process should eliminate factors, such as changing work process, that can make individual measurements inaccurate.
(C) SAP AG BC411 2-28 ©SAP AG
R
Test Frame III
IF i = 1.
base_time = result-time. ELSE.
SUBTRACT base_time FROM result-time. ENDIF.
MODIFY result. ENDLOOP.
LOOP AT result.
t1 = result-time / in_cnt. WRITE: / result-text, res_time. ENDLOOP.
IF i = 1.
base_time = result-time. ELSE.
SUBTRACT base_time FROM result-time. ENDIF.
MODIFY result. ENDLOOP.
LOOP AT result.
t1 = result-time / in_cnt. WRITE: / result-text, res_time. ENDLOOP.
The field base_time is the time that the system needs to call the 'control' FORM routine f0. This routine should always either be left empty or contain statements that are processed in every form routine, but do not belong to the statements that you want to measure.
(C) SAP AG BC411 2-29 ©SAP AG
R
Test Frame IV
*& FORM FILL_RESULT
FORM fill_result USING text. result-text = text. APPEND result. ENDFORM. *& FORM F0 FORM f0. ... ENDFORM.
*& FORM F1 Variant 1 FORM f1.
ENDFORM.
Statements for variant 1
*& FORM F2 Variant 2 FORM f2.
ENDFORM.
Statements for variant 2
In the subroutines f1 and f2, program the variants whose runtime you want to compare. Subroutine f0 can contain statements that are common to both variants (initializiation, closing processing, and so on).
(C) SAP AG BC411 2-30 ©SAP AG R
Database Access
ABAP Program ABAP Runtime analysis Database SQL TraceThe runtime analysis function tells you the number of database operations and the gross time that they require, that is, the time including that spent in queues or network overhead time.
Runtime analysis provides useful information about access to buffered tables, since they, by
definition, do not involve database access. However, for a precise analysis of the runtime required by SQL statements and their conversion in the database, you need the SQL Trace utility. This provides not only the runtime, but also other database-relevant information such as the index used to read the database.
(C) SAP AG BC411 2-31 ©SAP AG
R
SQL Trace
Application server
Database I/O
S
QL T raceThe SQL Trace tool allows you to analyze the execution of SQL statements, locking operations, and Remote Function Calls.
This section of the course concentrates on the runtime of SQL access.
Only one person can use the SQL Trace on an application server at any one time. You should therefore switch it off as soon as you have finished with it.
You start the SQL Trace using Transaction ST05, or by choosing the System -> Utilities -> SQL Trace.
The SQL Trace does not include access to buffered tables except in the case of the first time they are read (when they are not yet stored in the buffer).
The SQL Trace measures the time that the database need to execute a statement. Runtime analysis, on the other hand, measures the overall time required by the statement within the context of the application program, that is, the execution time plus the time spent in queues and that required for network transmission.
(C) SAP AG BC411 2-32 ©SAP AG
R
Trace Basic List
Debugging
?
?
Bezeichnungen
Goto Breakpoint Settings Development System Help
DDIC Info Explain SQL ABAP Display More Info
Transaction = SAPBC411 Client = 000 User = Heilerm Duration Object Oper Rec RC Statement
684 REEXECS 0 0 Free
10,998 TRDIR REEXECS 1 0 Select Where "name" = 'SAPBC411_...
14,920 SPFLI PREPARE Select Where "mandt" = ? AND "carrid" 461 SPFLI DECLARE Select Where "mandt" = ? AND "carrid" 347 SPFLI OPEN Select Where "mandt" = '400' AND "... 191,329 SPFLI FETCH 6 0
206 SPFLI FETCH 6 0 300 SPFLI FETCH 0 100 209 SPFLI CLOSE 0 0
SQL Trace - Basic List
The basic list of the SQL Trace contains the following information:
Duration: The time in microseconds (10-6) required by the database to execute the operation. The individual columns have the following meanings:
Object The database table to which the operation refers
Oper The operation executed by the database. This can be one of the following: Declare Creates a new cursor for an SQL statement and assigns a cursor number to it Prepare Converts the SQL statement and determines the execution plan
Open/Close Opens/closes the cursor with the precise values required for the access Reopen Reuses an existing cursor in the cursor cache on the application server Fetch Moves the cursor through the data and sends it to the application server EXEC/REEXECS: If you want to insert, change, or delete data
Rec The number of records read
RC The return code of the corresponding statement Statement The SQL statement currently being processed
'Expensive' statements (more than 100,000 µs) are highlighted in red.
(C) SAP AG BC411 2-33 ©SAP AG
R
SQL Statement SELECT
"CARRID" , "CONNID" , "PAYMENTSUM", "CURRENCY" FROM
"SPFLI" WHERE
"MANDT" = :A0000 AND "CARRID" = :A0001 Execution Plan
SELECT STATEMENT 5 TABLE ACCESS BY ROWID SPFLI
INDEX RANGE SCAN SPFLI______0
SQL Trace Function: Explain SQL
The Explain SQL function provides a more detailed analysis of an SQL statement. It displays how the statement is executed and, above all, the index that is used. SFLIGHT____0 means that the primary index was used. A figure other than zero indicates that the statement used a secondary index from the ABAP Dictionary.
The precise display of the Explain SQL function depends on the database system. The above example shows how it appears under Oracle.
(C) SAP AG BC411 2-34 ©SAP AG
R
SELECT ... FROM SPFLI WHERE CARRID IN ITAB_CARRID.
SPFLI_______0
MANDT CARRID CONNID ROWID
---001 AA ---0017 1237 001 AA 0064 1235 001 AZ 0555 1234 001 AZ 0788 1236
Index for primary key
SPFLI
ROWID MANDT CARRID CONNID... ---1234 001 AA 0017 1235 001 AA 0064 1236 001 AZ 0555 1237 001 AZ 0788 Table contents
Index Range Scan/Access by Rowid
When you access the database using index range scan/access by rowid, the system starts searching from the point in the table where the specified key occurs in the primary key, and reads sequentially until the key changes. Table entries that match the key are ready from the table using their ROWID.
(C) SAP AG BC411 2-35 ©SAP AG
R
Summary
In this unit, you have learned the typical steps involved in program development, in particular how to check programs using
Static check Runtime check
and the most important functions for Dynamic program checks:
Runtime analysis
The GET RUN TIME statement SQL Trace
©SAP AG
R
Types: Contents
The ABAP type concept
ABAP Dictionary types and ABAP program types
Type compatibility and conversion
Type conversion and performance
Runtime type information
(C) SAP AG BC411 3-2 ©SAP AG
R
Unit Objectives
The system of types in ABAP
How ABAP Dictionary types are based on program types
The type compatibility and type conversion rules in ABAP
How type conversions can affect the performance of programs
How you can find out about the type of a data object at runtime
(C) SAP AG BC411 3-3 ©SAP AG R
Data Types
Predefined
Elementary
(Data element)Structured type
(Structure)Table type
Class/Interface
Structured
User-defined
Data type
AB AP Dic tiona ry AB AP Pro gra mThe ABAP type concept is based on the following ideas:
User-defined data types can be either elementary or structured. Elementary types cannot be broken down into other, smaller components. Structured types, on the other hand, consist of any number of elementary or other structured types.
There are two types of structured data types - structure types and table types:
You can combine components of any type to form a structure (record in other terminology). You can combine lines of any type to form a table type (array in other terminology).
Data types that are structure types or table types may contain other structure types or table types as elements.
ABAP Types can be defined either locally in an ABAP program or systemwide in the ABAP Dictionary. In the ABAP Dictionary,
user-defined elementary types are called data elements user-defined structure types are called structures.
(C) SAP AG BC411 3-4 ©SAP AG
R
Predefined ABAP Types
Data type Description Initial value Default Permitted
length length P I F N C D T X Num e ric A lpha-numeric REF TO Packed number Integer
Floating point number
Numeric string Numeric string Text Text Date YYYYMMDD Date YYYYMMDD Time HHMMSS Time HHMMSS Hexadecimal Hexadecimal Reference Reference 0 0 0.000 00...0 00...0 Space Space 00000000 00000000 000000 000000 X‘00‘ X‘00‘ -8 4 8 1 1 1 1 8 8 6 6 1 1 -1-16 4 8 1 1 --max.max. 1 1 --max.max. 8 8 6 6 1 1 --max.max.
-There are nine predefined data types in ABAP.
The numeric types P, I, and F have the following attributes and value ranges:
Type Attribute Value range Suitable for
I Integer - 231 bis +231 Counters, numbers,
indexes, periods
P Number with decimal places 2 digits per byte Currency amounts, length, weight F Floating point -2.2E-308 to +1.8E308 Calculations with very large amounts
In terms of runtime, calculations with type I numbers is the cheapest, followed by type F, with type P calculations being the most expensive. Note, however, that the system may round or truncate type F numbers. In particular, it is not a good idea to test whether two floating point numbers are equal. Instead, you should check whether they differ by less than a predefined amount. You should only use type F numbers for mathematical problems where rounding errors are tolerable. Use type P numbers for business calculations.
Type P numbers are stored with two digits per byte. The last half-byte is used for the plus or minus sign.
(C) SAP AG BC411 3-5 ©SAP AG
R
Predefined Dictionary Types I
DEC INT1 INT2 INT4 Description Calculation/amount field 1 byte integer 2 byte integer 4 byte integer Currency field Currency key Quantity Unit key
Accuracy of QUAN field Floating point number
CURR CUKY QUAN UNIT PREC FLTP
Type Permitted length n
1-31, in tables 1-17 3 5 10 1-17 5 1-17 2-3 2 16 ABAP type P((n+1)/2) internal internal I P((n+1)/2) C(5) P((n+1)/2) C(n) X(2) F(8)
The ABAP Dictionary has significantly more built-in types than the ABAP programming language. ABAP Dictionary data types are converted into corresponding ABAP types in ABAP programs. ABAP Dictionary data types have the following semantics (continued on next page):
DEC Calculation or amount field with decimal point, sign, and thousand separators INT1 1 byte integer, value between 0 and 255
INT2 2 byte integer, value between -32767 and 32767
INT3 4 byte integer, value between -2177483647 and 2177483647 CURR Currency field. Corresponds to a DEC amount field.
Fields with this type must point to a reference field with type CUKY. CUKY Currency key referenced by a type CURR field.
QUAN Quantity field. Corresponds to a DEC amount field.
Fields with this type must point to a reference field with type UNIT. UNIT Unit key referenced by a type QUAN field.
PREC Accuracy of a QUAN field. FLTP Floating point number.
(C) SAP AG BC411 3-6 ©SAP AG
R
Predefined Dictionary Types (2)
Description Permitted length n ABAP type
Numeric text 1-255 N(n)
Character 1-255 C(n)
Long Character 256-max C(n)
Date 8 D
Accounting period YYYYMM 6 N(6)
Time HHMMSS 6 T
Uninterpreted byte sequence 1-255 X
Long byte sequence 256-max X(n)
Client 3 C(3)
Language int. 1, ext. 2 C(1)
ACCP TIMS RAW LRAW CLNT LANG Type DATS LCHR CHAR NUMC
CHAR Character. Fields with type CHAR may only be up to 255 characters long in tables. If you use longer character fields in tables, use the data type LCHR. There is no length restriction in structures.
LCHR. Character string at least 256 characters long. Fields of this type must occur at the end of transparent tables and be preceded by a length field with type INT2 that is filled with the required length when the table is filled.
DATS Date.
ACCP Accounting period in the format YYYYMM. TIMS Time
RAW Byte sequence. Fields with type RAW may only be up to 255 characters long in tables. If you want to use longer RAW fields in tables, use the data type LRAW.
LRAW Byte sequence of any length longer than 256 bytes. Fields of this type must occur
at the end of transparent tables and be preceded by a length field with type INT2 that is filled with the required length when the table is filled.
CLNT Client
LANG Language key. This is displayed as a two-character code, but stored in the database as a single character. It is converted using the exit ISOLA.
(C) SAP AG BC411 3-7 ©SAP AG
R
Calculations Using Numeric Types
REPORT sapbc411d_arithmetic.
DATA: int1 TYPE i, int2 TYPE i,
pack1 TYPE p, float TYPE f.
int1 = 1. int2 = 3. pack1 = 3. float = ( int1 / int2 ) * pack1.
WRITE float. 1,0000000000000000E+00
ABAP performs calculations using either F, P, or I arithmetic. The arithmetic used is determined by the following rule:
F before P before I
F before P before I
ABAP has three different kinds of arithmetic: F, P, and I.
Arithmetic expressions are calculated using one of there three types. All of the operands (including the answer) are considered when determining the type of arithmetic to use.
The priority rule is F before P before I. This means that:
If at least one of the operands has type F, ABAP will use type F arithmetic.
If there are no type F operands, but at least one type P operand, ABAP will use type P arithmetic. If there are no type F or P operands, ABAP will use type I arithmetic.
Note that:
Operands with inbuilt functions (such as SQRT(…)) are type F.
Numeric strings up to nine digits are type I, and numeric strings with ten or more digits are type P.
(C) SAP AG BC411 3-8 ©SAP AG
R
Summary of Declarative Statements
Input parameter
PARAMETERS Constant
CONSTANTS
TABLES Table work area
Type
TYPES
SELECT-OPTIONS RANGES
Selection criteria
Elementary field, structure,
internal table DATA
You should already know the following declarative statements: TYPES Defines a type
CONSTANTS Defines a constant
DATA Defines an elementary field, structure, or internal table PARAMETERS Defines an input parameter
TABLES Defines a table work area
SELECT-OPTIONS Defines a selection criterion
You can display a full list of ABAP declarative statements in the ABAP Editor F1 help by choosing ABAP Overview -> Overview of the ABAP Programming Language -> Classification of Key Words by Type.
(C) SAP AG BC411 3-9 ©SAP AG
R
Type Conversion
Two structured types are compatible when they have
exactly the same structure and their componentsare
compatible.
Two table types are compatible when their line typesare compatible and their
key sequences, uniqueness attributesand table typesare the same.
Non-compatible types can be converted if a conversion rule exists. Compatible types can be assigned to each other without conversion.
Two elementary types are compatible when they
have exactly the same typeand length(and, for
type P fields, the same number of decimal places).
Two object references are compatible when they refer to the same class. An
object reference is compatible with an interface reference when the class
referenced by the object implements the interface.
If you assign data objects to each other, the ABAP runtime system converts the types if: the two types are incompatible, and
there is a defined conversion rule.
An object reference o1 with the type REF TO <class> is compatible with object reference o2 when o2 references the class <class> or one of its subclasses. Object references with the type <class> are compatible with interface references when the class <class> implements the interface.