Chapter 5: HANA Database Data Retrieval (AMDP)
In the previous chapter, we met the "conductor" of our program, which masterfully orchestrates the entire reporting process. We saw it delegate the most critical task—fetching the data—to a specialized helper. Now, it's time to meet that specialist.
Imagine you need to write a research paper and you need information from 50 different books in a giant library. You have two options:
- The Old Way: You run to the library, find the first book, run back to your desk to read it, then run back to the library for the second book, and so on. This is incredibly slow and inefficient.
- The Smart Way: You give your list of 50 books to a specialized librarian who works inside the library. This librarian knows the fastest way to gather everything, collect all the books, and bring them to you in a single trip.
Our program uses the "smart way" to get data from the SAP HANA database. This high-performance technique is called an ABAP Managed Database Procedure (AMDP).
What is an AMDP?
An AMDP is a special kind of programming object that lets us write database query logic in its native language (in our case, SQLScript) and "push" that logic down to be executed directly on the HANA database.
Instead of our ABAP program asking the database for raw data from Table A, then raw data from Table B, and trying to combine them itself (the "running back and forth" method), we give the database a single, complex set of instructions. The database, which is designed for this kind of work, performs all the heavy lifting—filtering, joining, and aggregating the data—and returns only the final, neat result set.
This is the secret to our report's speed and efficiency.
How Our Report Uses the AMDP
In our program's get_amdp_data subroutine, you see a very clean call to our AMDP class, zcl_amdb_fico_tax.
" 1. Create an object for our specialized data retriever.
DATA(lo_amdp) = NEW zcl_amdb_fico_tax( ).
" 2. Call its 'get_data' method to fetch the raw data.
lo_amdp->get_data(
EXPORTING
im_qacdoca = ex_acdoca " The user's filters for table ACDOCA
im_qbset = ex_bset " The user's filters for table BSET
IMPORTING
ex_final = gt_final " The table to receive the raw data
).
Let's break down this simple-looking call:
- We create an instance of our "specialized librarian,"
zcl_amdb_fico_tax. - We call its
get_datamethod and hand over our "shopping lists" (ex_acdocaandex_bset) that we prepared in Chapter 1: User Selection Criteria. - The method works its magic and fills our
gt_finaltable (which is based on the "raw"ty_finalblueprint from Chapter 3: Report Data Model) with all the requested data.
To the main program, the process is simple. It just makes one call. All the complexity is hidden inside the AMDP.
Under the Hood: A Trip to the Database
What happens when that get_data method is called? It's a direct line to the HANA database.
The ABAP program doesn't pull bits and pieces of data. It delegates the entire task to the AMDP, which in turn tells the HANA Database Engine to execute the complex logic. The database does all the work efficiently and sends back just one clean package of data.
Diving into the AMDP Code
Let's peek inside the zcl_amdb_fico_tax class to see how it's built.
First, the class definition needs a special "marker" to tell the system it's an AMDP.
File: zcl_amdb_fico_tax.txt
CLASS zcl_amdb_fico_tax DEFINITION PUBLIC.
INTERFACES if_amdp_marker_hdb. " <-- This marker enables AMDP features!
" ... method definitions ...
ENDCLASS.
The line INTERFACES if_amdp_marker_hdb is crucial. It's like giving our librarian a special keycard that grants them access to the database's private work areas.
Next, the get_data method itself is declared with special keywords.
File: zcl_amdb_fico_tax.txt
METHOD get_data BY DATABASE PROCEDURE
FOR HDB
LANGUAGE SQLSCRIPT.
-- Code written here is SQLScript, not ABAP!
-- It runs directly on the HANA database.
ENDMETHOD.
This signature signals that everything inside this method is high-performance SQLScript code meant for the HANA database (HDB).
Inside the method, the logic is pure database query language. Here's a simplified view of the two key steps:
Step 1: Apply the User's Filters The first thing the AMDP does is take the packaged filter strings from our ABAP program and apply them to the database tables.
-- This is SQLScript, not ABAP!
-- Take the BSET filter string and apply it to the BSET table
lt_bset = APPLY_FILTER ( bset, :im_qbset );
-- Do the same for the ACDOCA table
lt_acdoca = APPLY_FILTER ( acdoca, :im_qacdoca );
The :im_qbset variable holds the filter criteria (like Company Code and Fiscal Year) that the user entered. The APPLY_FILTER function is a powerful tool that turns this into a dynamic WHERE clause for our query.
Step 2: Join Tables and Select Data
Now that we have our pre-filtered tables (lt_bset and lt_acdoca), the AMDP can perform the main query: joining them together to build our final raw dataset.
-- Join the filtered data and select the required fields
ex_final = SELECT
s.bukrs, -- Company Code
s.belnr, -- Document Number
s.gjahr, -- Fiscal Year
a.budat, -- Posting Date
a.sgtxt -- Item Text
FROM :lt_bset AS s
INNER JOIN :lt_acdoca AS a ON s.belnr = a.belnr
AND s.bukrs = a.rbukrs;
This looks like a standard SQL query, and it is! But the magic is that it's running at incredible speed right inside the database. The result of this query is directly assigned to ex_final, which is the exporting parameter that fills our ABAP table gt_final.
Conclusion
In this chapter, we pulled back the curtain on the most powerful part of our report's engine: the HANA Database Data Retrieval (AMDP) layer.
We learned that:
- An AMDP is a "code pushdown" technique that runs complex queries directly on the HANA database for maximum performance.
- It's like giving a complex order to a specialized librarian who works inside the library, avoiding slow, back-and-forth trips.
- Our report uses an AMDP class (
zcl_amdb_fico_tax) to join multiple financial tables and apply user filters efficiently. - This is done by writing logic in SQLScript inside a special AMDP method.
We've now fetched our raw data with lightning speed. But this data is still a bit rough. For example, it might contain a vendor number (100345) but not the vendor's name ("Global Supplies Inc."). How do we polish this raw data into a more user-friendly format? That's the final step in our data preparation journey.
Next: Chapter 6: Data Transformation and Enrichment
Generated by AI Codebase Knowledge Builder