AMDP: Avoiding FOR ALL ENTRIES and pushing calculation to Database Layer
The objective of this document is to explain step-by-step process to create AMDP method using multiple select queries to avoid FOR ALL ENTRIES and push calculation to database layer.
Requirement is to fetch records from database table MATDOC based on certain plant and storage location combination. On the fetched records, perform calculation e.g. summation on quantity based on various combinations e.g. Material/ Plant/Storage Location, Material/Plant, Material. The developer would like to leverage AMDP to address this requirement.
Relevant fields of MATDOC Table:
List of Materials
List of Plant and Storage Location combination
3. Understanding limitation in FOR ALL ENTRIES select statement
In a select query, with FOR ALL ENTRIES, one can’t use Group BY clause. The addition GROUP BY has no effect if FOR ALL ENTRIES is used.
With new directive of S/4 HANA coding, all the calculation should be pushed to database layer. Hence one can’t leverage the code pushdown if FOR ALL ENTRIES is used in select query.
To avoid FOR ALL ENTRIES in select query, one can go ahead and use multiple ranges for each field of driver table of select query. But with multiple ranges, we get cross referencing entries.
1. Range Table cross referencing entries
|Plant||Storage Location||Number of Entries in MATDOC with Plant/Storage Location combination||Number of Entries in MATDOC when both Plant/Storage location are passed as individual ranges|
As you can see number of entries are considerably increased because of cross referencing of plant and storage location i.e. Plant 0001 & Storage location 0002 combination AND Plant 1010 & Storage location 0002 combination is fetching extra (458 – 412 = 48) Entries.
The following steps explain step by step configuration:
1. Create an AMDP Method inside a class
Include the IF_AMDP_MARKER_HDB interface in the class. See below screenshot.
Define the method as below screenshot. Input parameters include list of materials and list of plant and storage locations.
2. Write first select statement
Prepare first select statement based on list of materials and list of plant & storage locations. See below screenshot. Pay attention to AMDP method implementation syntax.
Here we have used inner join on database table with input parameter table.
3. Write subsequent select statement
One good feature of AMDP is that one can write select statements on local variables e.g. local internal tables. Write second select statement on records fetched in 1st select statement and use GROUP BY clause.
4. Use GROUP BY clause in resulting dataset
Now when we have resulting dataset, we can write further select statements on local internal table obtained in 1st select statement with various conditions of GROUP BY class. This will enable us to perform quantity summation (calculation) and prepare output in desired format. One can write multiple select statements based on requirements. See below screenshot.
Now run the AMDP method by executing class from SE24 transaction. It should open the window to test the method.
Populate the Material List, Plant List and Storage Location List as below
Press execute button and see the result in export parameter table ET_PLANT_QTY
Coding part follows standard SQL Script references. Here select statement is broken into multiple steps depending upon select options.
See below screenshot for Class/Method definition
See below screenshot for Method implementation.
All standard limitations of AMDP such as:
- An AMDP class can only be edited in ADT (Eclipse).
- Client will not be handled automatically like in open SQL.
- In case of CDS Views, write appropriate annotations in CDS View definition for client handling so that they can be used inside AMDP. Accordingly, AMDP definition will change.
- Exposed associations in CDS Views can’t be accessed inside AMDP.
- As of now, AMDP only works when underlying database is HANA.