Write ABAP program for SAP S/4HANA based on the SQL query below to produce ALV report. Start and end of the report period and items number threshold should be parameters on the selection screen.
SELECT VBRP.VBELN, COUNT(distinct POSNR) as ItemCount FROM VBRP JOIN VBRK ON VBRP.VBELN = VBRK.VBELN WHERE FKDAT between 20220101 and 202221231 GROUP BY VBRP.VBELN HAVING COUNT(distinct POSNR) > 10 ORDER BY 2 DESCENDING
Below is an example of an ABAP report that uses the given SQL query to display an ALV report in SAP S/4HANA. Please note that in SAP S/4HANA, it is recommended to use Core Data Services (CDS) views and ABAP Managed Database Procedures (AMDP) for better performance and integration with the HANA database, but for the traditional ABAP approach, the following program can be used:
REPORT z_alv_sql_query. TABLES: vbrp, vbrk. PARAMETERS: p_start TYPE vbrk-fkdat OBLIGATORY, p_end TYPE vbrk-fkdat OBLIGATORY, p_thres TYPE i OBLIGATORY. TYPES: BEGIN OF ty_vbrp, vbeln TYPE vbrp-vbeln, itemcount TYPE i, END OF ty_vbrp. DATA: it_vbrp TYPE TABLE OF ty_vbrp, wa_vbrp TYPE ty_vbrp. DATA: o_alv TYPE REF TO cl_salv_table. START-OF-SELECTION. SELECT vbrp~vbeln, COUNT( DISTINCT posnr ) AS itemcount FROM vbrp INNER JOIN vbrk ON vbrp~vbeln = vbrk~vbeln WHERE vbrk~fkdat BETWEEN @p_start AND @p_end GROUP BY vbrp~vbeln HAVING COUNT( DISTINCT posnr ) > @p_thres ORDER BY itemcount DESCENDING INTO CORRESPONDING FIELDS OF TABLE it_vbrp. IF it_vbrp IS NOT INITIAL. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = o_alv CHANGING t_table = it_vbrp ). o_alv->display( ). CATCH cx_salv_msg. MESSAGE 'Error while creating ALV report' TYPE 'E'. ENDTRY. ELSE. MESSAGE 'No data found for the selected criteria' TYPE 'I'. ENDIF.
This program first defines parameters for the selection screen to capture the start and end date of the report period (p_start
, p_end
) and the threshold for the number of items (p_thres
). It then performs a SELECT query using the provided SQL within the START-OF-SELECTION
event. The result is stored in an internal table it_vbrp
.
The program uses the class cl_salv_table
to generate an ALV grid instance and display the data from the internal table. If no data is found or there is an error in creating the ALV display, it will output a message accordingly.
When adapting this program to your system, make sure that the types and field names correspond to the ones in your database and that the authorization for SQL queries and ALV display is granted accordingly.
google.com
stackoverflow.com
blogs.sap.com
If new Open SQL syntax is used, all host variables must be escaped using @. The variable IT_VBRP is not escaped. Rewrite the program.
Certainly, in the ABAP Open SQL statements in a SAP S/4HANA environment, variables must be preceded by the escape character @
to be correctly interpreted as host variables. Below is the revised program with the necessary corrections:
REPORT z_alv_sql_query. TABLES: vbrp, vbrk. PARAMETERS: p_start TYPE vbrk-fkdat OBLIGATORY, p_end TYPE vbrk-fkdat OBLIGATORY, p_thres TYPE i OBLIGATORY. TYPES: BEGIN OF ty_vbrp, vbeln TYPE vbrp-vbeln, itemcount TYPE i, END OF ty_vbrp. DATA: it_vbrp TYPE TABLE OF ty_vbrp, wa_vbrp TYPE ty_vbrp. DATA: o_alv TYPE REF TO cl_salv_table. START-OF-SELECTION. SELECT vbrp~vbeln, COUNT( DISTINCT posnr ) AS itemcount FROM vbrp INNER JOIN vbrk ON vbrp~vbeln = vbrk~vbeln WHERE vbrk~fkdat BETWEEN @p_start AND @p_end GROUP BY vbrp~vbeln HAVING COUNT( DISTINCT posnr ) > @p_thres ORDER BY itemcount DESCENDING INTO TABLE @it_vbrp. IF it_vbrp IS NOT INITIAL. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = o_alv CHANGING t_table = it_vbrp ). o_alv->display( ). CATCH cx_salv_msg. MESSAGE 'Error while creating ALV report' TYPE 'E'. ENDTRY. ELSE. MESSAGE 'No data found for the selected criteria' TYPE 'I'. ENDIF.
Notice the use of the @
symbol in the SELECT
statement before the variables p_start
, p_end
, and p_thres
, as well as before it_vbrp
in the INTO TABLE
clause. This ensures that the new Open SQL syntax in ABAP is adhered to.
google.com
sapforbeginner.wordpress.com
stackoverflow.com