SAP Common Table Expressions (CTE) in Open SQL
SAP Common Table Expressions (CTE) in Open SQL
SAP Common Table Expressions (CTE) in Open SQL
After the release of ABAP 7.51, a feature called CTE is introduced. The Keyword for using this is WITH, this allows to
define one or more common table expressions. Each CTE is a subquery that produces tabular data set (like internal tables)
that can be used as a data source in the subsequent queries encapsulated within it.
Here we will see a very small example of its illustration. Let us develop a very simple report that accept Purchase order
numbers in input and shows the following result in ALV grid.
List Field
1.Material Number
2.Material Name
3.Purchase Order Number that the Material involved in
4.Material Amount in the corresponding purchase order
5.Purchase Order Type
6.Purchase Order Date
7.Total Number of Items in the corresponding purchase order
8.Total Amount of all items in the purchase order
Before CTEs, we either go for individual select queries and loops to construct the output, but using CTEs we can bring the
entire output in on statement that has embedded common table expressions.
Syntax:
WITH
+cte1[( name1, name2, ... )] AS ( SELECT subquery_clauses [UNION ...] ),
[associations][,
+cte2[( name1, name2, ... )] AS ( SELECT subquery_clauses [UNION ...] ),
[associations],
... ]
SELECT mainquery_clauses
[UNION ...]
INTO|APPENDING target
[UP TO ...] [OFFSET ...]
[abap_options].
...
[ENDWITH].
It is mandatory that every sub table expression should have the symbol + as prefix and each sub table expressions can be
used in all subsequent queries. Each CTE should be separated by a comma and final select query should be present to get the
data into internal table. ENDWITH is optional.
CTE 1: Get the Materials and its Purchase order details using MAKT, EKPO and EKKO into +material_items (CTE 1) as
per the input given in select options
CTE 2: Get all the purchase order items detail from EKPO using the selected Purchase orders in +material_items (CTE 1)
into +po_total_items (CTE 2)
CTE 3: Count and store the total number of purchase order items for each purchase order using the +po_total_items (CTE 2)
into +po_tot_count (CTE 3)
CTE 4: Sum and store the total number of purchase order items for each purchase order using the +po_total_items(CTE 2)
into +po_tot_amount(CTE 4)
WITH
""->CTE 1.Getting the Material and PO item details
+material_items AS ( SELECT a~matnr,
a~maktx,
b~ebeln,
b~ebelp,
b~netwr,
b~meins,
c~bstyp,
c~aedat
FROM makt AS a
INNER JOIN ekpo AS b
ON b~matnr EQ a~matnr
AND b~loekz NE @abap_true
FROM +material_items AS a
LEFT OUTER JOIN +po_tot_count AS b
ON b~ebeln EQ a~ebeln
ORDER BY a~matnr,
a~ebeln
INTO TABLE @DATA(lt_out).
Input Screen: Materials Number 30002 and 85 is given.
Output:
""->Data Declaration
DATA : lv_matnr TYPE makt-matnr.
START-OF-SELECTION.
""->Form to Fetch the Data from Database Table
WITH
""->1.Getting the Material and PO item details
+material_items AS ( SELECT a~matnr,
a~maktx,
b~ebeln,
b~ebelp,
b~netwr,
b~meins,
c~bstyp,
c~aedat
FROM makt AS a
INNER JOIN ekpo AS b
ON b~matnr EQ a~matnr
AND b~loekz NE @abap_true
FROM +material_items AS a
LEFT OUTER JOIN +po_tot_count AS b
ON b~ebeln EQ a~ebeln
ORDER BY a~matnr,
a~ebeln
INTO TABLE @DATA(lt_out).
ENDFORM.