SAP ABAP Advanced Cookbook
上QQ阅读APP看书,第一时间看更新

Using field symbols and data references to print database table contents

Field symbols and references are an important combination for dynamic programming. Data references are addresses of data objects stored in reference variables. Field symbols are placeholders, or symbolic representations of these data objects.

This recipe shows how to print all the contents of a particular database table (the name of which is only known at runtime). For simplicity sake, we will only focus on the main logic pertaining to dynamic programming. In addition to data references and field symbols, we will also see some dynamic SQL statement application.

Getting ready

The code will let you create a small data browser program that will take as input the database table name whose contents are to be read, and the number of rows and columns (fields) to be displayed. The output displays the table's field names as column headers along with the data stored in the table.

The knowledge of the describe_by_data method of the cl_abap_structdescr class and widening (downcasting) will also be used.

How to do it...

For creating a program that prints the contents of an entered SAP table name, follow these steps:

  1. Declare parameters for inputting the name of the table whose data is to be accessed. Also we take as input the number of rows and columns (fields) that are to be displayed.
    How to do it...
  2. Declare field symbols for the internal table, the table row, and the table fields. In addition, data reference variables for internal table and the structure are defined.
    How to do it...
  3. We place a small check statement to make sure the program runs only when the columns value entered by the user is equal to one or more.
    How to do it...
  4. Next, we create the data objects using the create data statement. We then dereference and assign them to placeholders (field symbols) respectively. If the table name entered by the user is wrong, a cx_sy_create_data_error exception is generated, and therefore need to catch it in our coding using the catch statement.
    How to do it...
  5. Then, the SELECT statement is written in order to fetch the data. The name from the parameter is used as the table name and read into the internal table pointed by field symbol my_itab. The number of rows are also specified based on user input.
    How to do it...
  6. The description of the created row structure (pointed by my_struc) of the internal table is then read using the describe_by_data statement. An object reference descr to the class cl_abap_structdescr is declared. Then, the static method describe_by_data of the cl_abap_typedescr class is called and returned to the descr variable. The operator (?=) is used for downcasting. (As the cl_abap_typedescr class is an abstract class and returns a reference to the description object cl_abap_typedescr).
    How to do it...
  7. As each column (field) of the database table is represented as a row in the component components of the descr object, a loop is run on it. Only the number of columns entered by the user is read using the from 1 to columns.
  8. A positions internal table position is also created that will hold the position (starting position) of each column displayed on the screen. The length of each field is used for finding the next field position. Also included is the code for printing the table column header.
    How to do it...
  9. Then, the main part for reading the contents of the table is written. The loop is then run at the data internal table pointed to by field symbol my_itab. A do loop is also run for each row which used the assign statement for each field value. The table positions value is read for getting the correct position of the corresponding field column.
    How to do it...

How it works...

In the data browser program, the statements read any table name entered by the user and the number of rows and columns entered, and then display the relevant data from the table. Let us see how the program code works.

The parameters statement displays the selection screen to the user, and takes as input the table name and the table rows and columns.

How it works...

The static method describe_by_data of the cl_abap_typedescr class provides the description object pertaining to the structure passed.

The widening cast method is used to store the returned object in the descr variable of cl_abap_structdescr type (as the cl_abap_typedescr class is the super class of cl_abap_structdescr and cl_abap_typedescr is an abstract class).

The descr object contains a component internal table components. A loop is then carried out on the components table in order to print the name of the table fields as columns headers. The length of the field is also taken into consideration. For example, if the table T511 is entered, the length and names of the various table fields exists in DESCR->COMPONENTS. The position table is also filled within this loop in order to store the appropriate positions of the each. This will be later used so that the correct data is printed under the corresponding column header (field name).

How it works...

Finally, the main loop is run. The loop is carried out on the internal table, which contains all the rows of the database table in question. For each row of the table, the do loop is run. within the do loop, the components (fields) of the table row are processed and assigned to the field symbol <my_field>. The do loop is run the number of times equal to the number of table fields asked by the user. The value of the field is then outputted. Once all required fields have been processed, the do loop is exited. Within the do loop, the positions table filled earlier is read in order to get the correct position where a particular cell is to be positioned.

How it works...

The positions table along with the uline statements lets you give the box shape to the output.