14
ABAP: Tables and Data Prepared by J Kreie New Mexico State University ABAP Tables and Data This lesson requires that you first complete the lesson “ABAP Tables, Domains, and Data Elements.” The program you create here will use the tables created in the previous lesson. Add/View Data in Tables Open the Object Navigator. Create a new program named Z_$$$$_students without the “TOP INCL.” For the program title enter: $$$$ Student Information. Assign it to your package and transport request. Below are screen shots of the code with comments between the screen shots. Comments are also embedded in the ABAP program. You don’t have to type them but it is recommended. It will help you read and understand the program if you refer to it later on. Selection-screen You’ll create three selection screens in this program: 1 st : Get the user input for what they want to do—add data to a table or view data in a table. 2 nd : Get user input for data to add to the EDULEVEL table. 3 rd : Get user input for data to add to the STUDENT table. You can create input screens in ABAP using the selection-screen statement. A selection-screen is given a number such as the ‘110’ shown below. As you can see this is a block of code that starts with “begin” and ends with the “end” statement. Within a selection-screen there can be one or more blocks (different sections on the screen). All the examples here have one block. The title of the screen frame is given a text symbol (text-001, in this case). You fill in the actual text for that later on. The parameters here use radio buttons in a group. Assigning them all to a group Page 1 of 14

Abap 020 Tables and Data

  • Upload
    ly-bang

  • View
    14

  • Download
    5

Embed Size (px)

DESCRIPTION

Abap 020

Citation preview

Page 1: Abap 020 Tables and Data

ABAP: Tables and DataPrepared by J Kreie

New Mexico State University

ABAP Tables and Data

This lesson requires that you first complete the lesson “ABAP Tables, Domains, and Data Elements.” The program you create here will use the tables created in the previous lesson.

Add/View Data in Tables

Open the Object Navigator. Create a new program named Z_$$$$_students without the “TOP INCL.” For the program title enter: $$$$ Student Information. Assign it to your package and transport request.

Below are screen shots of the code with comments between the screen shots. Comments are also embedded in the ABAP program. You don’t have to type them but it is recommended. It will help you read and understand the program if you refer to it later on.

Selection-screen

You’ll create three selection screens in this program:

1st: Get the user input for what they want to do—add data to a table or view data in a table. 2nd: Get user input for data to add to the EDULEVEL table. 3rd: Get user input for data to add to the STUDENT table.

You can create input screens in ABAP using the selection-screen statement. A selection-screen is given a number such as the ‘110’ shown below. As you can see this is a block of code that starts with “begin” and ends with the “end” statement. Within a selection-screen there can be one or more blocks (different sections on the screen). All the examples here have one block.

The title of the screen frame is given a text symbol (text-001, in this case). You fill in the actual text for that later on.

The parameters here use radio buttons in a group.

Assigning them all to a group ensures that only one choice can be made. Also, note that a default choice is set.

Page 1 of 9

Page 2: Abap 020 Tables and Data

ABAP: Tables and DataPrepared by J Kreie

New Mexico State University

The second selection-screen gets user input if they want to add a record to the EDULEVEL table. The use of the OBLIGATORY setting makes sure the user enters data for that parameter. The program will not proceed until the required data is entered. This is a very handy feature for the developer. It reduces the amount of error-checking you have to do. Note that the parameters are defined by referencing fields in the EDULEVEL table. This ensures data type capability.

This selection-screen sets up parameters for student data. The points made about the previous selection-screen also apply here. Some parameters are required (obligatory) and each parameter takes on the definition of the field it corresponds to in the table.

The main program calls the first selection-screen then it evaluates which choice the user made by using an IF statement. The appropriate subroutine (procedure or FORM) is called based on the user’s selection. PERFORM is the ABAP statement to call subroutine.

Page 2 of 9

Page 3: Abap 020 Tables and Data

ABAP: Tables and DataPrepared by J Kreie

New Mexico State University

The remainder of the code is comprised of subroutines. Each subroutine has a comment header to identify its beginning. The subroutine code starts with FORM and terminates with ENDFORM. Don’t type the comment header because it is created automatically when you use Pretty Printer to format your code.

The first subroutine sets up a structure and internal table for processing data in the EDULEVEL transparent table. The data is read from the transparent table into the internal table (a table in RAM that exists only during program execution). Data in the internal table is moved row by row into a structure from which the WRITE statement can display each field in the row.

Data is read from the transparent table using embedded SQL SQL embedded in any programming language is referred to as “embedded SQL.”

The SELECT statement in this subroutine retrieves all records from the external table and puts the data in the internal table. Later on you’ll use select statements that set criteria and limit the data retrieved from the transparent table.

