Creating your first basic ABAP report from selected data

Now that you have logged in and create a very basic report using the write statement to display some text it is now time to introduce the select command so you can retrieved data from a database table to display in your report. For this we are going to use table EKKO to get the data for the report.

There is actually a lot involved in this lesson as we need to declare data types to store data, fetch the data and then display it but there is no point in covering this in separate lessons as you cant really see whats happing until its all put together. Note the comments within the code which can be added any where into ABAP code by adding a '*' at the very beginning of the line or by adding '"' imediately before the text.

Step 1 - Add code to create data types
Starting with the helloworld program created in lesson 2, add the folloing code to declare you data variables.

* Creates a single line structure variable with the same structure as the ekko database
  data: wa_ekko type ekko.
* Creates an internal table
* i.e. a multi line itab variable with the same structure as the ekko database
  data: it_ekko type standard table of ekko.


Step 2 - Add code to select data from table
For this example simple add the following code to perform the select. This will select the first 10 rows from database table ekko and store it in internal table it_ekko.

  Select *           		"* means get all fields
   up to 10 rows		"only retrieves 10 rows
    from ekko			"Gets data from ekko table
    into table it_ekko.   	"Internal table to store retrieved data in


Step 3 - Add code to display data
Now add the following code to display the data to the user

  loop at it_ekko into wa_ekko.  	"loops around the data in table it_ekko
    write:/ wa_ekko-ebeln.		"Writes data currently stored in wa_ekko-ebeln field
  endloop.  			        "end of loop	


Step 4 - Further reading on the select statement
Now you have implemented a simple select statement you can check out this page for more information on the select statement and how it can be used. Also see following link for useful SAP tables

Next Lesson - Add Selection Screen


Related Articles

SAP tutorials to tech yourself how to develop ABAP reports, programs and other objects
Adding a basic selection screen to your first ABAP report