abap chapter 1 to 5.docx

75
 BEGINNER’S GUIDE  TO SAP ABAP AN INTRODUCTION TO PROGRAMMING SAP APPLICATIONS USING ABAP

Upload: sham777779567

Post on 14-Jan-2016

245 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 1/75

BEGINNER’SGUIDE TO

SAP ABAP

AN INTRODUCTION TOPROGRAMMING SAP

APPLICATIONS USING ABAP

Page 2: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 2/75

Table of Contents

Page 3: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 3/75

Chapter 1: SAP System Overvie1

  SAP System Ar!hite!t"re1

  E#vir$#me#t %$r Pr$&rams '

  ($r) Pr$!esses*

  The Dispat!her*

  The Data+ase I#ter%a!e,

  -irst .$$) at the ABAP ($r)+e#!h,

  ABAP Di!ti$#ary/

  ABAP E0it$r

  -"#!ti$# B"i.0er

  Me#" Pai#ter

  S!ree# Pai#ter

  O+2e!t Navi&at$r

Chapter ': Data Di!ti$#ary3

  Creati#& a Ta+.e3

  Creati#& -ie.0s 11

  Data E.eme#ts 1'

  Data D$mai#s 1,

  Te!h#i!a. Setti#&s''

  E#teri#& Re!$r0s i#t$ a Ta+.e'*

  4iei#& the Data i# a Ta+.e'5

Chapter *: Creati#& a Pr$&ram'3

  C$0e E0it$r *1

Page 4: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 4/75

  (rite Stateme#ts*,

  O"tp"t I#0ivi0"a. -ie.0s,1

  Chai#i#& Stateme#ts T$&ether

,'  C$py 6$"r Pr$&ram ,*

  De!.ari#& 4aria+.es ,7

  C$#sta#ts,/

Chapter , ,

  Arithmeti! 8 A00iti$#,

  Arithmeti! 8 S"+tra!ti$#,3

  Arithmeti! 8 Divisi$# ,3

  Arithmeti! 8 M".tip.i!ati$# ,3

  C$#versi$# R".es79

  Divisi$# 4ariati$#s 71

  The sta#0ar0 %$rm $% 0ivisi$#71

  The i#te&er %$rm $% 0ivisi$#7'

  The remai#0er %$rm $% 0ivisi$#7'

Chapter 7 8 Chara!ter Stri#&s 7*

  De!.ari#& C a#0 N -ie.0s7*

  Data type C7*

  Data type N 7,

  Stri#& Ma#ip".ati$# 77

  C$#!ate#ate 77

  C$#0e#se75

  NOGAPS7/

Page 5: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 5/75

  -i#0 the Le#&th $% a Stri#& 7/

  Rep.a!e 7

  Sear!h 7

  Sear!h E;amp.es 1<'<*<,7

  Shi%t 51

  Sp.it 5'

  S"+-ie.0s5*

Chapter 5 8 De+"&&i#& Pr$&rams

  -ie.0s m$0e

  System 4aria+.es

  Ta+.e M$0e

  Brea)p$i#ts

  Stati! Brea)p$i#ts

  (at!hp$i#ts

  E#0i#& a De+"& Sessi$#

Chapter /: ($r)i#& ith Data+ase Ta+.es=====================

  Ma)i#& a C$py $% a Ta+.e =========================>

  A00 Ne -ie.0s =========================>===

  -$rei&# ?eys =========================>===

  Appe#0 Str"!t"res =========================>===

  I#!."0e Str"!t"res =========================>===

  ?ey -ie.0s =========================>======

  De.eti#& -ie.0s =========================>======

  De.eti#& Ta+.es =========================>===

Chapter 8 ($r)i#& ith Other Data Types====================

  Date a#0 Time -ie.0s =========================>===

  Date -ie.0s i# Ca.!".ati$#s =========================>

  Time -ie.0s i# Ca.!".ati$#s =========================>

  @"a#tity a#0 C"rre#!y -ie.0s i# Ca.!".ati$#s==================

Page 6: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 6/75

Chapter 3 8 M$0i%yi#& Data i# a Data+ase Ta+.e===================

  A"th$risati$#s =========================>======

  -"#0ame#ta.s =========================>======

  Data+ase L$!) O+2e!ts =========================>

  Usi#& Ope# S@L Stateme#ts=========================>

  Usi#& Ope# S@L Stateme#ts 8 7 Stateme#ts==================

  I#sert Stateme#t =========================>===

  C.ear Stateme#t =========================>===

  Up0ate Stateme#t =========================>===

  M$0i%y Stateme#t =========================>===

  De.ete Stateme#t =========================>===

