Day 2

Your First ABAP/4 Program


After you complete this chapter, you should be able to:

Before Proceeding

Before proceeding, you should:

Many of the procedures in this book are demonstrated using ScreenCams. ScreenCams are like movies; they show a series of screens, including keystrokes and mouse movements, with a descriptive voiceover. They can be found on the CD-ROM that comes with this book. See the readme.txt file found in the root directory of the CD-ROM for more information.

Exploring the Development Environment

A development object is anything created by a developer. Examples of development objects are programs, screens, tables, views, structures, data models, messages, and includes.

The R/3 system contains tools for creating and testing development objects. These tools are located in the R/3 Development Workbench. To access any development tool, you go to the workbench.

The workbench contains these tools to help you create development objects:

The following testing and search tools are also available:

All development objects are portable, meaning that you can copy them from one R/3 system to another. This is usually done to move your development objects from the development system to the production system. If the source and target systems are on different operating systems or use different database systems, your development objects will run as-is and without any modification. This is true for all platforms supported by R/3. (For a list of supported hardware and operating systems, refer to Table 1.1.)

Discovering Program Types

There are two main types of ABAP/4 programs:

Defining Reports

The purpose of a report is to read data from the database and write it out. It consists of only two screens (see Figure 2.1).

Figure 2.1:The selection screen and the output screen.

The first screen is called the selection screen. It contains input fields allowing the user to enter criteria for the report. For example, the report may produce a list of sales for a given date range, so the date range input fields would appear on the report's selection screen.

The second screen is the output screen. It contains the list. The list is the output from the report, and usually does not have any input fields. In our example, it would contain a list of the sales that occurred within the specified date range.

The selection screen is optional. Not all reports have one. However, all reports generate a list.

In this book, you will learn how to create report programs.

Defining Dialog Programs

Dialog programs are more flexible than reports, and so are more complex at the program level. They can contain any number of screens, and the screen sequence can be changed dynamically at run time. On each screen, you can have input fields, output fields, pushbuttons, and more than one scrollable area.

Discovering Report Components

ABAP/4 reports consist of five components (shown in Figure 2.2):

Figure 2.2.The components of an ABAP/4 program.

Only the source code and program attribute components are required. The rest of the components are optional.

All development objects and their components are stored in the R/3 database. For example, the source code for a report is stored in database table dd010s.

Discovering the Program Runtime Object

ABAP/4 programs are interpreted; they are not compiled. The first time you execute a program, the system automatically generates a runtime object. The runtime object is a pre-processed form of the source code. However, it is not an executable that you can run at the operating system level. Instead, it requires the R/3 system to interpret it. The runtime object is also known as the generated form of the program.

If you change the source code, the runtime object is automatically regenerated the next time you execute the program.

Introducing Program Naming Conventions

The company you work for is a customer of SAP. Therefore, programs that you create at your company are called customer programs.

Customer development objects must follow naming conventions that are predefined by SAP. These conventions are called the customer name range. For programs, the customer name range is two to eight characters long and the program name must start with the letter y or z. SAP reserves the letters a through x for their own programs.

Please take a moment now to choose a unique 3-character identifier for your programs. Within this book, I'll refer to this identifier as your handle. It must begin with a y or z. For example, you might use the letter z followed by your two initials. The notation will indicate where you should use your handle. For example, if you chose zkg and you see the direction "Enter a program name of abc," you would enter zkgabc. I recommend that as you go through this book, you should use your handle as the first three characters of all development objects you create. If you do, they will be easy to recognize later, and easy to find.

The program naming conventions adopted for this book are as follows:

The setup program that creates development objects and loads them with data for the exercises is called y-setup. If necessary, it can be re-run at any time to restore the exercise data to its original condition. To remove all the development objects and data created by the setup program from the system, run y-uninst. See the readme.txt file on the CD-ROM for more information.

Creating Your First Program

What follows is a description of the process you will follow to create a program.

When you sign on to R/3 to create your first ABAP/4 program, the first screen you see will be the SAP main menu. From there, you will go to the Development Workbench, and then to the editor. You will enter a program name, and create the program. The first screen you will see will be the Program Attributes screen. There, you must enter the program attributes and save them. You will then be allowed to proceed to the source code editor. In the source code editor, you'll enter source code, save it, and then execute the program.

Start the ScreenCam "How to Create Your First Program" now.

Follow this procedure to create your first program. Help with common problems is given in the Troubleshooter that follows it. If you have trouble with any one of the steps, don't forget to consult the Troubleshooter.

  1. From the R/3 main menu, select the menu path Tools->ABAP/4 Workbench. A screen with the title ABAP/4 Development Workbench is displayed.
  2. Press the ABAP/4 Editor button on the application toolbar. The ABAP/4 Editor: Initial Screen is displayed.
  3. In the Program field, type the program name 0201.
  4. Press the Create button. The ABAP/4: Program Attributes screen is displayed. The fields containing question marks are required.
  5. Type My First ABAP/4 Program in the Title field. By default, the contents of this field will appear at the top of the list.
  6. Type 1 in the Type field. A 1 indicates the program is a report.
  7. Type an asterisk (*) in the Application field. The value in the Application field indicates to which application area this program belongs. The complete list of values can be obtained by positioning your cursor on this field and then clicking on the down-arrow to the right of it. For example, if this program belongs to Inventory management, you would put an L in the Application field. Since this is a simple test program, I have used an asterisk to indicate that this program does not belong to any particular application area.
  8. Tickmark the Editor Lock check box. Enabling Editor lock will prevent changes to the program by anyone other than the creator. For your exercises, tickmark this box to safeguard your programs from accidental modification by others. However, you should not use this to lock actual development programs. It will prevent other programmers from maintaining them later.
  9. To save the program attributes, press the Save button on the Standard toolbar. The Create Object Catalog Entry screen is displayed.
  10. Press the Local Object button. The program attributes screen is redisplayed. In the status bar at the bottom of the screen, the message "Attributes for program saved" appears. (Note: the message you see will contain the program name too, but since this name will vary for each user, it is left out of the text in the book. This convention of leaving the development object name out of the message will be carried throughout the book.)
  11. Press the Source Code button on the application toolbar. The ABAP/4 Editor: Edit Program screen is displayed.
  12. Choose the menu path Settings->Editor Mode. The Editor: Settings screen is displayed.
  13. Choose the radio button PC Mode With Line Numbering.
  14. Choose the radio button Lower Case.
  15. Press the Copy button (the green check mark). You have now saved your editor settings. (Editor settings only need to be set once.)
  16. Look at line 1. If it does not contain the statement report 0201., type it now, as shown in Listing 2.1.
  17. On line 2, type write 'Hello SAP world'. Use single quotes and put a period at the end of the line.
  18. Press the Save button on the Standard toolbar.
  19. To execute your program, choose the menu path Program->Execute. A screen with the title My First ABAP/4 Program is displayed, and the words Hello SAP world are written below it. This is the output of the report, also known as the list.


Listing 2.1  Your First ABAP/4 Program

1 report ztx0201.
2 write 'Hello SAP World'.

The code in Listing 2.1 produces this output:

Hello SAP World

Congratulations, you have just written your first ABAP/4 program! To return to the editor, press the green arrow button on the standard toolbar (or the F3 key).

