After you complete this chapter, you should be able to:
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.
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.)
There are two main types of ABAP/4 programs:
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.
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.
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.
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.
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.
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.
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).
|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.|
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:
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.
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.
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.
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.
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 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.|
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:
Start the ScreenCam "How to Find All Occurrences of a String" now.
To find all occurrences of a string:
Start the ScreenCam "How to Search and Replace in the Source Code" now.
To search and replace in the source code:
Start the ScreenCam "How to Search and Replace Via a Summary Screen" now.
To search and replace via a summary screen:
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.
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:
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.
|The Buffer||Cut and paste within a program||Press the Cut or Copy buttons in the editor||Press the Insert From Buffer button|
|The X,Y,Z Buffers||Cut and paste between two programs||Menu path: Block/ Buffer->Copy to X Buffer||Menu path: Block/Buffer->Insert X Buffer|
|The Clip- board||Cut 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|
3- Drag and mark
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.
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.
|Get help on the editor||Choose the menu path Help->Extended Help.|
|Get help on any ABAP/4 keyword||Put your cursor on a keyword within the code and press F1.|
|Save your program||Press F11.|
|Execute your program||Press F8.|
|Display function keys||Right-click anywhere in the screen area of a window.|
|Move the cursor to the command field||Press Ctrl+Tab.|
|Bring any line to||Double-click on whitespace (at the end of the line), or place your|
|the top of the screen||cursor on whitespace and press F2.|
|Insert a line||Press the Enter key with your cursor at the beginning or end of a line.|
|Delete a line||Put your cursor on the line and press the Cut button.|
|Mark a block of code||Place 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 lines||Select the block and press the Cut button.|
|Repeat a line or block||Press the Duplicate Line button.|
|Split a line||Position your cursor at the split point and press the Enter key.|
|Join two lines together||Position your cursor at the end of the line and press the Concatenate button.|
|Copy lines within the current program||Mark 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 program||Mark 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 Clipboard||Mark 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 lines||Mark a block, and choose the menu path Block/Buffer->Insert comment|
|Uncomment a block of lines||Mark a block, and choose the menu path Block/Buffer->Delete comment.|
|Print your program||Press the Print button.|
|Print your program's output||While 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 change||Press the Undo button. There is only one level of undo.|
|Automatically format source code||Choose 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 program||Choose 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 editor||From any screen, type /nse38 in the command field and press Enter.|
|Copy a program||From the ABAP/4 Editor: Initial Screen, choose the menu path Program->Copy.|
|Rename a program||From the ABAP/4 Editor: Initial Screen, choose the menu path Program->Rename.|
|Delete a program||From 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 time||Starting 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 differences||From 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 program||From inside the editor, choose the menu path Program->Generate version. The current program is saved in the version database.|
|Retrieve a program version||From 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 versions||From 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 program||Choose the menu path Program->Print. On the next screen, specify a printer and tickmark the Print Immed. check box.|
Start the ScreenCam "Getting Help" now.
For a complete tutorial on the editor:
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.
F1 help is useful for looking up syntax and often contains useful code samples.
To obtain F1 help:
Within F1 help, highlighted words are hypertext links. Clicking on them takes you to more information.
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.)
R/3 Library help is stored in Windows help files. To view it:
Additionally, help can be obtained from the ABAP/4 Editor: Initial Screen. To view it:
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:
Start the ScreenCam "Finding Your Development Objects" now.
To display all of the development objects you have created in the R/3 system:
You are now in display mode on the selected object. You can press the Display <-> Change button to switch into edit mode.
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:
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:
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.
|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.
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.
To display a table or structure definition in the DDIC:
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.
R/3 also provides a utility to enable you to display data that exists within a table. To use it:
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.
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.
You can control which fields are displayed on the Data Browser: Table: Selection Screen. To do so:
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.
The select statement retrieves records from the database.
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.
* 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
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.
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.
If the user then presses Back, processing proceeds as follows:
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.
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.
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.
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.
Below is the simplified syntax for the data statement.
data wa like t1.
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.
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.
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.
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:
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.
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.
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.
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
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
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:
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
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).
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.
Below is the simplified syntax for the select single statement.
select * from t1 [into wa] [where f1 op v1 and/or f2 op v2 ...].
* 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
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.
There are two ways to put comments into your code:
* 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.
|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?|
|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.|
|Is there an AutoSave feature in the editor like the one in MSWord?|
|No. But when you want to save you can just press the F11 key.|
|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?|
|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.|
|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?|
|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.|
|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?|
|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.|
|Can I nest select statements inside each other?|
|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.|
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."
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.
report zty0200a. select * from ztxlfa1. write / ztxlfa1-lifnr. endselect. endselect. " intentional duplicate line report zty0200b. if sy-subrc <> 0. write: / 'no records found'. endif.
if sy-dbcnt > 0. write: / sy-dbcnt, 'records found'. endif.
1000 V9 1000 V8 1000 V6 2000 V9
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 report zty0203. 2 tables ztxlfa1 3 select * from ztxlfa1 where lifnr > Ô1050'. 4 write / ztxlfa1-lifnr. 5 endselect.
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 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 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 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.