Chapter 19 8 Pr$&ram -.$ C$#t$. a#0 L$&i!a. E;pressi$#s=============

  C$#tr$. Str"!t"res =========================>===

  I% Stateme#t =========================>======

  Li#)i#& L$&i!a. E;pressi$#s T$&ether ====================

  Neste0 I% Stateme#ts =========================>===

  Case Stateme#t =========================>======

  Se.e!t L$$ps =========================>======

  D$ L$$ps =========================>=========

  Neste0 D$ L$$ps =========================>======

  (hi.e L$$ps =========================>=======

  Neste0 (hi.e L$$ps =========================>===

  L$$p Termi#ati$# 8 CONTINUE ======================

  L$$p Termi#ati$# 8 CEC? =========================>

  L$$p Termi#ati$# 8 EIT =========================>

Chapter 11 8 Se.e!ti$# S!ree#s=========================>

  Eve#ts =========================>=========

Page 7: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 7/75

  I#tr$ t$ Se.e!ti$# S!ree#s =========================>

  Creati#& Se.e!ti$# S!ree#s=========================>

  At Se.e!ti$# S!ree# =========================>===

  Parameters =========================>======

  DE-AULT =========================>======

  OBLIGATOR6 =========================>======

  A"t$mati! Ge#erati$# $% Dr$pD$# e.0s=====================

  LO(ER CASE =========================>======

  Che!) B$;es a#0 Ra0i$ B"tt$# Parameters=====================

  Se.e!tOpti$#s =========================>======

  Se.e!tOpti$# E;amp.e =========================>===

  Se.e!tOpti$# A00iti$#s =========================>===

  Te;t E.eme#ts =========================>======

  4aria#ts =========================>=========

  Te;t Sym+$.s =========================>======

  Te;t Messa&es =========================>======

  S)ip Li#es a#0 U#0er.i#e =========================>===

  C$mme#ts =========================>=========

  -$rmat a Li#e a#0 P$siti$# =========================>=

  E.eme#t B.$!)s =========================>======

Chapter 1' 8 I#ter#a. Ta+.es =========================>==

  I#tr$0"!ti$# =========================>=======

  Types $% I#ter#a. Ta+.es =========================>==

  Sta#0ar0 Ta+.es =========================>======

  S$rte0 Ta+.es =========================>======

Page 8: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 8/75

  ashe0 Ta+.e =========================>======

  I#ter#a. Ta+.es Best Pra!ti!e G"i0e.i#es====================

  Creati#& Sta#0ar0 a#0 S$rte0 Ta+.es========================

  Create a# I#ter#a. Ta+.e ith Separate ($r) Area=================

  -i..i#& a# I#ter#a. Ta+.e ith ea0er Li#e====================

  M$veC$rresp$#0i#& =========================>===

  -i..i#& I#ter#a. Ta+.es ith a ($r) Area====================

  Usi#& I#ter#a. Ta+.es O#e Li#e at a Time

======================  M$0i%y =========================>=========

  Des!ri+e a#0 I#sert =========================>===

  Rea0 =========================>=========

  De.ete Re!$r0s =========================>======

  S$rt Re!$r0s =========================>======

  ($r) Area Diere#!es =========================>===

  L$$ps =========================>=========

  M$0i%y =========================>=========

  I#sert =========================>=========

  Rea0 =========================>=========

  De.ete =========================>======

  De.ete a Ta+.e ith a ea0er Li#e=========================>

  CLEAR =========================>=========

  RE-RES =========================>=========

  -REE =====================

  De.ete a Ta+.e ith a ($r) Area=========================>

Chapter 1* 8 M$0".arii#& Pr$&rams=========================>

Page 9: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 9/75

  I#tr$0"!ti$# =========================>=======

  I#!."0es =========================>=========

  Pr$!e0"res =========================>=========

  S"+R$"ti#es =========================>======

  Passi#& Ta+.es =========================>======

  Passi#& Ta+.es a#0 -ie.0s T$&ether=======================

  S"+R$"ti#es E;ter#a. Pr$&rams=========================>

  -"#!ti$# M$0".es =========================>======

  -"#!ti$# M$0".es 8 C$mp$#e#ts=========================>

  Attri+"tes Ta+ =========================>======

  Imp$rt Ta+ =========================>======

  E;p$rt Ta+ =========================>======

  Cha#&i#& Ta+ =========================>======

  Ta+.es Ta+ =========================>======

  E;!epti$#s Ta+ =========================>=====

  S$"r!e C$0e Ta+ =========================>====

  -"#!ti$# M$0".e Testi#& =========================>=

  -"#!ti$# M$0".es C$0i#& =========================>

Page 10: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 10/75

Page 11: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 11/75

Chapter 1: SAP System Overview

We will start out by covering the high-level architecture of SAP system, including the technicalarchitecture and platform independence. We will dig into the environment that our ABAP programsrun in, which include the work processes and the basic structures of an ABAP program. hen we can

focus on a running SAP system, discuss the business model overview, and begin looking at theABAP workbench.

SAP System Architecture

!irst, the echnical Architecture of a typical SAP system will be discussed, before moving on to the"andscape Architecture, and a discussion of why the landscape should be broken into multiplesystems.

his diagram shows the #-tier $lient%Server architecture of a typical SAP system&

At the top is the Presentation server, which is any input device that can be used to control an SAPsystem 'the diagram shows the SAP ()*, but this could e+ually be a web browser, a mobile device,and so on. he Presentation layer communicates with the Application server, and the Applicationserver is the brains of an SAP system, where all the central processing takes place. he Applicationserver is not ust one system in itself, but can be made up of multiple instances of the processingsystem. he Application server, in turn, communicates with the /atabase layer.

he /atabase is kept on a separate server, mainly for performance reasons, but also for security, providing a separation between the different layers of the system.

$ommunication happens between each layer of the system, from the Presentation layer, to theApplication server, to the /atabase, and then back up the chain, through the Application server again,for further processing, until finally reaching the Presentation layer.

1

Page 12: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 12/75

A typical "andscape Architecture - ypical here is subective, in practical terms there is not reallyany such thing as a standard, typical landscape architecture which most companies use. 0owever, it

is common to find a /evelopment system, a esting system and a Production system&

he reason for this is fairly simple. All the initial development and testing is done on a /evelopmentsystem, which ensures other systems are not affected. 1nce developments are at a stage where theymay be ready to be tested by an e2ternal source, or someone within the company whose role is tocarry out testing, the developments are moved, using what is called a ransport System, to the ne2tsystem 'here, the esting system.

 3ormally, no development at all is done on the testing system4 it is ust used for testing thedevelopments from the development system. *f everything passes through the esting system, aransport system is used again to move the developments into the Production environment. Whencode enters the Production environment, this is the stage at which it is turned on, and used within the

 business itself.

he landscape architecture is not separated ust for development purposes4 the company may haveother reasons. his could be the +uantity of data in the Production system, which may be too great to

 be used in the development environment 'normally the /evelopment and esting systems are not aslarge as the Production system, only needing a subset of data to test on. Also, it could be for securityreasons. 5ore often than not, companies do not want developers to see live production data, for datasecurity reasons. 3ormally, then, the /evelopment and esting systems would have their own set of data to work with.

he three systems described here, normally, are a minimum. *t can increase to four systems, perhaps

with the addition of a raining system, or perhaps multiple proects are running simultaneously,meaning there may be two separate /evelopment systems, or esting systems, even perhaps a$onsolidation system before anything is passed to the Production environment. his is all, of course,dependent on the company, but commonly each system within the "andscape architecture will haveits own Application server and its own /atabase server, ensuring platform independence.

Environment for Programs

 3e2t, we have the environment which programs run in, the Work Processes, and the structure of anABAP program. Within an SAP system there are two types of programs, 6eports and /ynpro7s.

6eports, as the name would suggest, are programs which generate lists of data. hey may involve asmall amount of interactivity, but mainly they supply data to the front-end interfaces, the SAP ()*and so on. When a user runs a report, they typically get a selection screen. 1nce they enter their 

'

Page 13: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 13/75

selection parameters and e2ecute the report, they normally cannot intervene in the e2ecution of the program. he program runs, and then displays the output.

/ynpro7s are slightly different. hey are dynamic programs, and allow the user to intervene in thee2ecution of the program, by processing a series of screens, called /ialogue screens. he user determines the flow of the program itself by choosing which buttons or fields to interact with on thescreen. heir action then triggers different functions which have been coded within the flow logic of the program. While reports are being created, interfaces are also to be generated which are classed as/ynpro7s, for all the selection criteria.

5ost of the work done by people involved with ABAP is done within 6eport programs, and eventhough these programs are labeled 6eports, they do not always generate output. he 6eport

 programs are there to process the logic, reading and writing to the /atabase, in order to make thesystem work.

Work Processes

8very program that runs in an SAP system runs on what are called Work Processes, which run on theApplication server. Work Processes themselves work independently of the computers operatingsystem and the /atabase that it interacts with, giving the independence discussed earlier with regardto the echnical architecture. When an SAP system is initially set up, the basis consultants 'whoinstall the system, keep it running, manage all the memory and so on configure SAP in such a waythat it automatically sets the number of Work Processes programs use when they start, the e+uivalentof setting up a pre-defined number of channels or connections to the /atabase system itself, each of 

which tend to have their own set of properties and functions.

The Dispatcher

9ou might come across something referred to as the /ispatcher. he SAP system has no technicallimits as to the number of users who can log on and use it, generally the number of users who canaccess an SAP system is much larger than the number of available Work Processes the system isconfigured for. his is because not everybody is sending instructions to the Application server ate2actly the same time. Because of this, users cannot be assigned a certain number of processes whilethey are logged on.

he /ispatcher controls the distribution of the Work Processes to the system users. he /ispatcher keeps an eye on how many Work Processes are available, and when a user triggers a transaction, the/ispatchers ob is to provide that user with a Work Process to use. he /ispatcher tries to optimise

*

Page 14: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 14/75

things as far as possible, so that the same Work Process receives the se+uential /ialogue steps of anapplication. *f this is not possible, for e2ample because the user takes a long time between clickingdifferent aspects of the screen, it will then select a different Work Process to continue the processingof the /ialogue program. *t is the Work Process which e2ecutes an application, and it is the Work Process which has access to the memory areas that contain all of the data and obects an applicationuses. *t also makes three very important elements available.

he first is the /ynpro processor. All /ynpro programs have flow and processing logic, and it is the/ynpro processors ob to handle the flow logic. *t responds to the users interactions, and controlsthe further flow of the program depending on these interactions. *t is responsible for /ialogue controland the screen itself, but it is important to remember that it cannot perform calculations4 it is purelythere to manage the flow logic of a program.

he ne2t important element is the ABAP processor, which is responsible for the processing logic of the programs. *t receives screen entries from the /ynpro processor, and transmits the screen output tothe program. *t is the ABAP processor which can perform the logical operations and arithmeticalcalculations in the programs. *t can check authorisations, and read and write to the /atabase, over the/atabase *nterface.

The Dataase !nterface

he /atabase *nterface is the third important element. *t is a set of ABAP statements that are/atabase independent. What this means is that a set of ABAP statements can be used that, in turn,can communicate with any type of /atabase that has been installed when the system was set up.Whether this is, for e2ample, a 5icrosoft S:" server or an 1racle /atabase, you can use the sameABAP statements, called 1pen S:", to control the entire /atabase reading and writing over the/atabase *nterface. he great advantage of this is that the ABAP statements have encapsulation,meaning the programmers do not need to know which physical /atabase system the ABAP system

they are using actually supports.

here are times when you may want to use a specific S:" statement native to the database which isinstalled. ABAP is designed in such a way that if this type of coding is necessary, this facility isavailable. *t is possible to directly access the /atabase through the programs using native S:"statements, but this is not encouraged. 3ormally, when systems are set up, the system administrator will forbid these practices, due to the security and stability risks to the system which may beintroduced. *f you are going to be programming ABAP, make sure 1pen S:" is used, because thenanyone subse+uently looking at the programs will understand what is trying to be achieved.

"irst #ook at the A$AP Workench

*t is now time to take a first look at an SAP ABAP program. he following section will look at theSAP System and introduce the ABAP Workbench. But before doing so, lets take a look at thestructure of an ABAP program.

,

Page 15: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 15/75

"ike many other programming languages, ABAP programs are normally structured into two parts.

he first is what is considered to be the /eclaration section. his is where you define the data types,structures, tables, work area variables and the individual fields to be used inside the programs. his is

also where you would declare global variables that will be available throughout the individualsubsections of the program. When creating an ABAP program, you do not only declare globalvariables, but you also have the option to declare variables that are only valid within specific sectionsinside the programs. hese sections are commonly referred to as internal Processing Blocks.

he /eclaration part of the program is where you define the parameters used for the selectionscreens for the reports. 1nce you have declared tables, global variables and data types in the/eclaration section of the program, then comes the second part of the ABAP program, where all ofthe logic for the program will be written. his part of an ABAP program is often split up into whatare called Processing Blocks.

he Processing Blocks defined within programs can be called from the /ynpro processor, whichwere discussed previously, depending on the specific rules created within the program. heseProcessing Blocks are almost always ust small sections of programming logic which allow the codeto be encapsulated.

"irst %ook 

When logged into an SAP system it will look something similar to the image below.

he way the SAP ()* looks may vary, the menu to the side may be different, but here the displayshow a minimal menu tree which will be used throughout this book.

he first thing to do here is look at the ABAP Workbench. o access this, you use the menu on theleft hand side. 1pen the SAP menu, choose ools and open the ABAP Workbench, where there will

 be four different options.

7

Page 16: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 16/75

he first thing to look at is a +uick overview of how to run a transaction in SAP. here are two waysto do this. !irstly, if the overview folder is opened, any item which does not look like a folder itself,is a transaction which can be run. *n this instance, we can see the 1bect 3avigator&

/ouble click this, and the transaction will open&

o e2it out of the transaction, click the Back button&

he second way of running a transaction is to enter the transaction code into the transaction codeinput area&

5

Page 17: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 17/75

A useful tip to become familiar with the names of transactions is to look at the 82tras menu --; openSettings and in the dialogue bo2 which appears, select the option /isplay technical names and click the $ontinue icon&

he menu tree will be refreshed, and when the 1verview folder is opened, the transaction codes will be made visible. *t is now possible to become familiar with them, and enter them directly into thetransaction code input area&

 3ow, a step-by-step look will be taken through the maor transactions of the ABAP Workbench to become familiar with, and use, as an ABAP developer.

A$AP Dictionary

1ne thing most programs will have in common is that they will read and write data to and from the

/atabase tables within the SAP system. he ABAP Workbench has a transaction to allow thecreation of /atabase tables, view the fields which make up these tables and browse the data inside.his is called the ABAP /ictionary. he ABAP /ictionary can be found by e2panding the ABAPWorkbench menu tree --; /evelopment. he transaction code to run the ABAP dictionary directly isS8<<&

/

Page 18: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 18/75

A$AP E&itor

he ne2t and probably most commonly used part of the ABAP Workbench is the ABAP 8ditor,which much of this course will focus upon. he ABAP 8ditor is where all of the code is created, thelogic built and, by using forward navigation 'a function within an SAP system which will bediscussed later, function modules defined, screens created and so on. he ABAP 8ditor can be foundunder the /evelopment menu, as shown above and with transaction code S8#=.

"unction $ui#&er

he ne2t important part of the Workbench is the !unction Builder, which is similar to the ABAP8ditor. *ts main function is to define specific tasks that can be called from any other program.*nterfaces are created in the !unction Builder, where the different data elements and different typesof tables are defined, that can be passed to and from the !unction which is built. he !unctionBuilder will be discussed a little later on, when the programs created are encapsulated into functionmodules. he !unction Builder can be called with transaction code S8#>.

'enu Painter

he ne2t item to look at here is called the 5enu Painter, which can be found in the )ser *nterfacefolder inside the /evelopment menu, or with transaction code S8?<. his is a tool which can beused to generate menu options, buttons, icons, menu bars, transaction input fields, all of which cantrigger events within the program. 9ou can define whether events are triggered using a mouse click,or with a keyboard-based shortcut. !or e2ample, in the top menu bar here, the "og off button can beseen, which can be triggered by using 'Shift @ !#&

