Friday 7 January 2022

My learnings with AMDP

In this blog, we will be covering some of the learnings of AMDP. We are covering different scenarios and errors related to it.

This is an attempt to help anyone who is starting with AMDP’s and need help with issues faced.

Further scenarios will be added in up-coming blogs or updated in this blog only.

Scenario 1) Add colon(:) sign as and when you are going to use a local variable in a method. Below error will come if there’s no colon added:

“LT_ABAP_DATA” is unknown. ABAP objects and DDIC objects must be declared in the METHOD statement. Local names must start with “:” here.

SELECT COUNT( * ) INTO LV_DSO_COUNT FROM :LT_ABAP_DATA WHERE LOW = 'XYZ';

In above code, we are using colons(:) before variable lt_abap_data. Lt_abap_data is created in method itself and need colon to be identified as local variable.

Scenario 2) If you are using any method of same AMDP class or other class or DDIC object, then that object must be declared in USING clause. If not, then you will encounter the below error:

“ZPCMCL_XYZ=>GET_ DATA” is unknown. ABAP objects and DDIC objects must be declared in the METHOD statement. Local names must start with “:” here.

Scenario 3) If you must pass value of a variable in all records, then following is the way to do:

E_view3 = SELECT ZP_PROGRM

:S_T_SCNR as ZP_SCNR

FROM: lt_all;

In above statement, we are selecting all records and putting the value of variable :S_T_SCNR into field ZP_SCNR.

Scenario 4) Using BETWEEN keyword from a range table.

FROM :lt_pbs_p102_tmp

                     WHERE

                        ZP_PRFTCR  IN ( SELECT LOW FROM :I_T_ABAP_DATA WHERE IOBJNM = 'ZP_PRFTCR' )

                        AND ZP_SCNR    = S_S_SCNR

                        AND ZP_VERSN   IN ( SELECT LOW FROM :I_T_ABAP_DATA WHERE IOBJNM = 'ZP_VERSN' )

                        "0FISCPER" BETWEEN ( SELECT LOW FROM :I_T_ABAP_DATA WHERE IOBJNM = '0FISCPER' )

                          AND ( SELECT high FROM :i_t_abap_data WHERE iobjnm = '0FISCPER' )

In above code , we are using BETWEEN keyword for field “0FISCPER” using Low and High.

Scenario 5) If you must pass default values, then below is the code.

e_view_temp = SELECT

                     CONCAT( lv_target_year ,'012') AS FISCPER,

                     '012' fiscper3,

In above code , we are putting default value ‘012’ in field FISCPER3.

Scenario 6) You can add arithmetic calculations while querying in where clause:

SAP AMDP, SAP ABAP, SAP ABAP Exam, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Preparation

Source: code snippet from personal development

In above query, we are doing calculations on FISCYEAR field by reducing one year.

Scenario 7) If the value is NULL and you want to pass a default, then you can use IFNULL method in select statement as shown below.

 et_out = select IFNULL(kurst,'2002') as kurst ,fcurr,tcurr,
                gdatu,
                ukurs
               from tcurr where tcurr = 'GBP';
In above code , if value of KURST is NULL then we are passing default value of ‘2002’.

Scenario 8) You can use keyword ::ROWCOUNT to count the number of rows found after a select statement.

SELECT low INTO e_t_versn FROM :i_t_abap_data WHERE iobjnm = 'T_ZP_VERSN';
IF ::ROWCOUNT = 0 THEN

END IF;

In above statement, we will have count of records after execution of select statement in variable ::ROWCOUNT. It’s similar to DESCRIBE TABLE in ABAP but be very careful of using this keyword, if you are using it for multiple times then it’s better to use COUNT(*) as we faced number of issues with this keyword when using multiple times. If it’s only one time, then we can use it without any issue.

Scenario 9) Use of CASE statement in Select query:

CASE
  WHEN s_t_scnr IS NOT NULL OR s_t_scnr <> '' THEN
    s_t_scnr
  ELSE
    ZP_SCNR
END AS ZP_SCNR,

Scenario 10) Dynamic WHERE clause in AMDP: There might be instances where you need to create dynamic where clause in AMDP(Dynamic queries are not recommended). So trying to cover one of the scenarios as shown below:

Step i):

lt_concat1 = SELECT '''' || LOW || '''' AS CONCAT_LOW
                                          FROM :I_S_ABAP_DATA
                                              WHERE IOBJNM = 'ZP_VERSN';
In above query, we are trying to select all data of LOW field from importing parameter I_S_ABAP_DATA and adding colons to the value. Below are the values of internal tables for better understanding:

:I_S_ABAP_DATA : This is the table we are querying. It’s a range table as you can see having ZP_VERSN with multiple values (not visible in pic).

SAP AMDP, SAP ABAP, SAP ABAP Exam, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Preparation

Source : code snippet from personal development

Once the query is executed, below will be result in internal table lt_concat1(Values with colons).

SAP AMDP, SAP ABAP, SAP ABAP Exam, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Preparation

Source : code snippet from personal development

Step ii) 

lt_concat = SELECT STRING_AGG(CONCAT_LOW,',') AS concat_low
                                            FROM :lt_concat1;
In above query, we are using STRING_AGG function to segregate the data into rows from internal table LT_CONCAT1. In this way, you will have data in LT_CONCAT as shown below(In single row):

SAP AMDP, SAP ABAP, SAP ABAP Exam, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Preparation

Source : code snippet from personal development

Step iii):

SELECT CONCAT_LOW INTO lv_concat_versn FROM :lt_concat;
IF ::ROWCOUNT > 0 THEN
  lv_s_versn := ' AND ZP_VERSN IN (' || (lv_concat_versn) || ')';
END IF;
 
In above query, we selecting converted values in variable lv_concat_Versn and creating WHERE clause. Below will be the value for variable lv_s_versn:

SAP AMDP, SAP ABAP, SAP ABAP Exam, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Preparation

Source : code snippet from personal development

In above code as you can see in highlighted text, field ZP_VERSN is ready to be filtered with two values. You can use the same logic for rest of your fields. We are adding AND operator because of there’s other fields before this field(you can change it according to your requirement).

Source: sap.com

No comments:

Post a Comment