Page 3 of 9

Page 4: Abap 020 Tables and Data

ABAP: Tables and DataPrepared by J Kreie

New Mexico State University

This subroutine retrieves and displays records from the STUDENT table.

Page 4 of 9

Page 5: Abap 020 Tables and Data

ABAP: Tables and DataPrepared by J Kreie

New Mexico State University

The following subroutine calls the second selection-screen to get user input for adding a record to the EDULEVEL table. Data for both parameters are required (obligatory).

Notice the INSERT statement that moves data from the program’s structure into the transparent table.

Page 5 of 9

Page 6: Abap 020 Tables and Data

ABAP: Tables and DataPrepared by J Kreie

New Mexico State University

This subroutine calls the third selection-screen to get data for a new student record.

This subroutine displays a message if the education level or student tables have no data.

Save and check your program. You’ll probably need to do some debugging.

Save your changes, check again, and activate your program.

Page 6 of 9

Page 7: Abap 020 Tables and Data

ABAP: Tables and DataPrepared by J Kreie

New Mexico State University

Test your program. The initial screen is a little hard to decipher but you’ll clear that up next.

Initial selection screen:

P_A_LVL option (Add row to education level):

Add some data:FR Freshman

P_A_STDT (Add a student):

Add a student.Notice that you have a list to choose from for the major and the education level.

So, you can see that the text on the screens is based on parameters names. Use the text elements/selection texts to clean up the initial input screen so regular English is shown instead of cryptic parameter names. Below is example text for the screen text symbols and selection text. Remember to activate the symbols and text. Try running your program again. It’s easier to understand!

Page 7 of 9

Page 8: Abap 020 Tables and Data

ABAP: Tables and DataPrepared by J Kreie

New Mexico State University

Of course, you can make this program multilingual by added translations for the selection texts and text symbols. You can do this on your own if you want to practice. Translations are not provided here.

Database concepts: Domain value range versus reference table

When you run this program, notice that the p_major parameter has a value search with a list of options. The list of options was created when a range of values was entered in the domain for the data element that the MAJOR field is based on. This example shows another powerful feature of ABAP. A value search is generated automatically if the metadata supports it. However, from a general database perspective, the value range defined for the domain in this lesson would not be practical in the real world. Why? It is very likely that majors will change over time; certainly some would be added. Defining the values in the domain makes it difficult to maintain this data in a real-time, dynamic way. It would be better to have a reference table (check table in SAP parlance) that can be maintained by certain users. To change the value list in the domain would require intervention by a developer. To change the list in a reference table—add a new major, for example—would only require data entry from an authorized end-user. A value range in a domain makes sense if the list is extremely stable, i.e. it is not expected to change.

A few comments about creating tables in SAP

It is unlikely that you’ll create many, if any, tables in SAP using ABAP Workbench. In future lessons you’ll work with SAP’s tables that support the many business modules. The point of creating the tables in the previous lesson and working with them here is to show how tables are constructed in SAP and how database concepts such as domains can be applied in a real relational database environment.

It is also important to understand the distinction between SAP’s transparent tables and the internal tables you will often use in your programs. Internal tables exist in RAM only during program execution. Once the data from a transparent table(s) has been loaded into an internal table, the access and processing of the data is much, much faster than working directly with transparent tables. As a programmer and power SQL user, you should use internal tables but you should think about how to populate them efficiently. In other words, don’t pull every record from a transparent table into an internal table (select * from mara, for example) if it makes sense to limit the records through certain criteria, such as country code, customer ID, date, etc.

Practice

Create a program that allows you to add data to or list data from the tables you created in the “practice” section of the previous lesson.

Page 8 of 9

Page 9: Abap 020 Tables and Data

ABAP: Tables and DataPrepared by J Kreie

New Mexico State University

Terms to Know

check table............................................................................................................................................................. 4embedded SQL...................................................................................................................................................... 3external table.......................................................................................................................................................... 3FORM..................................................................................................................................................................... 2insert statement...................................................................................................................................................... 4internal table........................................................................................................................................................... 3PERFORM.............................................................................................................................................................. 2reference table........................................................................................................................................................ 4select statement..................................................................................................................................................... 3selection-screen..................................................................................................................................................... 1value search........................................................................................................................................................... 4

References

ABAP Basics. Günther Färber and Julia Kirchner, SAP Press - Galileo Press, 2007.

Discover ABAP. Karl-Heins Kühnhauser, SAP Press, 2007.

ABAP Objects: An Introduction to Programming SAP Applications. Horst Keller and Sascha Kruger, SAP Press, 2002.

Page 9 of 9