Screen Painter

While the 5enu Painter is used for building menu items, menu bars and so on, the ne2t item on thelist is the Screen Painter, transaction code S8<, which allows you to define the user input screen,meaning that you can define te2t bo2es, drop-down menus, list bo2es, input fields, tabbed areas ofthe screen and so on. *t allows you to define the whole interface which the user will eventually use,and behind the initial elements that are put on the screen, you can also define the individual functionswhich are called when the user interacts with them.

O(ect )avigator

he last item to look at here is the 1bect 3avigator, a tool which brings together all the previoustools, providing a highly efficient environment in which to develop programs. When building large

 programs, with many function modules, many screens, the 1bect 3avigator is the ideal tool to use tonavigate around the development. *t can be found in the 1verview menu of the ABAP Workbench,with transaction code S8=.

hese are the main features of the ABAP Workbench interacted with during this course. *n the SAP

menu tree, there are evidently many more transactions which can be used to help develop programs, but these cover the vast maority of development tools which will be used.

Page 19: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 19/75

Chapter *: Data Dictionary

his chapter will focus specifically on the /ata /ictionary. his is the main tool used to look at,understand and enhance the /atabase and /atabase tables which are used by the SAP system. 9oucan view standard tables delivered by SAP using this tool, create new tables and enhance the e2isting

tables delivered by SAP with new fields.

!irst, a database table will be created, involving the creation of fields, data elements and domains. Ane2planation of what each of these is, and why they are necessary to the tables built will be given./uring the building of the tables, the tools used to check for errors will be shown. 1nce these errorsare eradicated, the tables can be activated so that they can be used within the system.

After this, a look will be taken at maintaining the technical settings of the table created, which willallow the entry of data, before finally looking at the data which has been entered using standard SAPtransactions available in the SAP system.

Creating a Ta#e

With the SAP ()* open, you will be able find the /ata /ictionary in the SAP menu tree. his isdone via the ools menu. 1pen the ABAP Workbench and click the /evelopment folder, where theABAP /ictionary can be found and double-clicked. Alternatively, use the transaction code S8<<&

 3ow, the initial screen of the ABAP /ictionary will appear&

o create a table, select the /atabase table option. *n this e2ercise a transparent table will be created.1ther types of table do e2ist 'such a cluster tables and pool tables, but at this early stage thetransparent table variety is the important one to focus upon.

he table name must adhere to the customer-defined name space, meaning that the name must beginwith the letter C or 9, most commonly this will be C. *n this e2ample, the table will show a list ofemployees within a company, so, in the /atabase table area, type C85P"1988S and click the$reate button.

3

Page 20: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 20/75

1nce this is done, a new screen will appear&

*n the Short /escription field, a description for the table must be included, enter 8mployees&

*n the /elivery and 5aintenance tab 'which opens by default, look at the /elivery class section,select the field and then click the drop-down button, where a list of /elivery classes will be shownand selected&

!or the table being created here, choose Application table, as the data held in the table fits thedescription master and transaction data.

19

Page 21: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 21/75

*n the field below this, labeled /ata Browser%able Diew 5aint., choose the /isplay%5aintenanceallowed option, which will allow for data entry directly into the table later on. *t should look likethis&

Before going any further, click the Save button&

A window appears titled $reate 1bect /irectory 8ntry.

 3early all development work done with SAP is usually done within a development environment, before being moved on to, for e2ample, a +uality assurance environment and on further to production. his window allows you to choose the appropriate Package which is supported by othersystems where the work may be moved on to. *n this e2ample scenario, though, developments willnot be moved on to another system, so click %oca# o(ect, so as to indicate to the system 'via the

 phrase E5P which appears that the obect is only to e2ist within the development system and notto be transported elsewhere. 1nce this is done, the status bar at the bottom will show that the obecthas been saved&

o check everything has worked as we want, select the (o to menu and selects the 1bect directoryentry option, a similar pop-up bo2 to the previous one will appear, where the Package field willshow E5P, confirming this has been done correctly.

11

Page 22: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 22/75

Creating "ie#&s

he ne2t step is to begin creating !ield names for the table, in the !ields tab&

!ields, unlike the name of the table, can begin with any letter of the alphabet, not ust C and 9 andcan contain up to <F characters.

ables must include at least one Gey field, which is used later for the searching and sorting of data,and to identify each record as being uni+ue.

An *nitial value can be assigned to each field, for e2ample, in the case of a field called 8mployee$lass you could say the maority of employees are 6egular Staff 'S, but some are /irectors, with acode of /. he standard initial value would be S, but the user could change some of these to a /later on.

Data E#ements

8very !ield in the table is made up of what is called a /ata 8lement, which defines specificattributes of each field.

1'

Page 23: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 23/75

he first !ield to be created here is an important one within an SAP system, and identifies the clientwhich the records are associated with. *n the !ield name, enter $lient, and in the /ata 8lement, type5A3/. his /ata 8lement already e2ists in the system, and after entering it, the systemautomatically fills in the /ata ype, the "ength, 3umber of /ecimals and Short te2t for the /ata8lement itself. 8nsure that the $lient field is made a Gey field in the table by checking the Gey

 bo2.

he ne2t field will be called 8mployee. Again, check the bo2 to make this a Gey field, and enter thenew /ata 8lement C883)5 '/ata 8lements broadly must adhere to the customer name space by

 beginning with C or 9. 1nce this is done, click the save button.

 3e2t, because the /ata 8lement C883)5 does not yet e2ist, it must be created. *f you try to

activate or even check the table 'via the $heck button, an error message is displayed&

)ntil the /ata 8lement C883)5 is created, it cannot be used within the system. o do this,

forward navigation is used. /ouble-click the new /ata 8lement, and a window labeled $reate /ataelement appears. Answer 9es to this, and the 5aintain /ata 8lement window comes up.

1*

Page 24: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 24/75

*n the Short te2t area, enter 8mployee /ata 8lement. 3e2t, the 8lementary data type, called the

/omain, must be defined for the new /ata element. /omains must adhere to the customer namespace, so in this instance the same name as the /ata element will be given& C883)5, 'thoughgiving both the same name is not imperative. Again, forward navigation will be used to create the/omain.

Data Domains

/ouble-click the entry 'C883)5 in the /omain area, and agree to save the changes made. 3ow,the $reate 1bect /irectory 8ntry window will re-appear and again it is important to save thisdevelopment to the E5P development class, via the save or local obect button visible in this

window.

After doing this, a window will appear stating that the new /omain C883)5 does not e2ist.$hoose 9es to create the /omain, and in the window which appears, type into the Short te2t bo2 adescription of the /omain. *n this e2ample, 8mployee /omain&

1,

Page 25: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 25/75

he /efinition tab, which, as shown above, opens automatically. he first available field here is/ata type, click inside the bo2 and select the drop-down menu, and a number of generic data typesalready e2isting within the ABAP dictionary will appear.

he 3)5$ type is the one to be used here for the 8mployee data, a Hcharacter string with onlydigitsI. 1nce this selection is double-clicked, it will appear in the /ata type area in the /efinitiontab.

 3e2t, in the 3o. characters field, enter the number =, indicating that the field will contain ama2imum of = characters, and in the /ecimal places area, enter . An 1utput length of = should beselected, and then press 8nter.