These are the Common Problems Encountered While Creating a Program and Their Solutions
Trouble
Solution
When you press the Create button, you get a dialog box saying Do Not Create Objects in the SAP Name Range. You have entered the wrong program name. Your program names must start with y or z. Press the Cancel button (the red X) to return and enter a new program name.
When you press the Create button, you get a dialog box with an input field asking for a key. You have entered the wrong program name. Your program names must start with y or z. Press the Cancel button (the red X) to return and enter a new program name.
You are getting a Change Request Query screen asking for a Request Number. On the Create Object Catalog Entry screen, do not enter a value in the Development class field.
 Press the Local Object button instead.

Exploring the Source Code Editor

In this section you will learn how to harness the power of the ABAP/4 editor. You will learn to use two screens in this section:

Using the Editor: Initial Screen

The ABAP/4 Editor: Initial Screen is shown in Figure 2.3. From there, you can display or change all program components. For example, to change the source code component, choose the Source Code radio button and then press the Change button. Or, to display the attributes component, choose the Attributes radio button and then press the Display button.

Figure 2.3.From the ABAP/4 Editor: Initial Screen you can display or change program com-ponents.

TIP
In Figure 2.3, notice that the Object Components group box encloses radio buttons, the Display button, and the Change button. When you see a group box enclosing both radio buttons and pushbuttons, the radio buttons deter-mine the component acted upon by the enclosed pushbuttons. The effect of the radio buttons is limited by the group box; they have no effect on push-buttons outside the box.

Pressing the Change button displays the selected component in change mode, which enables you to change the component.

Exploring the Functionality of the Source Code Editor

From the ABAP/4 Editor: Initial Screen, choose the Source Code radio button and press the Change button. The ABAP/4 Editor: Edit Program screen is shown, as in Figure 2.4.

Figure 2.4.Use the ABAP/4 Editor: Edit Program screen to change com-ponent functionality.

TIP
Many developers find the R/3 user interface complex and therefore difficult to learn. I suggest that you adopt a methodical approach that enables you to become comfortable with each new screen. Whenever you come across a new screen, slowly scan the menu items and buttons. Begin at the top left corner of the screen and work down and to the right. Hold your cursor over each item long enough to read it and its accompanying ToolTip. Taking a few minutes on each new screen will help you become familiar with the functions available.


Exploring the Standard Toolbar

Start the ScreenCam "Exploring the Standard Toolbar" now.

The Standard toolbar controls are shown in Figure 2.5.

Figure 2.5.These are the controls on the Standard tool-bar within the ABAP/4 editor.

The Standard toolbar controls (refer to Figure 2.3), in order, are:

DO
DON'T
DO save your program before entering a /n command in the command field. Otherwise, you will lose your changes. DON'T close the editor window by pressing the X button in the top right-hand corner of the window. You will lose unsaved changes.

Using Find and Replace

Start the ScreenCam "Using Find and Replace" now.

Press the Find button on the Standard toolbar and the system will display the Search/Replace screen (see Figure 2.6). Enter the string you want to find into the Find field.

Figure 2.6.This is the Search/Replace screen. It enables you to find and change character strings in your program.

There are several options on this screen to control the find process:

The Find field has some unusual qualities:

Start the ScreenCam "Setting the Scope of a Search or Replace Function" now.

To set the scope for a search or replace function, follow these steps:

  1. On the Search/Replace screen, type the string you want to find in the Find field.
  2. Choose the In Current Source Code radio button.
  3. Choose the Fm Cursor radio button to begin the search at the current cursor location. Alternatively, you can choose the From Line radio button and enter starting and ending line numbers for the search.
  4. Press the Continue button. The cursor is placed to the left of the first matching string.
  5. Press the Find next button to locate the next matching string.

Start the ScreenCam "How to Find All Occurrences of a String" now.

To find all occurrences of a string:

  1. On the Search/Replace screen, type the string you want to find in the Find field.
  2. Choose the In Program radio button.
  3. Press the Continue button. A summary of the occurrences found is displayed on the Global Search In Programs screen.
  4. Double-click on a line to see it in the context of the source code.
  5. Press the Back button to return to the Global Search In Programs screen.
  6. Press the Back button once more to return to the source code editor.

Start the ScreenCam "How to Search and Replace in the Source Code" now.

To search and replace in the source code:

  1. On the Search/Replace screen, type the string you want to find in the Find field.
  2. Tickmark Replace By and enter a replacement string in the Replace by field.
  3. Press the Continue button. The ABAP/4 Editor: Edit Program screen is displayed with the cursor positioned to the left of the next matching string.
  4. To replace it, press the Replace button. The string at the cursor location is replaced and the cursor is positioned at the next matching string.
  5. To go to the next position without replacing, press the Next Hit button. The cursor is positioned at the next matching string.
  6. To replace all remaining matches from the current cursor position to the end of the source code, press the No Confirmation button.

Start the ScreenCam "How to Search and Replace Via a Summary Screen" now.

To search and replace via a summary screen:

  1. On the Search/Replace screen, type the string you want to find in the Find field.
  2. Tickmark Replace By and enter a replacement string in the Replace by field.
  3. Choose the In Program radio button.
  4. Press the Continue button. The Global Replace In Programs screen is displayed with the cursor positioned on the first matching string. All lines containing matching strings are displayed and the matching strings are highlighted.
  5. To replace the string at the cursor position, press the Replace button. The string at the cursor location is replaced and the cursor is positioned at the next matching string.
  6. To go to the next position without replacing, press the Next Hit button. The cursor is positioned at the next matching string.
  7. To replace all remaining matches from the current cursor position to the end of the source code, press the No Confirmation button.

If you replaced any strings, you must save your changes before returning to the source code editor. To do so, press the Save button in the Standard toolbar, and then press the Back button. If you want to cancel your changes, press the Cancel button instead of the Save button.

Exploring the Application Toolbar

Start the ScreenCam "Exploring the Application Toolbar of the ABAP/4 Editor" now.

Before proceeding, maximize your window (if it is not already). You may not see the buttons at the far right of the Application toolbar if your window is smaller than the maximum size.

The Application toolbar controls are shown in Figure 2.7.

Figure 2.7.The Application toolbar controls.

The Application toolbar controls, in the order they appear on the toolbar, are as follows:

Using Cut and Paste

TIP
To bring a line to the top of the editor window, double-click between words or at the end of the line. Don't double-click on a word though; that won't work. An alternative method is to place your cursor at the end of a line and press the F2 key.

In most Windows applications, there is one Clipboard for cut and paste operations. In R/3, there are five clipboards. Table 2.1 describes them all.

Table 2.1  Cut and Paste Buffers
Called
Used for
How to Copy to It
How to Paste from It
The BufferCut and paste within a program Press the Cut or Copy buttons in the editor Press the Insert From Buffer button
The X,Y,Z BuffersCut and paste between two programs Menu path: Block/ Buffer->Copy to X Buffer Menu path: Block/Buffer->Insert X Buffer
The Clip- boardCut and paste between:
- two R/3 sessions
Menu path: Block/Buffer-> Copy to Clipboard Menu path: Block/Buffer->Insert from Clipboard to insert the Clipboard contents as new lines into the
 - R/3 and other Windows apps (e.g. Notepad)  program at the cursor position. Or Ctrl+V to paste over top of the existing lines. With Ctrl+V, data will be truncated if it won't all fit on the current screen.
 - copy code from F1 help to the editor editor
- copy more than one line of code
1- Click once
2- Ctrl+Y
3- Drag and mark
4- Ctrl+C
 

In the first row of Table 2.2 is program-specific buffer, simply called the buffer. It lets you copy within a program. To use it, place your cursor on a line or mark a block and press the Cut, Copy, or Insert From Buffer buttons. The buffer's contents are lost when you leave the editor.

