Sams Teach Yourself ABAP/4® in 21 Days


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.