he 3)5$ fields description should re-appear, confirming that this is a valid entry.

 3e2t, select the Dalue range tab, which is visible ne2t to the /efinition tab ust used&

17

Page 26: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 26/75

his is where you set valid value ranges for the /omain created. 1nce this is set, any subse+uentuser entering values outside the valid value range will be shown an error message and be re+uestedto enter a valid entry. 0ere, there are three options.

• !irst, where you can see Single values, it is possible to enter a list of individual valid values

which can be entered by the user.

Second, *ntervals, where you can enter a lower and upper limit for valid values, for e2ample< and J, which saves the effort of entering J individual single values in the Single valuessection.

• "ast, the Dalue table bo2 visible at the bottom. When there are a large number of possible

entries, this is a common method 'to do this you must specify a complete valid value tableentry list, in which case it is also necessary to introduce foreign keys to the table, to ensurethe users entries are tested against the value stored in the value table created.

his e2ample /omain, however, does not re+uire any Dalue range entry, so ust click the save button

and, again, assign it as a "ocal obect.

he ne2t step is to Activate the obect, allowing other /ata elements to use this domain going

forward. *n the toolbar click the small matchstick icon 'also accessible by pressing $6" @!#.

A pop-up window appears, listing the # currently inactive obects&

*t may be possible to activate all of the obects together, but this is not advised. *n a typicaldevelopment environment, a number of people will be creating developments simultaneously, and+uite often, others obects will appear in this list.

At this point, it is only the /omain which is to be activated, the top entry labeled /15A, with thename C883)5. When this is highlighted, click the green tick continue button. he window shoulddisappear, and the status bar will display the message 1bect's activated

 3ow it is possible to proceed with the creation of the table. !orward navigation was used for generating the /omain, so click the Back button, or press !# to return to the 5aintain /ata8lement screen. As the domain is active, the description entered previously should appear by thearea where C883)5 was typed, along with other /omain properties which have been created&

15

Page 27: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 27/75

 3e2t, the !ield labels must be created, so click that tab. he !ield labels entered here will appear asfield labels in the final table. *n this e2ample they should read 8mployee, or better, 8mployee

 3umber. *f this does not fit within the area given, ust tailor it so that it still makes sense, for e2ample typing 8mployee 3 into the Short !ield label bo2. 1nce the te2t has been put into the!ield label spaces, press enter, and the "ength section will automatically be filled in&

1nce this is complete, Save and Activate the element via the toolbar at the top. he inactive obectswindow will reappear, where two inactive obects will remain. 0ighlight the /ata element 'labeled

/8" and click the green tick $ontinue button at the bottom.

Again, the status bar should display 1bect's activated.

Press the back button to return to the able maintenance screen. 0ere you will now see that the85P"1988 column has the correct /ata ype, "ength, /ecimals and Short te2t, thus indicatingthe successful creation of a /ata element and /omain being used for this !ield.

1/

Page 28: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 28/75

 3e2t, the same practices will be used to create four additional fields.

he ne2t field to create should be titled S)63A58. his time it should not be selected as a Geyfield, so do not check the bo2. he /ata element, in this instance, is labelled CS)63A58&

 3ow, forward navigation will again be used. /ouble-click KCS)63A5874 choose 9es to save thetable and 9es again to create the new /ata element. he 5aintain /ata 8lement window willappear which will be familiar from the previous steps.

*n the Short te2t bo2 type Surname /ata 8lement and title the new domain CS)63A58&

/ouble-click the new domain and save the /ata element, assigning it a "ocal obect and thenchoose 9es to create the new /omain.

he /omain maintenance screen will reappear. 8nter the short te2t Surname and, this time4 the /atatype to select is $0A6, a $haracter string.

1

Page 29: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 29/75

he number of characters and output length should both be set to ?, then press enter to be sureeverything has worked, and click the Activate button.

 3ote that the Save button has not been pressed this time, as the Activate button will also save thework automatically. 8nsure you assign the obect to the E5P development class as usual.

*n the Activate menu, select the obect 'the domain 'labeled /15A named CS)63A58 to beactivated, and click the green tick continue button. he status bar should read 1bect saved andactivated.

!ollowing this, click Back or !# to return to the 5aintain /ata element screen. 8nsure the domainattributes have appeared 'Short te2t, /ata type, "ength and so on. *n the !ield "abel tab, enter Surname in each bo2 and press 8nter to automatically fill the "ength bo2es.

13

Page 30: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 30/75

Activate the /ata element 'in the Activate menu, the /8" obect named CS)63A58S,checking the status bar to ensure this has occurred with any errors. Again, press Back to return to the5aintain able screen, where the new /ata element will be visible&

he ne2t field to be created is titled !1683A58, and the data element C!1683A58. $lick tocreate the /ata element and follow the steps above again.

*n the 5aintain /ata 8lement screen, the Short te2t should read !orename /ata 8lement and thedomain C!1683A58. Save this and choose 9es to create the domain.

he domains short te2t should read !orename. )se the $0A6 data type again and a "ength and1utput length of ?. 3e2t, Activate the /omain as before.

6eturn to the 5aintain /ata 8lement screen. ype !orename into the four !ield label bo2es. Pressenter to fill the length bo2es and then activate the /ata 8lement named C!1683A58 as before.(o back again to see the table&

'9

Page 31: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 31/75

he ne2t field will be called itle and the /ata 8lement C*"8, follow the steps above again tocreate this field with the following information&

he /ata element short te2t should read itle /ata 8lement and the domain should be namedC*"8.

he /omain Short te2t should be itle and the /ata type is again $0A6. his time the "ength and1utput length will be <. he !ield labels should all read itle.

Activate all of these and go back to view the new, fifth field in the able.

he final field which will be created for this table is for /ate of Birth. *n the !ield bo2 type /1Band create the /ata element C/1B using the steps from the previous section and this information&

he /ata element short te2t should read /ate of Birth /ata 8lement and the domain should benamed C/1B.

he /omain Short te2t should be /ate of Birth and the /ata type is, this time, /AS, after whichan information bo2 will appear to confirm this. $lick the green tick to continue&

!or the /AS data type, the "ength and 1utput lengths are set automatically at = and < 'the 1utputlength is longer as it will automatically output dividers between the day, month and year parts of thedate.

he !ield labels should all read /ate of Birth, e2cept the Short label where this will not fit, so usttype /1B here. Activate the /omain and /ata element, and return to the table.

'1

Page 32: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 32/75

Technica# Settings

1nce this has been saved, the ne2t step is to move on to maintaining the technical settings of theable. Before creating the final /atabase table, SAP will need some more information about the table

 being created. Select echnical settings via the toolbar above the table, through the (o to menu, or with the shortcut $6"@S0*!@!J.

0ere, it is important to tell the system what /ata class is to be used, so select the drop down button.here are five different options, with accompanying descriptions. !or this table, select the first,labeled APP", and double-click it&

!or the SiLe category field, again click the drop-down button. 0ere, you have to make an estimate

as to the amount of data records which will be held within the table so that the system has some ideaof how to create the tables in the underlying database. *n this instance, it will be a relatively smallamount of information, so select the first siLe category, labelled &

''

Page 33: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 33/75

Below this are the Buffering options. 0ere, Buffering not allowed should be selected&

his prevents the table contents from being loaded into memory for reading, stopping the table from

 being read in advance of the selection of the records in the program. 9ou may, correctly, point outthat it may be advantageous to hold the table in the memory for speed efficiency, but in this e2ample,this is not necessary. *f speed was an issue in a development, buffering would then be switched on,ensuring the data is read into memory. *n the case of large tables which are accessed regularly butupdated infre+uently, this is the option to choose.

 3othing else on the 5aintain echnical Settings screen needs to be filled at this point, so click Saveand then go back to the table itself. *f all of this is successful, then the table should now be in a

 position to be activated and the entry of records can begin. $lick the Activate icon to activate thetable and check the status bar, which should again read 1bect Activated.

Entering +ecor&s into a Ta#e

 3ow that the table has been created, data can be entered. o do this, enter the )tilities menu, scrollto able contents, and then $reate 8ntries&

'*

Page 34: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 34/75

A /ata-entry screen will appear which has automatically been generated from the table created. hefield names correspond here to the technical names given when we created them. o change these tothe !ield labels which we set up, enter the Settings menu and select )ser Parameters. his facilityallows you to tailor how tables look for your own specific user */. Select the !ield label radio

 button and click $ontinue&

he !ield labels created will now appear as they were defined when creating the table&

he 8mployee 3umber field is limited to = characters, and the data type was set to 3)5$, so onlynumerical characters can be entered. $reate a record with the following data&

• 8mployee 3umber & <<

• Surname & Brown

• !orename & Stephen

• itle & 5r

• /ate of Birth & <F.M.<J=&

',

Page 35: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 35/75

Press 8nter and the system will automatically put the names in upper case, and validate each field toensure the correct values were entered&

$lick Save and the status bar should state /atabase record successfully created. 3e2t, click the6eset button above the data entry fields to clear the fields for the ne2t entry.

$reate another record with the following data&

• 8mployee 3umber & <M

• Surname & Nones

• !orename & Amy

• itle & 5rs

• /ate of Birth & <=<<FJ.

 3ote that this time the /ate of Birth has been filled in without the appropriate dividers. When 8nter is pressed, the system automatically validates all fields, correcting the /ate of Birth field to thecorrect formatting itself&