The X, Y, and Z buffers are used to copy code from one program to another. Use the Block/Buffer menu to access them. Although they are three separate buffers, they are all used in the same way. Their contents are retained after you leave the editor, but they are lost when you log off.

The Clipboard is the same as the Windows Clipboard. Use it to cut and paste into other Windows applications such as MSWord or Notepad. It is also used to copy text from F1 help screens (see the following section titled "Getting Help").

In addition to the buttons on the Application toolbar and the menus, you can use standard Windows functions to perform cut and paste operations. To highlight a word or line, drag your cursor across it, or hold down the Shift key and tap the left or right arrow keys (you can only highlight one line at a time using this method). To highlight multiple lines, click once on the screen with the mouse and then press Ctrl+Y. The pointer will change to crosshairs. Drag the crosshairs to highlight the section you want to copy. Press Ctrl+C to copy the highlighted text to the Clipboard, or press Ctrl+X to cut. Paste from the Clipboard using Ctrl+V.

Using Other Convenient Editor Functions

Table 2.2 contains a summary of commonly used editor functions. All functions are accessed from the ABAP/4 Editor: Edit Program screen unless otherwise noted.

Table 2.2  Other Convenient Program and Editor Functions
To…
Do…
Get help on the editorChoose the menu path Help->Extended Help.
Get help on any ABAP/4 keywordPut your cursor on a keyword within the code and press F1.
Save your programPress F11.
Execute your programPress F8.
Display function keysRight-click anywhere in the screen area of a window.
Move the cursor to the command fieldPress Ctrl+Tab.
Bring any line toDouble-click on whitespace (at the end of the line), or place your
the top of the screencursor on whitespace and press F2.
Insert a linePress the Enter key with your cursor at the beginning or end of a line.
Delete a linePut your cursor on the line and press the Cut button.
Mark a block of codePlace your cursor on the first line of the block and press F9 (select). Then place it on the last line of the block and press F9 again.
Delete a block of linesSelect the block and press the Cut button.
Repeat a line or blockPress the Duplicate Line button.
Split a linePosition your cursor at the split point and press the Enter key.
Join two lines togetherPosition your cursor at the end of the line and press the Concatenate button.
Copy lines within the current programMark a block. Press the Copy To Buffer button. Then position your cursor at the point where the code should be inserted, and press the Insert From Buffer button.
Copy lines to another programMark a block. Choose the menu path Block/Buffer->Copy to X buffer. Open a new program and choose the menu path Block/Buffer ->Insert X buffer. (You can also use the Y and Z buffers the same way.)
Copy to or from the Windows ClipboardMark a block. Choose the menu path Block/Buffer->Copy to clip-board. Then choose the menu path Block/Buffer->Insert clipboard.
Comment out a block of linesMark a block, and choose the menu path Block/Buffer->Insert comment
Uncomment a block of linesMark a block, and choose the menu path Block/Buffer->Delete comment.
Print your programPress the Print button.
Print your program's outputWhile viewing the output, choose the menu path System->List->Print.
Find and repeat find Shift code left and right Press the Find and Find Next buttons. Put your cursor on the line to be moved, in the column you want to move it to. Then press the Move Line button (F6). To move a block, position your cursor on the first line and then press the Move Line button.
Undo the last changePress the Undo button. There is only one level of undo.
Automatically format source codeChoose the menu path Program >Pretty Printer.
Download a program to a file on your PC Choose the menu path Utilities->Download.
Upload a program from a file on your PC Choose the menu path Utilities->Upload.
Save a temporary copy of your programChoose the menu path Program->Save Temp. Version. The temporary copy is deleted when you save the program.
Retrieve the temporary copy of your program Choose the menu path Program->Get Temp. Version. You can retrieve the saved copy any number of times until you save the program.
Jump directly to the editorFrom any screen, type /nse38 in the command field and press Enter.
Copy a programFrom the ABAP/4 Editor: Initial Screen, choose the menu path Program->Copy.
Rename a programFrom the ABAP/4 Editor: Initial Screen, choose the menu path Program->Rename.
Delete a programFrom the ABAP/4 Editor: Initial Screen, choose the menu path Program->Delete.
Save your changes under a new program name While editing a program, choose the menu path Program->Save As.
Display two programs at the same timeStarting on any screen, open a new window using the menu path System->Create new session. In the new window, go to the ABAP/4 Editor: Initial Screen, enter the second program name, and press the Display or Change button.
Compare two programs for differencesFrom the ABAP/4 Editor: Initial Screen, choose the menu path Utilities->Splitscreen editor. Enter two program names and press the Display button. To display the first difference, press the Next Difference button. To align both programs at the next identical line, press the Align button.
Compare two programs on different systems From the ABAP/4 Editor: InitialScreen, choose the menu path Utilities->Splitscreen editor. Press the Compare Diff. Systems button. Enter two program names and a system id and press the Display button.
Save a version of a programFrom inside the editor, choose the menu path Program->Generate version. The current program is saved in the version database.
Retrieve a program versionFrom the Editor Initial screen, choose the menu path Utilities->Version management. The Versions Of Object Of Type REPS screen is displayed. Deselect the active version and tickmark the version to restore. Press the Retrieve button and then press the Back button. Press the Yes button and the current version becomes the -1 generation, and a copy of the selected version becomes the current version.
Compare program versionsFrom the Editor Initial screen, choose the menu path Utilities ->Version management. The Versions Of Object Of Type REPS screen is displayed. Tickmark the versions you want to compare and press the Compare button. The Compare Programs: All screen is displayed. Scroll down to view the differences.
Print your programChoose the menu path Program->Print. On the next screen, specify a printer and tickmark the Print Immed. check box.

Getting Help

Start the ScreenCam "Getting Help" now.

For a complete tutorial on the editor:

  1. Go to the ABAP/4 Editor: Edit Program screen.
  2. Choose the menu path Help->Extended Help. The SAP R/3 Help screen is displayed.
  3. Click on the text ABAP/4 Editor. The BC ABAP/4 Development Workbench Tools screen is displayed.
  4. Click on any underlined text for help on that topic.

There are two basic types of help in the editor, F1 help and R/3 Library help. F1 help is also known as the ABAP/4 keyword documentation.

F1 help describes the syntax of ABAP/4 keywords and gives examples of their use. It is text-based and resides in tables within the R/3 database.

R/3 Library help is far more extensive and contains overviews of and procedures for creating development objects. It is Windows-based help and resides outside of the R/3 database, usually on a CD-ROM.

Obtaining F1 Help

F1 help is useful for looking up syntax and often contains useful code samples.

To obtain F1 help:

  1. Go to the ABAP/4 Editor: Edit Program screen.
  2. Place your cursor on the ABAP/4 keyword you want help for.
  3. Press F1. The Display Hypertext screen is displayed.

Within F1 help, highlighted words are hypertext links. Clicking on them takes you to more information.

TIP
You can use the FindIt utility on the CD-ROM to find even more code samples.

Within the help there are often code samples. To cut and paste them into your program, press Ctrl+Y, and then mark a block of code by dragging your cursor across it from the top left-hand corner to the bottom right-hand corner. Let go of the mouse and press Ctrl+C. Press the Back button to return to the ABAP/4 Editor: Edit Program screen and paste into your program using Block/Buffer->Insert Clipboard. Or you can paste over top of your code using Ctrl+V. (Ctrl+V doesn't paste into the source, it pastes onto the screen, so it won't paste past the bottom of the visible page.)

Obtaining R3 Library Help

