Problem solve Get help with specific problems with your technologies, process and projects.

ABAP for newbies: A first little business program

With the new build of the SAP NetWeaver 2004s ABAP trial version (SP11), you may need to update your ABAP developer skills (beginners and experienced) to understand the new version. By the end of this tip series, you should have a good understanding of what's going on in the ABAP application server. In part 10, Thomas Weiss demonstrates creating a first little business program.


Now it is time to write a program that does something more than just output "Hello World". In tip 7 of the series...

we have done the necessary preparatory work, that is, we have created a transportable package for all our programs and a program within this package. With some more experience you will handle this prep-work very fast with hardly paying any attention to it, though it might look time consuming at first sight. The other reason why I spent some more time on this is that it will pay if you know what your are doing when clicking your way through these dialog windows: The SAP CTS (Change and Transport System) does not exist for its own sake, but it has proven to be highly useful in large development projects and that is why SAP created the CTS.

In any case, the result of the last tip will be the starting point at which be begin to write our program. You will only be able to reproduce what we are doing in this tip, if you have the results of this tip or are able to create an empty report, that is a program of the type report. If you have trouble doing this, just look in tip 7.

The Aim: Our first Little Business Program

So much for the preliminaries. So let us resume the work we have begun in the last tip. Our aim is to write a typical business program: It selects some lines from a database table that fulfills a given condition, stores them in a container inside the program, and shows this data in a quick and dirty way on the screen. As to the presentation of the result in the user interface I will show three alternatives that are easy to realize, straight forward to understand and require minimal coding effort. You will probably be surprised at how simple it is to output a result table on the screen in ABAP.

All three alternatives are based on classic Dynpro technology. That means you should use them for test purposes only, if you need to output some fast results. Each alternative has an advantage of its own and contains new ABAP commands for you: The first one is straight forward in that you see all the different components that you output. You can use the second alternative to output any result table of a 'SELECT' on the screen no matter what its components are. Understanding this, you get to know two important means for dynamic programming: the field-symbol, which is a sort of a pointer and the dynamic assign. The third alternative is based on a service class that needs hardly more than the name of table you want to output. The code of this gives you an impression of how object orientation is realized in ABAP.

Nevertheless, these are useful alternatives, they are all more or less comfortable ways to output results for test purposes. In a real-world program you use Web Dynpro ABAP, encapsulate your business logic in a global class or a function module and hand over the data needed in the user interface as parameters. But I think it is perfectly legitimate to use this classic technology at this stage of our tip series. Just compare it to the output to console so widely used in Java tutorials though nobody would bother a real end user with such a user interface.

How to Use the ABAP Documentation

Our example is based on the SAP flight model: This model gives a simple description of seat bookings in passenger airplanes by flight customers and is realized in a set of database tables such as 'SCARR' for air carriers, 'SPFLI' for flight connections, 'SFLIGHT' for flights and 'SBOOK' for bookings. The topics we touch in our tips are mainly covered under these three subnodes

The documents under the node ABAP Programming and Runtime Environment describe language-related topics. Under the header ABAP Workbench you find documentation on the tools to edit objects like reports, function modules, and global classes, on the Transport Organizer, on test tools like the ABAP Debugger and the Performance Trace and all the other tools of the ABAP Workbench. The Web Dynpro ABAP documentation provides a description of the complete architecture of Web Dynpro ABAP and its main concepts. You should consult the Help Portal for ABAP topics when you have questions that are related to tools, the architecture or the relation of concepts. For information on the keywords and syntax of the language itself there is the system documentation. One way to access the system documentation is by marking the relevant command in the editor and pressing the F1 button.

How to Get an Internal Table with the Line Type of a Database Table

So much for this short digression to the ABAP documentation and how to access it. Let us go on and select some flights from the database table sflight, in particular the flights that fulfill two conditions: The airline is Alitalia and the seat capacity of the airplane is smaller than 250 seats. We want to output the result set of this select condition on the screen. We begin our program by declaring the data we need:

DATA: itab_flight TYPE STANDARD TABLE OF sflight,
      wa_flight TYPE sflight
.

In the first statement we define an internal table with the line type of the database table sflight. The way this works in general is simple:

DATA: sometable TYPE STANDARD TABLE OF [any database table ].

This declaration defines the internal table some_table with the line type of the database table given in the square brackets at the end. A internal table is the ABAP data type that is tailor made for business programming. It is a dynamic data field that can contain a practically infinite number of lines. You can use centrally defined line types or define a line type within your program.

The figure shows a linetype for contacts (1). It consists of forename, last name and city. The internal table itab_contacts (2) can contain a practically infinite number of lines, the technical size limit for an internal table is 2 GB.

Open SQL in ABAP

Next we select from the database table sflights all flights of the carrier Lufthansa with airplanes that have less than 250 seats:

SELECT * FROM sflight INTO TABLE itab_flight
         WHERE carrid = 'LH' AND seatsmax < 250
.

As you can see a subset of SQL is part of ABAP. This so-called Open SQL has a big advantage for you: It is a database independent subset an SQL, so that your SQL statements are valid for the database of every vendor that is supported by the SAP NetWeaver Application Server ABAP. As the application server always has a database connection at hand, you need not care about opening, administrating and closing such a connection. By using the keywords INTO TABLE you perform an array fetch, that is, the whole result set is transported into the internal table. The only precondition is that the database table and the internal table have the same line type.