'7

Page 36: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 36/75

Save, 6eset, and then further records can be entered following the same steps&

 3ote that if dates are entered in the wrong format, an error message will appear in the status bar&

,iewing the Data in a Ta#e

 3ow that data has been entered into the table, the final few steps will allow this data to be viewed.

0aving entered several data records in the manner discussed previously, click the Back key to returnto the /ictionary& /isplay able screen. o view the table created with the data entered, from the)tilities menu, select able contents and then /isplay&

'5

Page 37: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 37/75

A selection screen will then appear, allowing you to enter or choose filter values for the fields youcreated. he selection screen is very useful when you have lots of data in your table. *n this case,though, only five records have been entered, so this is unnecessary. 0owever, for e2ample if youwere to only want to focus on a single employee number, or a small range, these figures can beselected from this screen&

o view all of the records, do not enter any data here. Nust click the 82ecute button, which isdisplayed in the top left corner of the image above, or use the shortcut !=. 9ou will now see a screenshowing the data records you entered in the previous section&

'/

Page 38: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 38/75

*f further fields were to e2ist, the screen would scroll further to the right, meaning not all fields could be displayed simultaneously due to field siLe properties.

*f you want to see all of the data for one record, double-click on the record and this will be shown.Alternatively, several records can be scrolled through by selecting the desired records via the check-

 bo2es to the left of the $lient column and then clicking the $hoose icon on the toolbar&

hese can then be individually viewed and scrolled through with the 3e2t entry button&

o return to the full table then, simply click the Back button, or press !#.

82periment with the table created, using the toolbars range of options to filter and sort theinformation in a number of ways&

!or e2ample, to organiLe alphabetically by forename, click to select the !orename field, and then

click the Sort ascending button&

here are a number of things which can be achieved in this table view, and it can be a useful tool for checking the data within an SAP system without going through the transaction screens themselves.

'

Page 39: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 39/75

Chapter -: Creating a Program

o begin creating a program, access the ABAP 8ditor either via transaction code S8#=, or bynavigating the SAP menu tree to ools ABAP Workbench /evelopment, in which the ABAP8ditor is found. /ouble-click to e2ecute.

A note to begin& it is advisable to keep the programs created as simple as possible. /o not make themany more complicated than is necessary. his way, when a program is passed on to another developer to work with, fi2 bugs and so on, it will be far easier for them to understand. Add as many commentsas possible to the code, to make it simpler for anyone who comes to it later to understand what a

 program is doing, and the flow of the logic as it is e2ecuted.

he program name must adhere to the customer naming conventions, meaning that here it must beginwith the letter C. *n continuation of the e2ample from the previous chapter, in this instance the

 program will be titled KCO8mployeeO"istO<7, which should be typed into the KProgram7 field on theinitial screen of the ABAP 8ditor. 8nsure that the KSource code7 button is checked, and then click K$reate7.

'3

Page 40: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 40/75

A KProgram Attributes7 window will then appear. *n the Kitle7 bo2, type a description of what the program will do. *n this e2ample, H5y 8mployee "ist 6eportI. he 1riginal language should be setto 83, 8nglish by default, ust check this, as it can have an effect on the te2t entries displayed withincertain programs. Any te2t entries created within the program are language-specific, and can bemaintained for each country using a translation tool. his will not be e2amined at length here, but issomething to bear in mind.

*n the KAttributes7 section of the window, for the Kype7, click the drop-down menu and selectK82ecutable program7, meaning that the program can be e2ecuted without the use of a transactioncode, and also that it can be run as a background ob. he KStatus7 selected should be Kest program7,

and the KApplication7 should be KBasis7. hese two options help to manage the program within theSAP system itself, describing what the program will be used for, and also the program developmentstatus.

!or now, the other fields below these should be left empty. Particularly ensure that the K8ditor "ock7 bo2 is left clear 'selection of this will prevent the program from being edited. K)nicode checksactive7 should be selected, as should K!i2ed point arithmetic7 'without this, any packed-decimal fieldsin the program will be rounded to whole numbers. "eave the KStart using variant7 bo2 blank. hen,click the Save button.

*9

Page 41: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 41/75

he familiar K$reate 1bect /irectory 8ntry7 bo2 from the previous section should appear now, clickthe K"ocal obect7 option as before to assign the program to the temporary development class. 1ncethis is achieved, the coding screen is reached.

Co&e E&itor

0ere, focus will be put on the coding area. he first set of lines visible here are comment lines. heseseven lines can be used to begin commenting the program. *n ABAP, comments can appear in twoways. !irstly, if a is placed at the beginning of a line, it turns everything to its right into a comment.

*1

Page 42: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 42/75

 3ote that the must be in the first column on the left. *f it appears in the second column or beyond,the te2t will cease to be a comment.

A comment can also be written within a line itself, by using a H. Where this is used, everything to theright again becomes a comment. his means that it is possible to add comments to each line of a

 program, or at least a few lines of comments for each section.

he ne2t line of code, visible above, begins with the word 68P16. his is called a SA8583,and the 68P16 statement will always be the first line of any e2ecutable program created. hestatement is followed by the program name which was created previously. he line is then terminatedwith a full stop 'visible to the left of the comment.

8very statement in ABAP must be followed by a full stop, or period. his allows the statement totake up as many lines in the editor as it needs, so for e2ample, the 68P16 statement here couldlook like this&

As long as the period appears at the end of the statement, no problems will arise. *t is this periodwhich marks where the statement finishes.

*f you re+uire help with a statement, place the cursor within the statement and choose the K0elp on...7 button in the top toolbar&

*'

Page 43: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 43/75

A window will appear with the ABAP keyword automatically filled in. $lick the continue button andthe system will display help on that particular statement, giving an e2planation of what it is used for and the synta2. his can be used for every ABAP statement within an SAP system. Alternatively, this

can be achieved by clicking the cursor within the statement, and pressing the !< key.

**

Page 44: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 44/75

A further tip in this vein is to use the KABAP /ocumentation and 82amples7 page, which can beaccessed by entering transaction code ABAP/1$) into the transaction code field. he menu tree tothe left hand side on this screen allows you to view e2ample code, which one7s own code can later be

 based upon. his can either be copied and pasted into the ABAP editor, or e2perimented with inside

the screen itself using the 82ecute button to run the e2ample code.

*,

Page 45: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 45/75

6eturning to the ABAP editor now, the first line of code will be written. 1n the line below the68P16 statement, type the statement& write K08""1 SAP W16"/7.

he write statement will write whatever is in +uotes after it to the output window 'there are a number of additions which can be made to the write statement to help format the te2t, which we will return ina later chapter.

Save the program, and check the synta2 with the K$heck7 button in the toolbar 'or via $6"@ !M.he status bar should display a message reading HProgram CO85P"1988O"*SO< is syntacticallycorrectI. hen, click the KActivate7 button, which should add the word KActive7 ne2t to the programname. 1nce this is done, click the K/irect processing7 button to test the code.

he report title and the te2t output should appear like this, completing the program.

Write Statements

 3ow that the first program has been created, it can be e2panded with the addition of further ABAPstatements. )se the Back button to return from the test screen to the ABAP editor.

0ere, the tables which were created in the ABAP /ictionary during the first stage will be accessed.

he first step toward doing this is to include a table7s statement in the program, which will be placed below the 68P16 statement. !ollowing this, the table name which was created is typed in,LOemployeeOlistO<, and, as always, a period to end the statement.

*7

Page 46: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 46/75

While not essential, to keep the format of the code uniform, the Pretty Printer facility can be used.$lick the KPretty Printer7 button in the toolbar to automatically alter the te2t in line with the PrettyPrinter settings 'which can be accessed through the )tilities menu, Settings, and the Pretty Printer tab in the ABAP 8ditor section.

1nce these settings have been applied, the code will look slightly tidier, like this&

"et us now return to the AB"8S statement. When the program is e2ecuted, the AB"8S statementwill create a table structure in memory based on the structure previously defined in the ABAP/ictionary. his table structure will include all of the fields previously created, allowing the recordsfrom the table to be read and stored in a temporary structure for the program to use.

o retrieve from our data dictionary table and place them into the table structure, the S8"8$statement will be used.

ype SELECT * from z_employee_list_01. his is telling the system to select everything 'the refersto all-fields from the table. Because the S8"8$ statement is a loop, the system must be told wherethe loop ends. his is done by typing the statement 83/S8"8$. 3ow we have created a select looplet7s do something with the data we have are looping through. 0ere, the W6*8 command will beused again. 6eplace the Hwrite ‘HELLO SA !O"L#7.I line with Hwrite z_employee_list_01.I towrite every row of the table to the output window&

*5

Page 47: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 47/75

Q

$heck the code with the K$heck7 button, and it will state that there is a synta2 error.

he cursor will have moved to the AB"8S statement which was identified, along with the abovewarning. he name HCO85P"1988O"*SO<I appears to be incorrect. o check this, open a new

session via the 3ew Session button in the toolbar . 82ecute the ABAP /ictionary with transactioncode S8<<, search for C in the K/atabase table7 bo2 and it will bring back the table C85P"1988S,meaning that the initial table name CO85P"1988O"*SO< was wrong. $lose the new session andthe synta2 error window and type in the correct table name KC85P"1988S7 after the AB"8Sstate. 9our screen should look like this.