R/3 Library help is stored in Windows help files. To view it:

  1. From any screen, choose the menu path Help->R/3 Library. The R/3 System Online Help screen is displayed.
  2. For help on ABAP/4, click on the text Basis Components. The Basis screen is displayed.
  3. Click on the text ABAP/4 Development Workbench. The ABAP/4 Development Workbench screen is displayed.
  4. From here you can get detailed help on almost any aspect of programming in ABAP/4. Click on the topic that you want help for.

Additionally, help can be obtained from the ABAP/4 Editor: Initial Screen. To view it:

  1. Go to the ABAP/4 Editor: Initial Screen.
  2. Choose the menu path Utilities->ABAP/4 key word doc. The Display Structure: ABAP/4-SAP's 4GL Program Language screen is displayed.
  3. Press the Find button on the Application toolbar. The Search Titles screen is displayed.
  4. Type the text you want to find in the Find field.
  5. Press the Continue button. The first line containing that text is highlighted.
  6. Double-click on the highlighted line to display more information, or press the Continue Search button on the Standard toolbar to locate the next occurrence.

Help can also be obtained from within the editor while you are editing the source code. Choose the menu path Utilities->Help on. Here, you can get the following types of help:

Finding Your Development Objects

Start the ScreenCam "Finding Your Development Objects" now.

To display all of the development objects you have created in the R/3 system:

  1. From the ABAP/4 Development Workbench screen, press the Object Browser button. The Object Browser: Initial Screen is displayed.
  2. Choose the radio button Local Priv. Objects.
  3. Press the Display pushbutton. The Object Browser: Development Class $TMP screen is displayed. Here you will see a list of development object categories. To the left of each category is a plus sign.
  4. Click once on a plus sign to expand the node.
  5. Double-click on an object name to display it. If the object you double-clicked on was a program, a tree view of the program and its components is displayed.
  6. To display the program source code, double-click on the name of the program at the top of the tree. The ABAP/4 Editor: Display Program screen is displayed.

You are now in display mode on the selected object. You can press the Display <-> Change button to switch into edit mode.

NOTE
This method only works for objects that have been saved as local objects. They will be displayed here only if you pressed the Local Object button on the Create Object Catalog Entry screen when you created the object.

If you are looking for a program and you know the first few characters of its name, you can find it from the ABAP/4 Editor: Initial Screen. To do this:

  1. Go to the ABAP/4 Editor: Initial Screen.
  2. In the Program field, type the first few characters of your program name followed by an asterisk. For example, to display all programs beginning with ztx, type ztx*.
  3. Click on the down-arrow to the right of the Program field. The Programs screen is displayed. A list of matching program names appears on this screen. To the right of each program name is the Short description from the program attributes.
  4. Double-click on the program name you want to edit. The ABAP/4 Editor: Initial Screen is displayed, and the program name you double-clicked on appears in the Program field.

Introducing the R/3 Data Dictionary

The R/3 Data Dictionary (or DDIC for short) is a utility for defining data objects. Using the DDIC, you can create and store objects such as tables, structures, and views. To invoke the Data Dictionary, perform these steps:

  1. Go to the ABAP/4 Development Workbench screen.

  1. Press the ABAP/4 Dictionary button in the Standard toolbar. The Dictionary: Initial Screen is displayed, as shown in Figure 2.8.

Figure 2.8.The Dictionary: Initial Screen.

The DDIC is within the R/3 system. You can think of it as sitting on top of a database such as Oracle or Informix and acting like a remote control, generating and sending SQL statements to it. For example, you might create a table definition in the DDIC. When you activate the table definition, SQL statements are generated and sent to the RDBMS, causing it to create the actual table in the database. When you want to modify the table, you must modify the table definition in the DDIC. When you activate the table again, more SQL is generated causing the RDBMS to modify the table.

DO
DON'T
you must modify the table definition in the DDIC. When you activate the table again, more SQL is generated causing the RDBMS to modify the table. DO use the DDIC to create and modify all database objects.
DON'T modify a table or anything else at the RDBMS level. The Data Dictionary definition cannot update itself and it will be out of sync with the database. This can cause application errors and even lead to a loss of data integrity.

Exploring Tables and Structures

In R/3 a table is a collection of rows. Each row contains fields, also called columns. Normally, within a table, each row has the same number of columns as the other rows of the table.

A table holds persistent data. In other words, if you put data into a table, it remains there after your program ends. It will remain there until it is changed or deleted by your program or another program. The name of a table is unique within the entire R/3 system.

When you look at a table in the DDIC, you are looking at the description of a table in the underlying database. You are not looking directly at the database table itself. Figure 2.9 shows how a table definition appears in the R/3 DDIC.

Figure 2.9.The DDIC definition for table lfa1.

In R/3 a structure is a description of a group of fields. It describes the field names, their sequence, and their data types and lengths. Each structure has a name that is unique within the entire R/3 system. A structure cannot have the same name as a table.

A structure name can be used in two ways:

Strictly speaking, a structure is something that only exists within the R/3 Data Dictionary. If you see the word structure, you should immediately think "DDIC structure." However, some SAP documentation uses the word structure to also refer to a group of variables within a program. The structure emara is shown in Figure 2.10 as an example of how a structure appears in the DDIC.

Figure 2.10.The DDIC definition for structure emara.

In R/3, tables and structures are both defined in the DDIC. You will notice however, when viewing them, there is very little difference between them. That is because in R/3, a table is just a description of an actual database table. It is the structure of the table in the database. So, both tables and structures within the DDIC define a layout-a series of fields. The major difference between the two is that a table has an underlying database table associated with it. A structure does not.

Some people find the R/3 documentation confusing at times, since SAP occasionally uses these two terms interchangeably.

Displaying a Table or Structure Definition

To display a table or structure definition in the DDIC:

  1. Go to the Dictionary: Initial Screen.
  2. Enter the table or structure name in the Object Name field.
  3. Click the Tables or Structures radio button.
  4. Click the Display button. The Dictionary: Table/Structure: Display Fields screen is displayed, as shown in Figures 2.9 and 2.10.

What you see in Figure 2.9 is the structure of table lfa1 in the R/3 Data Dictionary, not the actual database table. In R/3, only a table's structure can be defined, along with some attributes such as the primary key fields.

Displaying Data in the Table

R/3 also provides a utility to enable you to display data that exists within a table. To use it:

  1. Go to the Dictionary: Table/Structure: Display Fields screen.
  2. Choose the menu path Utilities->Table contents. The Data Browser: Table: Selection Screen is displayed.
  3. To display all of the rows in the table, press the Execute button without entering any search criteria. The Data Browser: Table Select Entries screen is displayed (see Figure 2.11).

Figure 2.11.The Data Browser: Table Select Entries screen.

The number of records displayed is limited by the value in the Maximum No. Of Hits field on the Data Browser: Table: Selection Screen.

CAUTION
Don't blank out the Maximum No. Of Hits field to display all records unless you actually intend to look through the entire list. For large tables, this can consume a lot of CPU on both the database and application servers and also significantly increase network traffic, thereby slowing down the system. If such a large report is needed, it should usually be run in batch. The preferred approach is to narrow down your search by entering values into the fields on the Data Browser: Table: Selection Screen. This will be covered in detail in a later chapter.

Determining Displayed Fields

You can control which fields are displayed on the Data Browser: Table: Selection Screen. To do so:

  1. Go to the Data Browser: Table: Selection Screen.
  2. Choose the menu path Settings->Fields for selection. The Choose Fields For Selection screen is displayed.
  3. Tickmark the fields you want included on the Data Browser: Table: Selection Screen. Uncheck any that you don't want to appear.
  4. Press the Execute button. The Data Browser: Table: Selection Screen is displayed and contains the fields you selected.