Next we want to see the content of the internal table. One way to see the content of this internal table at runtime is to switch to the debugger and to have at a look at the content of it. We will show in a later tip how easy this is in ABAP. You can just set a breakpoint at a particular statement and the debugger opens as soon as this statement is processed.

Test Output of an Internal Table 1: A Loop and a Write-Statement

Here we will output the content of the internal table on the screen by using a classical list. ABAP has a particular control statement to loop over all lines of an internal table. Let us use it:

Every line of the internal table is put into the work area consecutively. Inside the loop the different components of the work area wa_flight are written to the list output. In ABAP the hyphen is the component selector for a structure.

How to Get to Know the Properties of a Database Table

You may ask how could I know the components of this structure. Answering this question is a good opportunity to show another useful feature of ABAP. Obviously, the work area is a structure of the line type of the database table sflight. So we double click the name of this database table in the declaration at the top of our report. We are asked to save the program, and we confirm. There we are:

The system has led us to the location where the table is defined. This is the ABAP Dictionary (Transaction SE11). By using the Back button we return to our program. So this is the way you get to know the components of the line type of a database table.

What the List Output with the Write Statement Looks like

The way the output to a list works is simple: As soon as the whole list is filled it is output to the screen. So let us save and run our program, and the upper part of the result on the screen looks like this:

Obviously this way to write the lines of the internal table while looping over it presupposes the knowledge of what the components of this table are.

Test Output of an Internal Table 2: A Dynamic Way to Output Any Internal Table

Next I will show you a way to write the lines of a table without knowing the different components of its line type. First of all you can reuse these lines of code to output any result of a SELECT that is stored in an internal table. And secondly, you encounter two important means to realize dynamic programming in ABAP: the field-symbol and the dynamic assign.

First we have to declare such a field-symbol, this is a particular field type in ABAP. Without going into the details of it, you should know that a field symbol works like a pointer without pointer arithmetics, but has a value semantics. That is you can assign it to different data fields and when accessing their content there is no need for dereferentiation.

FIELD-SYMBOLS: <wa_comp> TYPE ANY .

The typing ANY is necessary because the field symbol should be able to refer to data of any type. And this is what our loop looks like now using a dynamic assignment to the different components of the work area:

The most complex activity is contained in the third line. The rest is simple and straight forward: The system variable sy-index contains the loop counter that is counted up with every processing of the loop. The n-th component of the structure is assigned to the field symbol. If this is successful (if sy-subrc =0 ) the content of the field symbol is output to the list using the write-statement. If there is no more component to be assigned to the field symbol, the assign is not successful (sy-subrc <>0 ) and the we leave the DO-loop (the inner loop) and go on with the outer loop.

To check if our new lines yield the same result we outcomment the original loop. You mark the relevant lines and press CRT and "<" simultaneously. We run the program and the result is almost the same. There is one difference: This table has some more columns because this time we output all columns, while in the first alternative we have not shown the columns SEATSMAX_B, SEATSOCC_B, SEATSMAX_F, SEATSOCC_F. If you doubt this just double click on sflight in the program and look in the declaration of the table in the ABAP Dictionary.

Test Output of an Internal Table 3: The Object Oriented Way

A more comfortable way to output the internal table is by using a service class that outputs the whole table plus the header information provided in the ABAP Dictionary. First, we need to declare a field that can keep a reference to the service class.

DATA alv TYPE REF TO cl_salv_table .

The whole output is contained in two statements:
cl_salv_table=>factory( IMPORTING r_salv_table = alv
                        CHANGING  t_table      = itab_flight ).
alv->display(
).

The class method factory of the class cl_salv_table needs the internal table it should show, creates an instance of this very class, and exports a reference to this instance. In the second statement the method display of this instance outputs the table to the screen. Before we run the program, we should, of course, outcomment, the dynamic assign plus the loop:

The Whole Code of the Three Alternatives – An Overview

Now let us have a look at the whole code of our program in one listing. I have outcommented alternative two and three to output the internal table on the screen. So if you use this code as it is, you run alternative one. To change this just outcomment other parts of the code (press Shift + CRT + ">" to uncomment and CRT + ">" to outcomment some lines):

Summary

Now you have learned:

  • What an internal table is good for and how easily you can declare an internal table of the same line type as a given database table
  • How you write a SELECT statement in ABAP and put its result into an internal table.
  • A simple way to output an internal table on the screen by looping over it and outputting each line component-wise.
  • A dynamic way to output any internal table using a field symbol and a dynamic assign.
  • An object oriented way to output the whole table without any looping.



Thomas Weiss works in the SAP NetWeaver Product Management and has built elearning tutorials on ABAP topics.


This content is reposted from the SAP Developer Network.
Copyright 2007, SAP Developer Network


SAP Developer Network (SDN) is an active online community where ABAP, Java, .NET, and other cutting-edge technologies converge to form a resource and collaboration channel for SAP developers, consultants, integrators, and business analysts. SDN hosts a technical library, expert blogs, exclusive downloads and code samples, an extensive eLearning catalog, and active, moderated discussion forums. SDN membership is free.

Want to read more from this author? Click here to read Thomas Weiss' Weblog. Click here to read more about ABAP on the SDN.



This was last published in July 2007

Dig Deeper on SAP ABAP

PRO+

Content

Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close