0% found this document useful (0 votes)
495 views

AMDP - Avoiding FOR ALL ENTRIES and Pushing Calculation To Database Layer - SAP Blogs

This document explains how to create an ABAP managed database procedure (AMDP) to fetch records from a database table, perform calculations on the records, and avoid using FOR ALL ENTRIES. It involves using multiple select queries with ranges on key fields to fetch the records and group calculations. Limitations of FOR ALL ENTRIES include not being able to use GROUP BY and not pushing calculations to the database layer. The AMDP leverages multiple select queries and joins between input tables to fetch records and perform grouped calculations in the database layer.

Uploaded by

heavy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
495 views

AMDP - Avoiding FOR ALL ENTRIES and Pushing Calculation To Database Layer - SAP Blogs

This document explains how to create an ABAP managed database procedure (AMDP) to fetch records from a database table, perform calculations on the records, and avoid using FOR ALL ENTRIES. It involves using multiple select queries with ranges on key fields to fetch the records and group calculations. Limitations of FOR ALL ENTRIES include not being able to use GROUP BY and not pushing calculations to the database layer. The AMDP leverages multiple select queries and joins between input tables to fetch records and perform grouped calculations in the database layer.

Uploaded by

heavy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

8/9/23, 15:11 AMDP: Avoiding FOR ALL ENTRIES and pushing calculation to Database Layer | SAP Blogs

AMDP: Avoiding FOR Assigned Tags

ALL ENTRIES and SAP S/4HANA


ABAP Development
pushing calculation to amdp

Database Layer CALCULATION IN DB


LAYER
for all entries
10 6 27,780
group by
inner join with internal
table
1. Objective
View more...

The objective of this document is to explain step-by-step


process to create AMDP method using multiple select
Similar Blog Posts
queries to avoid FOR ALL ENTRIES and push calculation
to database layer. Understanding evolution
of CDS and AMDP in
most simple way
2. Requirement By Ujjwal Singh May 19,
2019

Requirement is to fetch records from database table


ABAP Managed
MATDOC based on certain plant and storage location Database Procedure
combination. On the fetched records, perform calculation By Şevval Özkan Oct 26,
e.g. summation on quantity based on various combinations 2021
e.g. Material/ Plant/Storage Location, Material/Plant,
Material. The developer would like to leverage AMDP to AMDP Statement
performance trace
address this requirement.
By Ankit Rastogi Feb 13,
2019
Relevant fields of MATDOC Table:

Fields Type Key


Related Questions

WERKS WERKS_D How do we convert a


HANA calculation View
File into ABAP CDS?
MATNR MATNR
By Diganto Goswami Jul
24, 2017
LGORT LGORT_D

Do we have any class by


ERFMG ERFMG which we can create
CDS and AMDP's from
SAP GUI itself ?
By Vimal Sharma Jun 02,
2017

AMDP select from


document store
Input Tables: collection
By Marco Hernandez May
List of Materials 17, 2019

https://blogs.sap.com/2018/06/19/amdp-avoiding-for-all-entries-and-pushing-calculation-to-database-layer/ 1/11
8/9/23, 15:11 AMDP: Avoiding FOR ALL ENTRIES and pushing calculation to Database Layer | SAP Blogs

Fields Type Key

MATNR MATNR

List of Plant and Storage Location combination

Fields Type Key

WERKS WERKS_D

LGORT LGORT_D

Output Tables:

Fields Type Key

MATNR MATNR

WERKS WERKS_D

ERFMG ERFMG
(SUM)

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.

https://blogs.sap.com/2018/06/19/amdp-avoiding-for-all-entries-and-pushing-calculation-to-database-layer/ 2/11
8/9/23, 15:11 AMDP: Avoiding FOR ALL ENTRIES and pushing calculation to Database Layer | SAP Blogs

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 Number of Number o


Location Entries in Entries in
MATDOC MATDOC
with when both
Plant/Storage Plant/Stor
Location location a
combination passed as
individual
ranges

0001 0001 412

1010 0002 0

SUM 412 460

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.

4. Configuration
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.

https://blogs.sap.com/2018/06/19/amdp-avoiding-for-all-entries-and-pushing-calculation-to-database-layer/ 3/11
8/9/23, 15:11 AMDP: Avoiding FOR ALL ENTRIES and pushing calculation to Database Layer | SAP Blogs

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

https://blogs.sap.com/2018/06/19/amdp-avoiding-for-all-entries-and-pushing-calculation-to-database-layer/ 4/11
8/9/23, 15:11 AMDP: Avoiding FOR ALL ENTRIES and pushing calculation to Database Layer | SAP Blogs

write multiple select statements based on requirements. See


below screenshot.

5. Test
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