Understanding ABAP/4 Syntax

Before continuing, you should now run the exercise setup utility supplied on the CD-ROM. It will create the tables and data needed for the exercises in this book. For instructions, see the readme.txt file located in the root directory of the CD-ROM.

Introducing the selectStatement

The select statement retrieves records from the database.

Syntax for the select Statement

The following code shows simplified syntax for the select statement:

select * from t1 [into wa] [where f1 op v1 and/or f2 op v2 ...]
[order by f1].
    (other abap/4 statements)
    endselect.

where:

* indicates that all fields in the table should be retrieved.
t1 is the name of a table previously defined on a tables statement.
wa is the name of work area that matches the structure of the table.
f1 is the name of a field in table t1.
op is one of the following logical operators: = <> > >= < <=.
v1 is a literal or variable.
and/or is either the word and or the word or.

Create a new program and name it 0202. Enter the code as shown in Listing 2.2.


Listing 2.2  Your Second Program

1 report ztx0202.
2 tables ztxlfa1.
3 select * from ztxlfa1 into ztxlfa1 order by lifnr.
4     write / ztxlfa1-lifnr.
5     endselect.

The code in Listing 2.2 should produce this output:

1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
2000
V1
V10
V11
V12
V2
V3
V4
V5
V6
V7
V8
V9
NOTE
Lfa1 is the vendor master table in R/3. Ztxlfa1 was created by the CD-ROM setup routine and is similar to lfa1, but is used for the exercises in this book. The field lifnr is the vendor number field.

This program reads all the records from table ztxlfa1 and writes out the contents of the field lifnr (the vendor number) in ascending order.

Notice that in your program, you have two things named ztxlfa1: a work area and a table. Both have the same name-ztxlfa1. The position of the name ztxlfa1 within a statement determines to which one you refer. On line 3, the first occurrence of ztxlfa1 refers to the database table. The second occurrence refers to the work area.

Start the "AVI Select Statement Processing" now.

Processing proceeds as follows (see Figure 2.12):

Figure 2.12.This is how the select statement works.

  1. Pressing F8 to execute the program causes a request to be sent from the SAPGUI to the dispatcher on the application server to execute program ztx0202.
  2. The request is dispatched to the first available work process.
  3. The user context is rolled into the work process.
  4. The program is retrieved from the database.
  5. The work process allocates a roll area to hold the program's variables, current program pointer, and private memory allocations.
  6. The program is interpreted by the work process on the application server and begins executing on line 3. (Lines 1 and 2 are declarations, not executable code.)
  7. Line 3 causes a row to be read from database table ztxlfa1 and placed into the work area named ztxlfa1.
  8. The first time line 4 is executed, a virtual page is allocated for the list.
  9. Line 4 causes the field lifnr to be written from the work area ztxlfa1 to the virtual page.
  10. Line 5 loops back to line 3.
  11. The next row is read from table ztxlfa1 and is placed into the work area ztxlfa1, overwriting the previous row.
  12. The field lifnr is written to the next line of the virtual page.
  13. Steps 9 through 11 are repeated for all rows in table ztxlfa1.
  14. The loop ends automatically when the last row is read from ztxlfa1.
  15. The program ends, but the roll area (containing the virtual page) remains allocated.
  16. The work process determines the number of lines that the user's screen is capable of displaying, and sends that many lines from the virtual page as the first page of the list to the presentation server.
  17. The user context and roll area are rolled out of the work process.

If the user then presses Page Down, processing proceeds as follows (see Figure 2.13):

Figure 2.13.This is the sequence of events triggered when the user presses page down.

  1. Pressing Page Down causes a request to be sent from the SAPGUI to the dispatcher on the application server to get the next page of output from ztx0202.
  2. The request is dispatched to the first available work process.
  3. The user context and roll area for ztx0202 are rolled into the work process.
  4. The work process sends the next page to the presentation server.
  5. The roll area and user context are rolled out of the work process.

If the user then presses Back, processing proceeds as follows:

  1. Pressing Back causes a request to be sent from the SAPGUI to the dispatcher on the application server to end program ztx0202.
  2. The request is dispatched to the first available work process.
  3. The user context and roll area for ztx0202 are rolled into the work process.
  4. The system frees the roll area for the program.
  5. The user context is rolled out.

TIP
To open a new R/3 window (called a session), from any screen choose the menu path System->Create New Session. You can open a maximum of six sessions. If you want to open more than six, simply log on a second time.

Understanding Table Work Areas

On line 3 of Listing 2.2, the words into ztxlfa1 are optional. If you leave them off, the work area of the same name as the table is used. In other words, the current row automatically goes into the ztxlfa1 work area. For this reason, it is known as the default table work area. In this case, the work area ztxlfa1 is the default work area for table ztxlfa1. Hence, the code in Listing 2.2 can be simplified to that shown in Listing 2.3.


Listing 2.3  Your Second Program Simplified

1 report ztx0203.
2 tables ztxlfa1.
3 select * from ztxlfa1 order by lifnr.
4     write / ztxlfa1-lifnr.
5     endselect.

TIP
To copy a program, choose the menu path Program->Copy from the editor initial screen. Alternatively, while editing a program, you can choose the menu path Program->Save As.

Compared to Listing 2.2, only line 3 has changed. This program works in exactly the same way and produces the same output as the previous one. Since no into clause is specified on the select statement, the system uses the default table work area ztxlfa1.

Using An Explicit Work Area

The tables statement always creates a default table work area, so you usually do not have to define your own. In some cases however, you might want to define an additional table work area. For example, if you wanted to keep the original version of a row and have a modified version too, you would need two work areas.

You can define additional table work areas using the data statement.

Simplified Syntax for the data Statement

Below is the simplified syntax for the data statement.

data wa like t1.

where:

Listing 2.4 shows how to use the data statement to create a new work area.


Listing 2.4  Using Your Own Table Work Area

1 report ztx0204.
2 tables ztxlfa1.
3 data wa like ztxlfa1.
4 select * from ztxlfa1 into wa order by lifnr.
5     write / wa-lifnr.
6     endselect.

The code in Listing 2.4 should produce the same output as the previous two listings (see the output from Listing 2.3). Please note that in this example it is not necessary to define a new work areaÑone is used only to illustrate the concept.

When you explicitly name a work area on the select statement (such as into wa on line 5), that work area is called an explicit work area.

Introducing the where Clause

To restrict the number of rows returned from the database, a where clause can be added to the select statement. For example, to retrieve vendors that have vendor numbers less than 2000, use the code in Listing 2.5.


Listing 2.5  Restricting the Vendors Selected

1 report ztx0205.
2 tables ztxlfa1.
3 select * from ztxlfa1 where lifnr < '0000002000' order by lifnr.
4     write / ztxlfa1-lifnr.
5     endselect.

The code in Listing 2.5 should produce this output:

1000
1010
1020
1030
1040
1050
1060
1070
1080
1090

This program reads all of the records from table ztxlfa1 where the vendor number is less than 2000, and writes out the contents of the field lifnr (the vendor number). In the database, lifnr is a character field containing numeric values that are right justified and padded on the left with zeros. Therefore, the value to be compared with lifnr should have the same format and data type to avoid data conversion and ensure the intended results are obtained.

Note that to code a character literal in ABAP/4 (such as '0000002000' on line 3), you must enclose the character string within single quotes.

Working with System Variables