Save the program and check the code, ensuring the synta2 error has been removed, and then click theest button '!= and the output window should display every row of the table.

"ook at the data in the output window. he system has automatically put each line from the table ona new row. he W6*8 statement in the program did not know that each row was to be output on a

*/

Page 48: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 48/75

new line4 this was forced by some of the default settings within the system regarding screen settings,making the line length correspond to the width of the screen. *f you try to print the report, it could bethat there are too many columns or characters to fit on a standard sheet of A?. With this in mind, it isadvisable to use an addition to the 68P16 statement regarding the width of each line.

6eturn to the program, click the 68P16 statement and press the !< key and observe the "*38S*C8 addition which can be included&

*n this e2ample, add the "*38-S*C8 addition to the 68P16 statement. 0ere, the line will belimited to ? characters. 0aving done this, see what difference it has made to the output window. helines have now been broken at the ? character limit, truncating the output of each line.

*

Page 49: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 49/75

Bear these limits in mind so as to avoid automatic truncation when printing reports. !or a standardsheet of A? this limit will usually be <#M characters. When the limit is set to this for the e2ampletable here, the full table returns, but the line beneath the title K5y 8mployee "ist 6eport7 displays the

 point at which the output is limited.

 3e2t, the program will be enhanced somewhat, by adding specific formatting additions to theW6*8 statement. !irst, a line break will be inserted at the beginning of every row that is output.

/uplicate the previous S8"8$ R 83/S8"8$ statement block of code and place a K%7 after theW6*8 statement. his will trigger a line break&

Save and e2ecute the code. he output window should now look like this.

he first S8"8$ loop has created the first five rows, and the second has output the ne2t five.

*3

Page 50: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 50/75

Both look identical. his is due to the "*38-S*C8 limit in the 68P16 statement, causing the firstfive rows to create a new line once they reached <#M characters. *f the "*38-S*C8 is increased to, for e2ample #M, the effects of the different W6*8 statements will be visible.

he first five rows, because they do not have a line break in the W6*8 statement, have appeared onthe first line up until the point at which the #M character limit was reached and a new line wasforced. he first four records were output on the first line. he th record appears on a line of its ownfollowed by the second set of five records, having had a line break forced before each record was

output.6eturn the "*38-S*C8 to <#M, before some more formatting is done to show the separation betweenthe two different S8"8$ loops.

Above the second S8"8$ loop, type $L%&E . his means underline.

$lick the )"*38 statement and press !< for further e2planation from the /ocumentation window,which will state HWrites a continuous underline in a new line.I /oing this will help separate the two

different S8"8$ outputs in the code created. 82ecute this, and it should look like so.

,9

Page 51: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 51/75

/uplicate the previous S8"8$ R 83/S8"8$ statement block of code again, including the)"*38, to create a third S8"8$ output. *n this third section, remove the line break from theW6*8 statement and, on the line below, type HW6*8 %.I his will mean that a new line will beoutput at the end of the previous line. 82ecute this to see the difference in the third section.

 3ow, create another S8"8$ loop by duplicating the second S8"8$ loop. his time the W6*8statement will be left intact, but a new statement will be added before the S8"8$ loop& S'% ,which means to skip a line. his can have a number added to it to specify how many lines to skip, inthis case M. *f you press !< to access the documentation window it will e2plain further, including the

ability to skip to a specific line. he code for this section should look like the first image, and whene2ecuted, the second.

,1

Page 52: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 52/75

1ur program should now look as shown below. $omments have been added to help differentiate thee2amples.

Output !n&ivi&ua# "ie#&s

$reate another S8"8$ statement. his time, instead of outputting entire rows of the table,individual fields will be output. his is done by specifying the individual field after the W6*8statement. 1n a new line after the S8"8$ statement add the following line W6*8 % Lemployees-surname. 6epeat this in the same S8"8$ loop for fields !orename and /1B. hen e2ecute thecode&

,'

Page 53: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 53/75

o tidy this up a little remove the % from the last M W6*8 statements which will make all # fieldsappear on < line.

Chaining Statements Together

We have used the W6*8 statement +uite a lot up to now and you will see it appear on a regular basis in many standard SAP programs. o save time, the W6*8 statements can be chained together,avoiding the need to duplicate the W6*8 statement on every line.

o do this, duplicate the previous S8"8$ loop block of code. After the first W6*8 statement, addH&I his tells the SAP system that this W6*8 statement is going to write multiple fields 'or te2tliterals. After the HLemployees-surnameI field change the period '. to a comma ', and remove thesecond and third W6*8 statements. $hange the second period '. to comma ', also but leave thelast period '. as is to indicate the end of the statement. his is how we chain statements together andcan also be used for a number of other statements too.

82ecute the code, and the output should appear e2actly the same as before.

,*

Page 54: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 54/75

Copy .our Program

"et7s now switch focus a little and look at creating fields within the program. here are two types offield to look at here, Dariables and $onstants.

!irstly, it will be necessary to generate a new program from the ABAP 8ditor. his can be doneeither with the steps from the previous section, or by copying a past program. he latter option is

useful if you plan on reusing much of your previous code. o do this, launch transaction S8#= againand enter the original program7s name into the KProgram7 field of the K*nitial7 screen, and then clickthe $opy button '$6" @ !.

A window will appear asking for a name for the new program, in this instance, enter Z_EMPLOYEE_LIST_2 in the Karget Program7 input bo2, then press the Copy button.

he ne2t screen will ask if any other obects are to be copied. Since none of the obects here have

 been created in the first program, leave these blank, and click Copy.

,,

Page 55: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 55/75

 he K$reate 1bect /irectory 8ntry7 screen will then reappear and, as before you should assign theentry to K"ocal obect7.

he status bar will confirm the success of the copy. Please note that the program must be activated before coping to another one.

he new program name will then appear in the KProgram7 te2t bo2 of the ABAP 8ditor *nitial screen. 3ow click the $hange button to enter the coding screen.

he copy function will have retained the previous report name in the comment space at the top ofyour program and in the initial 68P16 statement, so it is important to remember to update these.Also, delete the "*38-S*C8 limit, so that this does not get in the way of testing the program.

Because there are a number of S8"8$ and W6*8 statements in the program, it is worth looking athow to use the fast comment facility. his allows code to be, in practical terms, removed from the

 program without deleting it, making it into comments, usually by inserting an asterisk ' at the

 beginning of each line. o do this +uickly, highlight the lines to be made into comment and holddown $6" @ . his will automatically comment the lines selected. Alternatively, the te2t can hehighlighted and then in the K)tilities7 menu, select KBlock%Buffer7 and then K*nsert $omment 7. heselected code is now converted to comment.

,7

Page 56: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 56/75

/elete most of the code from the program now, retaining one section to continue working with.

Dec#aring ,aria#es

A field is a temporary area of memory which can be given a name and referenced within programs.!ields may be used within a program to hold calculation results, to help control the logic flow and,

 because they are temporary areas of storage 'usually held in the 6A5, can be accessed very fast,helping to speed up the program7s e2ecution. here are, of course, many other uses for fields.

,5

Page 57: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 57/75

he ne2t +uestion to e2amine is that of variables, and how to declare them in a program. A variableis a field, the values of which change during the program e2ecution, hence of course the termvariable.

here are some rules to be followed when dealing with variables&

• hey must begin with a letter.

$an be a ma2imum siLe of # characters,• $annot include @ , & or ' in the name,

• $annot use a reserved word.

When creating variables, it is useful to ensure the name given is meaningful. 3aming variablesthings like A<, AM, A# and so on is only likely to cause confusion when others come to work with the

 program. 3ames like, in the e2ample here, KSurname7, K!orename7, K/1B7 are much better, as fromthe name it can be ascertained e2actly what the field represents.

Dariables are declared using the /AA statement. he first variable to be declared here will be aninteger field. Below the section of code remaining in your program, type the statement DATA

followed by a name for the field - integer/1. hen, the data type must be declared using the wordT.PE and for integers this is referred to by the letter i. erminate the statement with a period.

ry another, this time named packe&0&ecima#/1, the data type for which is p. A packed decimalfield is there to help store numbers with decimal places. *t is possible to specify the number of decimal places you want to store. After the Kp7, type the word decimals and then the number desired,in this instance, M 'packed decimal can store up to <? decimal places. ype all of this, then save the

 program.

hese data types used are called elementary. hese types of variables have a fi2ed length in ABAP,so it is not necessary to declare how long the variables need to be.

here is another way of declaring variables, via the %!E addition to the /AA statement. /eclareanother variable, this time with the name packe&0&ecima#/* but, rather than using the 9P8addition to define the field type, use the word "*G8, followed by the previous variable7s nameHpackedOdecimal<I. his way, you can ensure subse+uent variables take on e2actly the same

 properties as a previously created one. $opy and paste this several times to create packedOdecimal#and ?.

*f you are creating a large number of variables of the same data type, by using the "*G8 addition, alot of time can be saved. *f, for e2ample, the /8$*5A"S part were to need to change to #, it wouldthen only be necessary to change the number of decimals on the original variable, not all of themindividually.

,/

Page 58: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 58/75

Additionally, the "*G8 addition does not only have to refer to variables, or fields, within the program. *t can also refer to fields that e2ist in tables within the SAP system. *n the table we createdthere was a field named KSurname7. $reate a new variable called new0surname using the /AAstatement. When defining the data type use the "*G8 addition followed by 2emp#oyees3surname ./efining fields this way saves you from having to remember the e2act data type form every field youhave to create in the SAP system.

