Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
thomas_weiss
Active Participant
Now it is time to write a program that does something more than just output "Hello World". In weblog 7 of our 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 weblog 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 weblog, if you have the results of this weblog or are able to create an empty report, that is a program of the type report.

The Aim: Our first Little Business Program


 

So much for the preliminaries. So let us resume the work we have begun in the last weblog. Our aim is to write a typical business program: It selects some lines from a database table that fulfill a given condition, stores them in a container in 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 a minimum of 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 result fast. 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, useful and fast-to-realize as these alternatives are, 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 weblog 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. You find more information on this model in the SAP Help Portal under this link . The SAP Help Portal contains the SAP documentation in different tree structures. The key node of the ABAP documentation in this portal is here . The topics we touch in our weblogs 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.
</p>

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 .

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 weblog 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:

 
LOOP AT itab_flight INTO wa_flight.
WRITE: wa_flight-mandt,
wa_flight-carrid,
wa_flight-connid,
wa_flight-fldate,
wa_flight-price,
wa_flight-currency,
wa_flight-planetype,
wa_flight-seatsmax,
wa_flight-seatsocc,
wa_flight-paymentsum,
wa_flight-seatsmax_b,
wa_flight-seatsocc_b,
wa_flight-seatsmax_f,
wa_flight-seatsocc_f.
SKIP.
ENDLOOP.

 

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:
LOOP AT itab_flight INTO wa_flight.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE wa_flight TO <wa_comp>.
IF sy-subrc <> 0.
SKIP.
EXIT.
ENDIF.
WRITE <wa_comp>.
ENDDO.
ENDLOOP.

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:

 


</p>

The Whole Code of the Three Alternatives – An Overview


 
REPORT  read_table_flights.
DATA: itab_flight TYPE STANDARD TABLE OF sflight,
wa_flight TYPE sflight,
alv TYPE REF TO cl_salv_table.
FIELD-SYMBOLS: <wa_comp> TYPE any.

SELECT *
FROM sflight INTO TABLE itab_flight
WHERE carrid = 'LH' AND connid = '402'.


* Alternative one: Loop at table and write the different components of work area
LOOP AT itab_flight INTO wa_flight.
WRITE: wa_flight-carrid,
wa_flight-connid,
wa_flight-fldate,
wa_flight-price,
wa_flight-currency,
wa_flight-planetype,
wa_flight-seatsmax,
wa_flight-seatsocc,
wa_flight-paymentsum.
skip.
endloop.

**Alternative two: Loop at table with dynamic assign of component
*LOOP AT itab_flight INTO wa_flight.
* DO.
* ASSIGN COMPONENT sy-index OF STRUCTURE wa_flight TO <wa_comp>.
* IF sy-subrc <> 0.
* SKIP.
* EXIT.
* ENDIF.
* WRITE <wa_comp>.
* ENDDO.
*ENDLOOP.

** object oriented way
*cl_salv_table=>factory( IMPORTING r_salv_table = alv
* CHANGING t_table = itab_flight ).
*alv->display( ).

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.


 
21 Comments