The R/3 system makes system variables available within your program. You don't have to define anything to access them, they are always available, and are automatically updated by the system as your program's environment changes. All system variables begin with the prefix sy-. For example, the current system date is available in the system field sy-datum, and the current time in sy-uzeit. They are usually called sy fields (pronounced sigh fields) for short.

All system variables are defined in the DDIC structure syst. Do not define syst in your program though; its fields are automatically available within every program.

NOTE
System variable names can be coded using either the prefix sy- or syst-. For example, sy-datum can also be coded as syst-datum; they are functionally equivalent. Sy- is the preferred form, although you will occasionally see syst- in older programs. Syst is the only structure that has a duality of prefixes. For all others, you must use the full structure name as the prefix.

Two system variables are very useful to know when coding the select statement:

Discovering sy-subrc

To determine if the select statement returned any rows, test the value of the system variable sy-subrc (pronounced sigh-sub-are-see) after the endselect statement. If rows were found, the value of sy-subrc will be 0. If no rows were found, the value will be 4. (See Listing 2.6.)


Listing 2.6  Using SY-SUBRC to Determine if Any Rows Were Selected

1 report ztx0206.
2 tables ztxlfa1.
3 select * from ztxlfa1 where lifnr > 'Z'.
4     write / ztxlfa1-lifnr.
5     endselect.
6 if sy-subrc <> 0.
7     write / 'No records found'.
8     endif.

The code in Listing 2.6 produces this output:

No records found

The select statement on line 3 is restricted by a where clause. In this case, no rows match the criteria in the where clause, so after the endselect, the value of sy-subrc is set to 4.

NOTE
Many keywords assign values to sy-subrc. Some keywords do not set it at all. If a keyword sets sy-subrc, the values that it can have are documented in the F1 help for that keyword.

If you have coded a select and want to test the value of sy-subrc, your test must come after the endselect. Why? The answer lies in the fact that the code between the select and endselect is executed once for each row returned from the database.

If zero rows are returned from the database, the code between select and endselect is never executed. Therefore, you must code the test for sy-subrc after the endselect.

NOTE
Sy-subrc is not automatically initialized to zero when your program starts. However, this should never be a concern, because you should not check it until after a statement that sets it has executed.

Discovering sy-dbcnt

To determine the number of rows returned by a select statement, test the value of sy-dbcnt after the endselect. You can also use it as a loop counter; between the select and endselect it contains a count of the current iteration. For the first row, sy-dbcnt will be 1, for the second it will be 2, and so on. After the endselect, it will retain its value and so it will contain the number of rows selected. For example, to print sequential numbers beside each vendor and to print a total of the number of rows selected at the bottom of the report, use the code in Listing 2.7.


Listing 2.7  Using SY-DBCNT to Count Rows Returned from the SELECT Statement

1 report ztx0207.
2 tables ztxlfa1.
3 select * from ztxlfa1 order by lifnr.
4     write / sy-dbcnt.
5     write ztxlfa1-lifnr.
6     endselect.
7 write / sy-dbcnt.
8 write 'records found'.

The code in Listing 2.7 produces this output:

        1  1000           
        2  1010           
        3  1020           
        4  1030           
        5  1040           
        6  1050           
        7  1060           
        8  1070           
        9  1080           
       10  1090           
       11  2000           
       12  V1             
       13  V10            
       14  V11            
       15  V12            
       16  V2             
       17  V3             
       18  V4             
       19  V5             
       20  V6             
       21  V7             
       22  V8             
       23  V9             
       23  records found

Displaying the syst Structure

There are two ways to display the components of structure syst:

To find fields within the structure, press the Find button on the Standard toolbar, and enter the field name you want to find.

If you don't know the name of the field you want to find, but rather wish to find one by description, you can search the descriptions also.

Start the ScreenCam "How to Search Through Field Descriptions" now.

To search through field descriptions, use this procedure:

  1. Begin at the Dictionary: Table/Structure: Display fields screen.
  2. Choose the menu path Table->Print. A dialog box will appear asking you which components you wish to print.
  3. Tickmark all check boxes and press the Continue button. A dialog box will appear asking you for print parameters.
  4. Press the Print Preview button. The dialog box will close and a list having a small font will appear.
  5. Choose the menu path Goto->List Display. The list will change into a larger, fixed-space font.
  6. In the command field, type %sc and press the Enter key. The Find dialog box will appear.
  7. Type the text you want to find and press the Enter key. A list of matches will appear, and each match will be highlighted.
  8. Click once on a highlighted word to jump immediately to that line in the list. If you wish to find another occurrence of the text, go to step 6 of this procedure.

Using the Chain Operator

The colon (:) is called the chain operator. Use it to combine lines of code that begin with the same word or sequence of words. Place the common part at the beginning of the statement followed by a colon. Then place the ending parts of the statements after it, each separated by a comma.

For example, to define two tables, you could code this:

tables ztxlfa1.
tables ztxlfb1.

Or, you could use the chain operator, like this:

tables: ztxlfa1, ztxlfb1.

Functionally, the two preceding code segments are identical, and at runtime there is no difference in performance. During code generation, the second code segment is expanded into two statements, so from a functional standpoint the two programs are identical.

Chain operators should be used to improve the readability of your program. Use of the chain operator is illustrated in Listing 2.8.


Listing 2.8  Using the Chain Operator to Reduce Redundancy in the WRITE Statement

1 report ztx0208.
2 tables ztxlfa1.
3 select * from ztxlfa1 order by lifnr.
4     write: / sy-dbcnt, ztxlfa1-lifnr.
5     endselect.
6 write: / sy-dbcnt, 'records found'.

The code in Listing 2.8 produces the same output as the previous program (see the output from Listing 2.7).

Using the select single Statement

The select single statement retrieves one record from the database. If you know the entire primary key of the record you wish to retrieve, select single is much faster and more efficient than select/endselect.

Syntax for the select single Statement

Below is the simplified syntax for the select single statement.

select * from t1 [into wa] [where f1 op v1 and/or f2 op v2 ...].

where:

* indicates that all fields in the table should be retrieved.
t1 is the name of a table previously defined on a tables statement.
wa is the name of the work area that matches the structure of the table.
f1 is the name of a field in table t1.
op is one of the following logical operators: = <> > >= < <=.
v1 is a literal or variable.
and/or is either the word and or the word or.

The following points apply:

Listing 2.9 illustrates the select single statement. For practice, create a new program and enter the code as shown in Listing 2.9.


Listing 2.9  The SELECT SINGLE Statement

1 report ztx0209.
2 tables ztxlfa1.
3 select single * from ztxlfa1 where lifnr = 'V1'.
4 if sy-subrc = 0.
5     write: / ztxlfa1-lifnr, ztxlfa1-name1.
6 else.
7     write 'record not found'.
8     endif.

The code in Listing 2.9 produces this output:

V1      Quantity First Ltd.

Line 3 finds vendor V1 in table ztxlfa1. Because only one row is returned, order by is not needed, and neither is endselect.

Commenting Code and Formal Documentation

There are two ways to put comments into your code:

For example:

* This is a comment
tables ztxlfa1.     " This is also a comment

There is no end-comment character. In other words, when you begin a comment, the remainder of the line will be a comment only. No more code can follow on that line. The comment ends at the end of the current line.

Aside from comments, you can formally document your code in the documentation component of the program. To do so, go to the Editor Initial screen, click on the Documentation radio button, and press Change. Type in your documentation and press Save.

Summary

Q&A

