The following section touches up the new features that are available while processing internal tables.
Using Secondary access to access internal tables
Internal tables are more like Database tables within the application server. If Database table can have secondary index why not internal tables.
Let’s say for an example we have an data which is going to be accessed in different ways in our program. For example, for Material Master tables we need to fetch certain records based on key field MATNR and certain fields based on RMATP( Reference Material). We generally get the records in the internal table sorted by field matnr as this it the key field. But when you need to read a record with Reference material then it is ideal that we sort the table according to RMATP and perform the binary search.
Having data in two different internal tables sorted differently is one option( waste of memory ) or sort the internal table according to the field with which data is going to be retrieved ( incurs processing time ).
The Following code block can be re-written with secondary index instead of sorting the values repeatedly.
Read access with secondary index.
Table Work Areas, No longer required.
With Header line internal table we have a default work area is created with the name same as internal table itself. This becomes quite confusing in the program as whether a statement access an internal table or work area.
SAP later introduced the concept of declaring a separate variable as work area with type similar to the internal table. This can be now avoided with work areas can be declared during the runtime itself.
Reading content from the internal table
We mostly read the data from the internal table using the read table statement. In the above section we have seen how we are going to eliminate the need for work area declaration to read the data. In this section we will eliminate the read table statement itself.
The Read access to the internal table can be simplified as below.
Now what happens if the table entry that we are looking for is not available. System triggers an exception in that case and goes for dump. it does not set sy-subrc unlike read table. This can be avoided by handling exception CX_SY_ITAB_LINE_NOT_FOUND OR with use of OPTIONAL keyword after release 740 SP08. If the record that you are looking for does not exist, then the system provides you the with initial value for the row according to datatype. [ space for characters, 0 for numbers ]
If i wanted to get a particular field matnr from the internal table according to index or key posting then we can specify the component name and read the value.
If the Material you are looking for does not exist you can also assign a default value as well.
If the row does not exist, material number will be filled with default value 102323.
Note: ls_mara_2 here in this example will be variable and not work area as we are getting only matnr value.
Move Corresponding for Internal Table
Most often we have the needs to move the content of one internal table to another. When the table component names differ we have to loop and move the records.
The matching components can be moved using the keyword “MOVE-CORRESPONDING” and then code to map the other fields. Since SAP Does not recommend use of MOVE-CORRESPONDING to avoid overflowing data to be mapped to a different component we had to code each and every component assignment by ourself.
This can be now handled with new constructor Operator CORRESPONDING.
Let’s say you have a below situation which cannot be handled before without a loop.
- The Fields names matches between two tables, yet you do not want to move the data for one component alone. ( Example: You have status column in both internal table but you do not want to move that column values and move the remaining columns )
- Field/ Component name differs between two internal tables.
Following Statement can also be used to move content between two tables.
New Functions for common internal table tasks
Built in functions are provided for some of the common internal table tasks that we do.
In case like open cursor technique when we wanted to loop over from the next record from internal table we get the index position to get the sy-tabix value and loop the internal table record from that position.
To Check if the record exist in the internal table you can use the built-in function line_exist inplace of read statement. In the previous topics above we have discussed on reading the values from internal table, in case where the record does not exist the system will raise an exception. LINE_EXIST statement can be used to determine if the record exist before reading it.
Note: In one of the previous topics we discussed, we no longer needed the RHS for the condition check. By Default system assigns the condition “IS NOT INITIAL”.
Internal tables Queries with REDUCE
Often after filling up the internal table we have to perform the loop on the records to derive some calculation values. Now for Simple expressions instead of looping you can using the reduce function to query on the internal table and get the data.
E.g In the SFLIGHT table we have the fields SEATSMAX_B and SEATSOCC_B which represents the seats maximum and occupied in business class. Now out of the available entry in the internal table i wanted to get a count as how many flights has the seats left.
This can be derived by adding a reduce query in internal table as below.
I am an SAP Netweaver Certified developer associate. I work in UI5 , Fiori, ABAP and HANA topics. I do freelance projects and also takes training in SAP UI5 and ABAP. For Training/ Freelance requirements reach out to me through Contact page