$heck this for synta2 errors to make sure everything is correct. *f there are no errors remove the

newOsurname, packedOdecimalM, # and ? fields as they are no longer needed.

With another addition which can be made to the /AA statement, one can declare initial values forthe variables defined in the program. !or the Hinteger<I variable, after H9P8 iI, add the followingaddition& ,A%4E **. his will automatically assign a value of MM to Hinteger<I when the programstarts.

!or packed decimal fields the process is slightly different. he DA")8 here must be specified withinsingle +uotation marks, K5657 as without these, the ABAP statement would be terminated by the

 period in the decimal. 3ote that one is not ust limited to positive numbers. *f you want to declare avalue of a negative number, this is entirely possible&

Constants

A constant is a variable whose associated value cannot be altered by the program during its

e2ecution, hence the name. $onstants are declared with the $13SA3S statement 'where the

/AA statement appeared for variables. When writing code then, the constant can only ever be

referred to4 its value can never change. *f you do try to change a $onstant7s value within the

 program, this will usually result in a runtime error.

he synta2 for declaring constants is very similar to that of declaring variables, though there are a

few differences. 9ou start with the statement CO)STA)TS. )se the name myconstant/1 for this

e2ample. (ive it a type p as before with < decimal place and a value of K 7677. $opy and paste and try

another with the name myconstant/*, this time a standard integer 'type Ki7 with a value of 7.

'A note& one cannot define constants for data types TS6*3(S, references, internal tables or

structures containing internal tables.

,

Page 59: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 59/75

Chapter 8:

Arithmetic 9 A&&ition

 3ow that the ability to create variables has been established, these can be used for calculationswithin a program. his chapter will begin by looking at some of the simple arithmetical calculations

within ABAP.

1ur program will be tidied up by removing the two constants which were ust created. *f a program

needs to add two numbers together and each number is stored as its own uni+ue variable, the product

of the two numbers can be stored in a brand new variable titled HresultI.

$reate a new /AA statement, name this Hresu#tI and use the "*G8 statement to give it the same

 properties as packedOdecimal<, terminating the line with a period.

o add two numbers together, on a new line, type Hresu#t integer/1 ; packe&0&ecima#/1.I 1n anew line enter, HW+!TE resu#t.I Activate and test the program, and the result will appear in the

output screen&

Things to rememer: !or any arithmetical operation, the calculation itself must appear to the right

of the U, and the variable to hold the result to the left. his ensures that only the result variable will

 be updated in the e2ecution. *f the variable titled HresultI had been assigned a value prior to the

calculation, this would be overwritten with the new value. Spaces must always be inserted on either

side of the U and @ signs. his applies to all arithmetical operators, including parentheses ' , which

will start to be used as the calculations become more complicated. 3ote that one space is the

minimum, and multiple spaces can be used, which may help in lining code up to make it more

readable, and indeed where calculations may be defined over many lines of code.

*t is not ust the products of variables which can be calculated in calculations, but also individualliteral values, or a mi2ture of the two, as shown here&

,3

Page 60: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 60/75

Arithmetic 9 Sutraction

o subtract numbers, the same method is used, replacing the @ with a -. $opy and paste the previous

calculation and make this change. Also, to make this simpler to understand, change the value of

 packedOdecimal< from -. to .. 1ne can see by doing this the way that changing the initial

variable will alter the calculation.

82ecute the code&

Arithmetic 9 Division

o divide numbers, the same method is followed, but the arithmetical operator this time will be a <

Arithmetic 9 'u#tip#ication

o multiply, the operator is a

79

Page 61: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 61/75

Additionally to these methods, the statements ADD= S4$T+ACT= D!,!DE and '4%T!P%. can be

used. he synta2 for these is slightly different. Beneath the first calculation 'where integer< and

 packedOdecimal< where added, write a new line of code HADD > to resu#t.I '*gnore the comment

line in the image.

While this is a legitimate method for calculations, it must be added that this is very rarely used, as

the initial method is much simpler.

Conversion +u#es

*n this program, different data types have been used when declaring variables. *t is the responsibility

of the programmer to ensure the data types used are compatible with one another when used for

calculations or moving data to and from obects. 1ne should not attempt calculations with variables

and numbers which do not match.

!or e2ample, a variable defined as an integer cannot be multiplied by a character, as these two data

types are incompatible. his would cause the system to generate synta2 and runtime errors when the

 program is e2ecuted. While SAP has built in automatic data type conversions for many of the

standard data types within ABAP, there are scenarios where the inbuilt conversion rules are not

appropriate. *t is important to become familiar with the inbuilt conversion rules and know when to

manipulate the data prior to using them in calculations. 0ere, some e2amples of conversion rules will

 be given, so that they can be used throughout programs created.

$onversion rules are pre-defined logic that determine how the contents of the source field can be

entered into a target field. *f one attempts to insert an integer field containing the value of < to a

character string, the built-in conversion rules will determine e2actly how this should be done without

any synta2 or runtime errors.

71

Page 62: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 62/75

!or e2ample, create a /AA statement with the name Hnum1I of  T.PE p 'packed decimal with

DEC!'A%S * and a ,A%4E of K-6--7. hen create another variable with the name Hresu#t1I of

type i 'integer. Attempt the calculation Hresu#t1 num1I. he conversion rule here would round the

number to the closest integer, in this case #.

As you work with different data types, these kinds of conversion rules will often be applied

automatically, and it is up to you to understand these conversion rules and the data types used within

the program to ensure no runtime errors occur.

Division ,ariations

 3ow, a slight step back will be taken to discuss the division operator further. *n ABAP, there are

three ways in which numbers can be divided&

• he standard result with decimal places.

• he remainder result.

• he integer result.

The stan&ar& form of &ivision6

$reate M variables, HnumaI and HnumI, with values of 5685 and 168> respectively, then create the

variable Hresu#t*I 'also with M decimal places. hen insert the calculation Hresu#t* numa <

num.I followed by a W+!TE statement for resultM. 82ecute the program.

7'

Page 63: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 63/75

The integer form of &ivision6

$opy the initial calculation4 change the initial variables to HnumcI and Hnum&I and the resulting

variable to Hresu#t-I. !or integer division, rather than using the standard <, use the operator D!,.

his will give the result of the calculation7s integer value, without the decimal places.

The remain&er form of &ivision6

!ollow the steps from the integer form, this time with HnumeI, Hnumf I and Hresu#t8I. !or this type

of division, the arithmetical operator should be 'OD. his, when e2ecuted, will show the remainder 

value.

7*

Page 64: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 64/75

Chapter 5: Character Strings

Dec#aring C an& ) "ie#&s

his chapter will discuss character strings. When creating programs, fields defined as characterstrings are almost always used. *n SAP, there are two elementary data types used for character

strings. hese are data type C, and data type ).

Data type C6

/ata type $ variables are used for holding alphanumeric characters, with a minimum of < character

and a ma2imum of F,# characters. By default, these are aligned to the left.

Begin this chapter by creating a new program. !rom the ABAP 8ditor7s initial screen, create a new

 program, named H?0Character0StringsI. itle this HCharacter Strings E@amp#esI, set the ype to

KE@ecuta#e program7, the Status to KTest program7, the Application to K$asis7, and Save.

$reate a new /AA field, name this HmycharI and, without any spaces following this, give a

number for the length of the field in parentheses. hen, include a space and define the 9P8 as c

his is the long form of declaring a type c field. Because this field is a generic data type, the system

has default values which can be used so as to avoid typing out the full length of the declaration. *f

7,

Page 65: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 65/75

you create a new field, named Hmychar*I and wish the field to be < character in siLe, the default

field siLe is set to 1 character by default, so the siLe in brackets following the name is unnecessary.

Also, because this character field is the default type used by the system, one can even avoid defining

this. *n the case of mycharM, the variable can be defined with only the field name. he code in the

image below performs e2actly the same as if it was typed H&ata mychar*1B type cI&

*n the previous chapter, the table HLemployeesI included various fields of type c, such as

HLsurnameI. *f one uses the AB"8S statement followed by Lemployees, then by double-clicking the

table name to use forward navigation and view the table, one can see that the HsurnameI field is of

data type $0A6, with length ?. his declaration can be replicated within the ABAP program&

6eturn to the program, and in place of mycharM, create a new field named H 2emp#oyees1I, with a

length of 8/ and type c. his will have e2actly the same effect as the previous declaration. 6eferring

 back to previous chapter, another way of doing this would be to use the "*G8 statement to declare

Lemployees 'or this time LemployeesM as having the same properties as the HsurnameI field in the

table.

Data type )6

he other common generic character string data type is 3. hese are by default right-aligned. *f one

looks at the initial table again, using forward navigation, the field named HemployeeI, which refers to

employee numbers, is of the data type 3)5$, with a length of =. 3)5$, or the number data type,

works similarly to the character data type, e2cept with the inbuilt rule to only allow the inclusion of

numeric characters. his data type, then, is ideal when the field is only to be used for numbers with

no intention of carrying out calculations.

o declare this field in ABAP, create a new /AA field named H2numer1I, 9P8 n. Again,

alternatively this can be done by using the "*G8 statement to refer back to the original field in the

table.

77

Page 66: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 66/75

String 'anipu#ation

"ike many other programming languages, ABAP provides the functionality to interrogate and

manipulate the data held in character strings. his section will look at some of the popular statements

which ABAP provides for carrying out these functions&

$oncatenating String !ields.• $ondensing $haracter Strings.

• !inding the "ength of a String.

• Searching for Specific $haracters.

• he S0*! statement.

• Splitting $haracter Strings.

• Sub!ields.

Concatenate

he concatenate statement allows two character strings to be oined so as to form a third string. !irst,

type the statement CO)CATE)ATE into the program, and follow this by specifying the fields, here

Hf1I, Hf*I and so on. hen select the destination which the output string should go to, here H&1I. *f

one adds a subse+uent term, separate& y sep 'HsepI here is an e2ample name for the separator

field, this will allow a specified value to be inserted between each field in the destination field&

 3ote& *f the destination field is shorter than the overall length of the input fields, the character stringwill be truncated to the length of the destination field, so ensure when using the $13$A83A8

statement, the string data type is being used, as these can hold over F, characters.

As an e2ample, observe the code in the image below.

he first # fields should be familiar by now. he fourth is the separator field, here again called HsepI

'the siLe of sep has not been defined here, and so it will take on the default which the system uses - <

character. he last field is titled HdestinationI, M characters long and of data type c.

Below this section is the $13$A83A8 statement, followed by the fields to combine together into

the destination field. he W6*8 statement is then used to display the result.

82ecuting this code will output the following&

75

Page 67: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 67/75

 3ote that the te2t has been aligned to the left, as it is using data type c. Also, the code did not include

the SEPA+ATED $. addition, and so the words have been concatenated without spaces. his can

 be added, and spaces will appear in the output&

Con&ense

 3e2t, the $13/83S8 statement. 1ften an ABAP program will have to deal with large te2t fields,

with unwanted spaces. he $13/83S8 statement is used to remove these blank characters.

 3ow, observe the code below&

his should, of course, be mostly familiar from the last section, with the addition of the new, M-

character Hspace&0nameI field, with large spaces between the individual words.

Below we have an e2ample of using the $13/83S8 statement using our new variable.

7/

Page 68: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 68/75

he $13/83S8 statement will remove the blank spaces between words in the variable, leaving

only < character7s space.

)O3APS

An optional addition to the $13/83S8 statement is )O3APS, which as you may guess, removes

all spaces from our variable.

"in& the %ength of a String

o find the length of a string, a function rather than a statement is used. Added beneath the previous

data fields here, is a new one titled H #enI, with a 9P8 i, so as to ust hold the integer value of the

string length.

he code to find the length of the Ksurname7 field and display it in the Klen7 field appears like this,

with Hstr#enI defining the function&

he output, then, will appear like this&

7

Page 69: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 69/75

+ep#ace

Below * have created the Hsurname*I field and is ? characters in length. 3ote that no 9P8 has

 been defined, so the system will use the default type, c&

Some te2t is then moved into the field after which the +EP%ACE statement is used to replace the

comma with a period&

1ne thing to note here is that the 68P"A$8 statement will only replace the first occurrence in the

string. So if, for e2ample, the surnameM field read H5r, Noe, SmithI, only the first comma would be

changed. All occurrences of comma7s could be replaced by making use of a while loop, which will

 be discussed later on.

Search

 3e2t, a look will be taken at searching for specific character strings within fields. )nsurprisingly, the

statement SEA+CF is used for this.

All that is needed is to enter SEA+CF followed by the field which is to be searched, in this instance

the surnameM field. hen the string which is to be searched for, for e2ample, KGoe7&

 3ote that here no variable has been declared to hold the result. *n the case of the S8A6$0 statement,two system variables are used. he first is Hsy3surcI, which identifies whether the search was

successful or not, and the second is Hsy3f&posI, which, if the search is successful, is set to the

73

Page 70: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 70/75

 position of the character string searched for in surnameM. Below, a small report is created to show the

values of the system variables.

SEA+CF E@amp#e 1

he first S8A6$0 statement, below, indicates that the surnameM field is being searched, for the

character string KNoe K. he Search statement will ignore the blank spaces. he output will show the

string being searched for, followed by the system variables and the value results. *n this case, the

search should be successful.

SEA+CF E@amp#e *

he ne2t e2ample is very similar, but the full stops either side of K 6Goe 67 mean that the blank spaces

this time will not be ignored and the system will search for the full string, including the blanks. 0ere,

the search will be unsuccessful, as the word KGoe7 in the SurnameM field is not followed by four blank 

spaces.

59

Page 71: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 71/75

SEA+CF E@amp#e -

his third search uses a wild card character K7 and will search for any words ending in K ith7. his,

again, should be successful.

SEA+CF E@amp#e 8

he last e2ample also uses the wild card facility, this time to search for words beginning with K Smi7,

which again should be successful. $ompare the places in the code where the appears in this and the

 previous e2ample.

6un a test on these searches, and output returns as follows&

When the sy3surc U this refers to a successful search. When sy3surc U ? in the second e2ample

this indicates that the search was unsuccessful.

51

Page 72: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 72/75

*n the first search, the sy3f&pos value of # refers to the third character in the surnameM field, the

offset , and the search term appears one character after this. he failure of the second search means

that a is displayed in the sy-fdpos field. he value of > in the sy-fdpos fields for the final two

searches both mean that the word KSmith7 was found, corresponding to the search terms, and that the

searched word appears < character after the offset value.

Shift

he SF!"T statement is a simple statement that allows one to move the contents of a character string

left or right, character by character. *n this e2ample, a field7s contents will be moved to the left,

deleting leading Leros. /eclare a new /AA variable as follows& Hemp#0numI, 1/ characters long,

and set the content of the field to K////758-*17, filling all < characters of the field&

)sing the S0*! statement, then, the ? Leros which begin this character string will be removed, and

the rest moved across to the left. ype the statement S0*!, followed by the field name. /efine that

it is to be shifted to the left, deleting leading Leros 'don7t forget the help screen can be used to view

similar additions which can be added to this statement. hen include a W6*8 statement so that the

result of the S0*! statement can be output. o the right of the number here, there will be four

spaces, which have replaced the leading Leros&

*f no addition to the S0*! statement is specified, the system will by default move everything ust

one character to the left, leaving one space to the right&

he C!+C4%A+  addition to the S0*! statement will cause, by default, everything to move one

space to the left again, but this time the character which is displaced at the beginning of the statement

will reappear at the end, rather than leaving a blank space&

5'

Page 73: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 73/75

Sp#it

he SP"* statement is used to separate the contents of a field into two or more fields.

1bserve the code below&

he first section contains several /AA statements, HmystringI, Ha1I, Ha*I, Ha-I and Hsep*I, along

with their lengths and types. HSepMI here is a separator field, with a value of K7.

HmystringI is then given a value of K 1*-8HH A$CD HH7I>J7, followed by a comment line 'which

the program will ignore, then a W6*8 statement, so that this initial value appears in the outputfollowed by a blank line, using the SG*P statement.

he SP"* statement appears, followed by the name of the string which is to be split. he AT 

addition appears ne2t, telling the program that, where HsepMI appears 'remember the value of this is

K7, the field is to be split. !ollowing this, the !)TO then specifies the fields which the split field is

to be written to. he slightly odd positioning of the spaces in the value of HmystringI will, when the

statement is output, make clear the way that the SP"* statement populates the fields which the data

is put into. 82ecute the code, and this is the result&

9ou can see that the initial field has been split into a<, aM and a# e2actly where the appeared,

leaving a leading space in the first two fields, but not in the third. Additionally, on closer inspection

there are blank spaces following the numbers in each field up to its defined length, which is <.

his ne2t e2ample shows the initial value of HmystringI now is made into a comment line, and the

comment line becomes part of the code&

5*

Page 74: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 74/75

Kmystring7 now contains the original contents plus a further set of characters. While the contents are

still to be split into # fields, the data suggests it should be split into ?. *n this case, with less fields

than those defined, the system will include the remainder of the string in the final field. 3ote that if

this field is not long enough for the remainder, the result would be truncated.

Su"ie#&s

Within ABAP, you have the option of referring to specific characters within a field. his is referred

to as processing subfields, whereby a specific character7s position within its field is referenced.

Again, observe the code below&

o start with, new /AA variables are declared, Hint0te#ephone0numI, Hcountry0co&eI and

Hte#ephone0numI, along with lengths and types. !ollowing this, a character string is assigned to

intOtelephoneOnum, a W6*8 statement for this string and a blank line.

 3e2t, the subfield processing appears. he first line states the country_code field is to be filled withthe first # characters of the int_telephone_num field, indicated by the number in brackets.

5,

Page 75: ABAP Chapter 1 to 5.docx

7/18/2019 ABAP Chapter 1 to 5.docx

http://slidepdf.com/reader/full/abap-chapter-1-to-5docx 75/75

hen, the field telephone_num is to be filled with 1- characters of the int_telephone_num field,

starting after the ?th character. he ;8 part of the code here refers to where the field is to begin. hen

we have W6*8 statements for both of the fields.

his last e2ample indicates that the specific characters of int_telephone_num moved to the

country_code field will be replaced, after the first character, by the literal, M-character value K/17,

showing that a subfield can itself be edited and updated without changing the initial field. he resultsshould look like this&

Subfields are regularly used in SAP to save time on creating unnecessary variables in memory. *t is ust as easy to use the subfield synta2.