Q
If I'm editing a program, what happens to my changes if I get disconnected from R/3; for example, if my PC freezes and I have to reboot? Will I lose my changes?
A
If you log on again within 5 or 10 minutes, you can reconnect to your session. When you log on again, enter your user ID and password but don't press the Enter key on the logon screen; instead, choose the menu path User->Copy Session. You will be reconnected to your previous session and everything will be restored to the way it was when you lost your connection.
Q
Is there an AutoSave feature in the editor like the one in MSWord?
A
No. But when you want to save you can just press the F11 key.
Q
Sometimes when I edit a program, I get a dialog box with the message "Program was temporarily buffered (possibly due to a system error/failure) Choose one of the following options." Why do I see it, and what should I do?
A
It means that there are two copies of your program in the system: the "real" one and a temporary one. The temporary one is created when you execute your program without first saving it. It is normally deleted when you return to the editor and save your changes, but if you abnormally exit from the editor, it will remain. For example, you could modify your program, and then execute it right away without saving it. If your program produced a short dump, you would then need to press the Exit button and you would be returned to the Workbench. Your editor session would be gone. There are now two versions of your program in the database: the changed version (the one you just executed) and the original. (The changed version is stored under a temporary name consisting of ! and the last seven characters of your program name.) If you now return to the editor and attempt to edit your program again, the system will detect the presence of both versions in the database and display a dialog box asking you which version you want to edit, the saved one or the temporary one. To display the temporary copy (the one you executed) simply press the Continue button.
Q
When I copy an entire program, the report statement still has the old report name on it. But if I run it, it runs without error. Shouldn't it give me an error?
A
The program name on the report statement is required, but it doesn't have to match the actual program name. It's for documentation only.
Q
Every time I log off R/3 I get a message box that says "Unsaved data will be lost"-even after I've saved everything. Why?
A
Despite its wording, this message doesn't mean that there is unsaved data that will be lost. It means "If you haven't saved your data, it will be lost." The best thing to do is just ignore it. Everyone else does.
Q
Can I nest select statements inside each other?
A
Yes, you can, but most of the time you shouldn't because it can be extremely inefficient. A better alternative is to use a database view.

Workshop

The following exercises will give you practice using the editor and writing simple programs using the tables, select, and write statements. The answers are in Appendix B, "Answers to Quiz Questions and Exercises."

Quiz

  1. What two things does the tables statement do?
  2. To what does the term default table work area refer?
  3. If the select statement is missing an into clause, where do the rows go?
  4. If a write statement does not contain a slash, is the output written to the same line as the output for the previous write statement or is it written to a new line?
  5. What is the name of the system variable that indicates whether any rows were found by the select statement?
  6. What is the name of the system variable that indicates how many rows were found by the select statement?
  7. Refer to Listing 2.6. If there are 30 rows in table ztxlfa1, how many times is line 4 executed?
  8. Refer to Listing 2.6. If table ztxlfa1 is empty (if it contains 0 rows), how many times is line 4 executed?

Editor Exercises

In the following exercise, you will copy a program, correct errors in the code, merge new code into it, execute it, print it, download it, upload it, create a version, and compare versions. Refer to Table 2.3 when necessary.

  1. Copy program zty0200a to program ¥¥¥0200a. To perform the copy, use the Copy button found on the Application toolbar of the ABAP/4 Editor: Initial Screen.
report zty0200a.
select * from ztxlfa1.
    write / ztxlfa1-lifnr.
    endselect.
    endselect.    " intentional duplicate line

report zty0200b.
    if sy-subrc <> 0.
        write: / 'no records found'.
        endif.
  1. Edit program 0200a. Correct the report name on the report statement.
  2. Insert a new line after line 1. Type the appropriate tables statement on that line.
  3. Get help for the tables statement. (Put your cursor on the tables statement and press F1.)
  4. Return from help back to your program. (Press the Back button in the Application toolbar.)
  5. Display the function key assignments. (Right-click anywhere in the screen area of the window.)
  6. Bring line 3 to the top of the screen. (Double-click on whitespace on line 3, not on a word.)
  7. Delete line 6. (Place your cursor on line 6 and press the Cut button on the Application toolbar.)
  8. Open a new session and display program zty0200b. (Choose the menu path System->Create session. A new SAP R/3 window is displayed. Choose the menu path Tools->ABAP/4 Workbench. The ABAP/4 Development Workbench screen is displayed. Press the ABAP/4 Editor button on the Application toolbar. In the Program Name field, type zty0200b. Press the Display button.)
  9. Mark lines 2 through 4 using the Select button. (Place your cursor on line 2. Press the Select button in the Application toolbar. Line 2 turns blue. Place your cursor on line 4. Press the Select button again. Lines 2 through 4 are marked in blue.)
  10. Copy the marked block to the X buffer. (Choose the menu path Block/Buffer ->Copy to X Buffer.)
  11. Switch back to the window in which program 0200a is displayed.
  12. Paste the lines from the X buffer after line 5. (Place your cursor on line 6. Choose the menu path Block/Buffer->Insert X Buffer.)
  13. Mark the block of lines 6 through 8. (Place your cursor on line 6. Press the Select button on the Application toolbar. Place your cursor on line 8. Press the Select button again.)
  14. Move the block four spaces to the left, aligning the if statement with the select statement. (Place your cursor on line 6 in column 1. Press the Move Line button on the Application toolbar.)
  15. Choose the menu path Program->Save As and save your modified program as 0200b.
  16. Display program 0200b using the menu path Program->Other Program.
  17. Correct the program name in the report statement. (Change 0200a to 0200b on line 1.)
  18. Select lines 3 through 5. (Place your cursor on line 3. Press the Select button on the Application toolbar. Place your cursor on line 5. Press the Select button again.)
  19. Copy the selected block to the program-internal buffer. (Press the Copy To Buffer button on the Application toolbar.)
  20. Paste the block you just copied at the end of your program. (Place your cursor on line 9 and press the Insert From Buffer button on the Application toolbar.)
  21. Paste the same block again at the end of your program. (Place your cursor on line 12 and press the Insert From Buffer button on the Application toolbar.)
  22. Undo the last paste. (Press the Undo button on the Application toolbar.)
  23. Type the following code on lines 12 through 14. Begin each line in column 1.
