Skip to Content
Technical Articles
Author's profile photo Thomas Weiss

ABAP Trial Version for Newbies Part 10: A First Little Business Program

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

 

image

 

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.

 

image

 

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:

 

image

 

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:

 

 

 

image

 

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:

 

image
</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.

 

Assigned Tags

      21 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member
      Hi Thomas Weiss

      Eventhough i am not an ABAPerr, your blogs give great clarity on the content in ABAP, but one small request that i see this blog as part 10 where can i find the links to 9 to 1 when i checked i could only find 7, 6 5, ....

      can you spare a min out of your valuable time to provide all the links on this topic till now...it will be very useful ...for people like me.

      thanks again for a wonderful blog.

      Regards
      Sreedhar

      Author's profile photo Thomas Weiss
      Thomas Weiss
      Blog Post Author
      I have now included a link into the first paragraph of this weblog. The link leads you to a list with all weblogs of the series. So this should help you.
      Author's profile photo Former Member
      Former Member
      Hi there,

      Forgive this comment, i am very much a novice and new to this fascinating language, and was working through the example in thie weblog.  When compiling the code, I recieved an error on the SELECT statement:

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

      The Error said 'unable to intepret 'FROM'. Possible causes of error, incorrect spelling or comma.

      SO i tried a few things and found that if I added the '*' before the FROM then it compiled ok.  Just wanted to make mention of it, if i am fault, please let me know.

      Thank you for this very encouraging and helpful weblog - dont stop, we want more!

      Regards
      Sulman

      Author's profile photo Former Member
      Former Member
      Hi again,

      I realised that you included the missing '*' in you ful code at the end, but you hadn't done it in the earlier broken down sample, I had not got that far when i posted my ealier query/comment.

      My bad,

      Thanks again
      Sulman

      Author's profile photo Former Member
      Former Member
      Hello,

      Regarding the dynamic way to output a table; what is the sy-subrc? The sy-index I understand is the loop counter which is also assigned to the field symbol. But you say that ‘if sy-subrc =0’ then the WRTIE statement is executed, but if ‘sy-subrc <>0’ then the IF statement is entered and the program exits.

      Thus my question is; what does the sy-subrc represent? In Java you have an equality operator (==) and an assignment one (=), what do you have in ABAP as the ‘=0’ and the ‘<>0’ do not make a great deal of sense.

      If I am getting ahead of myself, please let me know.

      Thank you for all your efforts.

      Regards
      Sulman

      Author's profile photo Thomas Weiss
      Thomas Weiss
      Blog Post Author
      Sorry, it seems some way I forgot the asterisk in that code snippet. Thank you for your comment. I have corrected it.
      Author's profile photo Thomas Weiss
      Thomas Weiss
      Blog Post Author
      After an successful dynamic ASSIGN the sy-subrc equals 0. If the dynamic ASSIGN was not successful the sy-subrc equals 4.
      Author's profile photo Former Member
      Former Member
      Hi,

      I m unable top find the class u have used in OO Way.Class cl_salv_table doesn't exist....

      Thanks,
      Karthik

      Author's profile photo Former Member
      Former Member
      Sorry guys i m working on 4.6 B version thats why i coudnt find this class in version 5.0 is available.

      Thx,
      Karthik

      Author's profile photo Former Member
      Former Member
      Hi Karthik,

      So may I know which class are you using instead of Thomas' example class in version 4.7 ?

      Thank you.

      Handoko

      Author's profile photo Peter Inotai
      Peter Inotai
      Hi Handoko,
      You can use class cl_gui_alv_grid. Check demo program BCALV_GRID_DEMO for details.
      Best regards,
      Peter
      Author's profile photo Former Member
      Former Member
      Hi Thomas,

      Could you please create one example for the object oriented way in version 4.7 ?

      Thank you.

      Handoko

      Author's profile photo Former Member
      Former Member

      It is better solution for output:    ENDLOOP.

      Author's profile photo Former Member
      Former Member
      I wasted alot of time because you dont show properly that there has to be a space in alv->display( ), you writeit like display().
      another example is that a space is needed in ( IMPORTING and not (IMPORTING that you wrote
      small things like this waste alot of time but are so easily fixed!

      but still you write a great blog, and you have funny examples!

      Thankful regards,
      Makan

      Author's profile photo Former Member
      Former Member
      i see now that when i posted my comment,it seems that spaces are displayed but hard to see... maybe you could at least write out that there should be spaces for those people that doesnt copy paste, but like to write the code themselves.

      thanks again!

      Author's profile photo Former Member
      Former Member
      Hello Thomas,

      Thanks for the blogs.

      The table SFLIGHT is emty !! Help!!!

      Best regards

      Author's profile photo Thomas Weiss
      Thomas Weiss
      Blog Post Author
      You can use the program SAPBC_DATA_GENERATOR to create data in the relevant tables of the SAP flight model.

      Regards

      Thomas

      Author's profile photo Thomas Weiss
      Thomas Weiss
      Blog Post Author
      You can use the program SAPBC_DATA_GENERATOR to create data in the relevant tables of the SAP flight model.

      Regards

      Thomas

      Author's profile photo Former Member
      Former Member
      Hi, thanks for this blog. I'm a newbie to Netweaver though I currently program in VB.NET and C++. I installed the Downloadable Netweaver Trail version on Windows Vista Ultimate.
      However, when I got to this topic and tried out the code, I kept getting a sy-subrc return of 4 and no data is returned from my select statement.
      I've tried other codes with same result. I tried running the same queries from SQL Studio and got results but it would seem whenever its executed from Netweaver it fails. What do I do please. Its wasting a lot of my learning time since I can't really try out my hands on what I'm reading!
      Author's profile photo Former Member
      Former Member
      I tried using the data browser (SE16) to check if I could read the data from  Netweaver and I'm not getting any result either!
      Author's profile photo Former Member
      Former Member
      For education of those who might find themselves in a similar situation: it would seem I didn't have sample data installed.
      See my posting and replies here http://sap.ittoolbox.com/groups/technical-functional/sap-dev/all-sql-statements-fail-with-sysubrc-return-code-4-3741210#M3741277

      Or the instruction on how to load the sample data here: http://help.sap.com/saphelp_nw70/helpdata/en/db/7c623cf568896be10000000a11405a/content.htm