https://blogs.sap.com/2018/06/19/amdp-avoiding-for-all-entries-and-pushing-calculation-to-database-layer/ 5/11
8/9/23, 15:11 AMDP: Avoiding FOR ALL ENTRIES and pushing calculation to Database Layer | SAP Blogs

Press execute button and see the result in export parameter


table ET_PLANT_QTY

6. Coding
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.

https://blogs.sap.com/2018/06/19/amdp-avoiding-for-all-entries-and-pushing-calculation-to-database-layer/ 6/11
8/9/23, 15:11 AMDP: Avoiding FOR ALL ENTRIES and pushing calculation to Database Layer | SAP Blogs

7. Limitation
All standard limitations of AMDP such as:

1. An AMDP class can only be edited in ADT (Eclipse).


2. Client will not be handled automatically like in open
SQL.
3. 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.
4. Exposed associations in CDS Views can’t be
accessed inside AMDP.
5. As of now, AMDP only works when underlying
database is HANA.

https://blogs.sap.com/2018/06/19/amdp-avoiding-for-all-entries-and-pushing-calculation-to-database-layer/ 7/11
8/9/23, 15:11 AMDP: Avoiding FOR ALL ENTRIES and pushing calculation to Database Layer | SAP Blogs

Alert Moderator

10 Comments

You must be Logged on to comment or reply to a post.

Gaurav Sharan
June 19, 2018 at 10:24 am

Cool...Straight forward example.. ! Well Done !!

Like 1Share

Mehmet Dagnilak
June 20, 2018 at 9:04 am

I loved the ideas of joining with an internal table and selecting from an local internal table
with grouping. Thank you very much for pointing these!

I wonder how Hana handles selecting from an internal table. Does it read all the data
into the internal table and then summarize it, or does it do all the calculations in one
step?

Like 0Share

Ankit RastogiBlog Post Author


June 25, 2018 at 4:59 am

Hi Mehmet
Follow
I guess it would be similar to any other DB tables i.e. fetch all the data then do
the summation.
Like
Thanks

RSS Ankit
Feed

Like 0Share

https://blogs.sap.com/2018/06/19/amdp-avoiding-for-all-entries-and-pushing-calculation-to-database-layer/ 8/11
8/9/23, 15:11 AMDP: Avoiding FOR ALL ENTRIES and pushing calculation to Database Layer | SAP Blogs

Zhe Zhao
June 21, 2018 at 5:38 am

With ABAP 7.52, an internal table can be specified as a data source data source of a
query. So we can join an internal table with datebase table directly by using Open SQL.
However, it is not possible to use more than one internal table in an SQL command.

Like 3Share

Mehmet Dagnilak
June 25, 2018 at 6:40 am

I loved this even better 🙂 I wish companies could upgrade more often..

Like 0Share

Ankit RastogiBlog Post Author


June 25, 2018 at 4:57 am

Thanks Zhe for letting us know on that.

Like 0Share

Sijin Chandran
September 19, 2018 at 7:26 am

Hi Ankit,

First very thanks for this informative blog.

Am pretty new to AMDP coding, I have one question,

While implementing AMDP Methods like below:

METHOD get_kna1_catalog BY DATABASE PROCEDURE


FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING kna1.

can't we have more than one Table reference in the using part ( highlighted below ).

USING kna1

https://blogs.sap.com/2018/06/19/amdp-avoiding-for-all-entries-and-pushing-calculation-to-database-layer/ 9/11
8/9/23, 15:11 AMDP: Avoiding FOR ALL ENTRIES and pushing calculation to Database Layer | SAP Blogs

In my case in the same method I want to fetch from various related tables like knvv, knvp
etc and at this part am not able to mention more than one table.

Helpful views much appreciated.

Thanks,

Sijin

Like 0Share

Sijin Chandran
September 20, 2018 at 1:16 pm

Found the answer,

Yes we can :

https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-
US/abenamdp_functions_abexa.htm

Like 0Share

S Patil
May 22, 2019 at 2:53 pm

Hi Ankit,

Very informative blog ! Thank you !!!

Thanks,

Sourabh

Like 0Share

B@lu .
October 27, 2019 at 12:08 pm

i have some doubts can any one clear me

1.can we use AMDP for SAP supported Database(s) other than HANA?

2.if a CDS view is implemented with AMDP Table Functions. so can we access it by
Open SQL statements?

https://blogs.sap.com/2018/06/19/amdp-avoiding-for-all-entries-and-pushing-calculation-to-database-layer/ 10/11
8/9/23, 15:11 AMDP: Avoiding FOR ALL ENTRIES and pushing calculation to Database Layer | SAP Blogs

https://blogs.sap.com/2018/06/19/amdp-avoiding-for-all-entries-and-pushing-calculation-to-database-layer/ 11/11

You might also like