if sy-dbcnt > 0.
write: / sy-dbcnt, 'records found'.
endif.
  1. Automatically fix the indentation by choosing the menu path Program->Pretty Printer.
  2. Save your program. (Press the Save button.)
  3. Return to the ABAP/4 Editor: Initial Screen and document your program. (Press the Back button on the Standard toolbar. Choose the Documentation radio button. Press the Change button.)
  4. Save your documentation and return to editing the source code. (Press the Save button. Press the Back button. Choose the Source Code radio button. Type 0200b in the Program field. Press the Change pushbutton.)
  5. Display the components of the SYST structure by double-clicking on a sy field. (Double-click on the word sy-subrc on line 6. The SYST structure is displayed.)
  6. Return to editing your program. (Press the Back button.)
  7. Find the first occurrence of the string ztxlfa1 in your program. (Press the Find button. The Search/Replace screen is displayed. Type ztxlfa1 in the Find field. Press the From Line radio button. Press the Continue button.)
  8. Repeat the find, finding each successive occurrence until you find them all. (Press the Find Next button repeatedly until the message "String write not found" displays in the status bar at the bottom of the window.)
  9. Add the table name ztxlfa1 to the tables statement. (Change the tables statement so that it reads tables: ztxlfa1, ztxlfb1.)
  10. Replace all occurrences of the string ztxlfa1 on lines 10 and 11 with the string ztxlfb1 using the Search/Replace function. (Put your cursor on line 10. Press the Select button. Put your cursor on line 11. Press the Select button. Lines 10 and 11 are now marked as a block in red. Press the Find button. The Search/Replace screen is displayed. Type ztxlfa1 in the Find field. Tickmark the Replace By check box. Type ztxlfb1 in the Replace By field. Press the Continue button. The ABAP/4 Editor: Edit Program screen is displayed. Press the No Confirmation button. All occurrences of the string within the marked block are replaced.)
  11. Use a function key to save your program, and another function key to execute it. (Press F11. Press F8.)
  12. Save the list output to a file on your hard drive by choosing the menu path List
    ->Save->File. Use unconverted format.
  13. Display your downloaded output using Notepad.
  14. Return to editing your code. (Press the Back button.)
  15. Download your program to the file c:\temp\ 0200b.txt. (Choose the menu path Utilities->Download. The Transfer to a Local File screen is displayed. Type c:\temp\ 0200b.txt in the File Name field. Press the OK button. The message "277 bytes transferred" appears in the status bar at the bottom of the window.)
  16. Edit your downloaded program using Notepad. Add a full-line comment after the report statement with your name on it. (Insert a new line after the report statement: *Created by Your Name.)
  17. Type the following comment at the end of the first select statement: " read all records from table ztxlfa1.
  18. In Notepad, save your modified program to disk.
  19. Upload it, replacing program 0200b. (Switch to your ABAP/4 Editor: Edit Program 0200b screen. Choose the menu path Utilities->Upload. The Import from a Local File screen is displayed. In the File Name field, type c:\temp\ 0200b.txt. Press the OK button.)
  20. Save the uploaded program. (Press the Save button.)
  21. Print your program. (Press the Print button on the Standard toolbar. The Print Parameters screen is displayed. Ensure the Print Immed. check box is checked. Press the Print button. Your output is sent to the printer, and you are returned to the ABAP/4 Editor: Edit Program screen.)
  22. Run your program and print the output. (Choose the menu path Program->Execute. The list output is displayed. Press the Print button on the Standard toolbar. The Print Screen List screen is displayed. Ensure the Print Immed. check box is checked. Press the Print button. You are returned to the list output.)
  23. Return to editing program 0200b and save a version of it in the Versions database. (Press the Back button to return to the ABAP/4 Editor: Edit Program screen. Choose the menu path Program->Generate Version.)
  24. Replace all occurrences of ztxlfb1 with ztxlfc3. (Press the Find button. The Search/Replace screen is displayed. Type ztxlfb1 in the Find field. Tickmark the Replace By check box. Type ztxlfc3 in the Replace By field. Press the In Program radio button. Press the Continue button. The Global Replace In Programs screen is displayed. Press the No Confirmation button. All occurrences of the string are replaced. Press the Save button. Press the Back button. You are returned to the ABAP/4 Editor: Edit Program screen.)
  25. Save your changes. (Press the Save button.)
  26. Return to the editor initial screen and compare the two versions. (Press the Back button. The ABAP/4 Editor: Initial Screen is displayed. Choose the menu path Utilities->Version management. The Versions Of Object ¥¥¥0200b Of Type REPS screen is displayed. Tickmark the Version 00001 check box. Press the Compare button in the Application toolbar. The Compare Programs: All screen is displayed. Scroll down to view the differences.)
  27. Retrieve the original version. (Press the Back button. The Versions Of Object 0200b Of Type REPS screen is displayed. Remove the tickmark from the Act. check box. Press the Retrieve button on the Application toolbar (the system does not provide a response). Press the Back button. The Restore Version screen is displayed. Press the Yes button. The Versions Of Object 0200b Of Type REPS screen is displayed. Press the Back button. The ABAP/4 Editor: Initial Screen is displayed.)
  28. Edit your program and mark the last three lines as a block. (Press the Change button. The ABAP/4 Editor: Edit Program screen is displayed. Place your cursor on line 13. Press the Select button. Line 13 turns red. Place your cursor on line 15. Press the Select button. The block is marked in red.)
  29. Comment out the block using the menu path Block/Buffer->Insert Comment *.
  30. Mark the last three lines as a block (see step 51).
  31. Remove the comments using the menu path Block/Buffer->Delete Comment *.

Programming Exercises

  1. Write a program that retrieves all rows from table ztxlfb1 where the company code is equal to or greater than 3000. On each line of output should be a company code and a vendor number, with the company code first. The output should be sorted first by company code in ascending order, then by vendor number in descending order. (Use F1 help to find the correct syntax.) Name your program e0201. A sample of how your output should appear follows.

1000 V9
1000 V8
1000 V6
2000 V9

  1. Copy program zty0202 to e0202 (the listing appears below). It does not produce any output. Find the bug in it and correct it. When fixed, the correct output should be no records found.
1  report zty0202.
2  tables ztxlfa1.
3  select * from ztxlfa1 where lifnr like 'W%'.
4      if sy-subrc <> 0.
5          write / 'no records found'.
6          endif.
7      write / ztxlfa1-lifnr.
8     endselect.

  1. Copy program zty0203 to e0203 (the listing appears below). It has a syntax error and a bug in it. Fix both the syntax error and the bug. When fixed, it should display vendor numbers greater than 1050 from table ztxlfa1.
1  report zty0203.
2  tables ztxlfa1
3  select * from ztxlfa1 where lifnr > Ô1050'.
4      write / ztxlfa1-lifnr.
5      endselect.

  1. Copy program zty0204 to e0204 (the listing appears below). The output is incorrect. Look at the output, discover what is wrong, and fix the program.
1  report zty0204.
2  tables ztxlfa1.
3  select * from ztxlfa1 where lifnr > '0000001050'.
4      write / ztxlfa1-lifnr.
5      endselect.
6  if sy-dbcnt <> 0.
7      write / 'no records found'.
8      endif.

  1. Copy program zty0205 to e0205 (the listing appears below). The output is incorrect. Look at the output, discover what is wrong, and fix the program.
1  report zty0205.
2  tables ztxlfa1.
3  select * from ztxlfa1 where lifnr > '0000001050'.
4  endselect.
5  write / ztxlfa1-lifnr.
6  if sy-subrc <> 0.
7      write / 'no records found'.
8      endif.

  1. Copy program zty0206 to e0206 (the listing appears below). Simplify the program by remove unnecessary code and unneeded words.
1  report zty0206.
2  tables: ztxlfa1, ztxlfb1, ztxlfc3.
3  data wa like ztxlfb1.
4  select * from ztxlfa1 into ztxlfa1.
5      write / ztxlfa1-lifnr.
6      write   ztxlfa1-name1.
7      endselect.
8  if sy-subrc <> 0 or sy-dbcnt = 0.
9      write / 'no records found in ztxlfa1'.
10     endif.
11 uline.
12 select * from ztxlfb1 into wa.
13     write / wa-lifnr.
14     write   wa-bukrs.
15     endselect.
16 if sy-subrc <> 0.
17    write / 'no records found'.
18    endif.

  1. Copy program zty0207 to e0207 (the listing appears below). It has both a syntax error and a programming error in it. Discover what is wrong and fix the program. The output should display a single record (vendor 1000) using the most efficient programming construct.
1  report zty0207.
2  tables ztxlfa1.
3  select single * from ztxlfa1 where lifnr > '0000001000'.
4      write / ztxlfa1-lifnr.
5      endselect.
6  if sy-subrc <> 0.
7      write / 'no records found'.
8      endif.