SAP ABAP CDS View
Here is an example of a simple CDS View and how you can use it in an ABAP program:
- Creating a CDS View
First, let's create a simple CDS View that retrieves data from the standard SAP table MARA (Material Master). Here's an example of the code:
less@AbapCatalog.sqlViewName: 'ZMATERIAL'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Material View'
define view Z_MM_MATERIAL as select from mara
{
key mara.matnr as MaterialNumber,
mara.mtart as MaterialType,
mara.matkl as MaterialGroup
}
This CDS View creates a view named "ZMATERIAL" that retrieves three fields from the MARA table: MaterialNumber, MaterialType, and MaterialGroup.
- Using the CDS View in an ABAP Program
Now, let's use this CDS View in an ABAP program. Here's an example of the code:
sqlREPORT z_test_cds_view.
DATA: lt_materials TYPE STANDARD TABLE OF z_mm_material.
SELECT * FROM z_mm_material INTO TABLE lt_materials.
LOOP AT lt_materials INTO DATA(ls_material).
WRITE: / ls_material-materialnumber, ls_material-materialtype,
ls_material-materialgroup.
ENDLOOP.
This ABAP program declares an internal table of type Z_MM_MATERIAL and selects data from the CDS View into the internal table. It then loops through the internal table and writes the MaterialNumber, MaterialType, and MaterialGroup fields to the screen.
Note that in order to use the CDS View in an ABAP program, you will need to activate it first. You can do this by right-clicking on the CDS View in the ABAP Development Tools (ADT) and selecting "Activate". Once the CDS View is activated, you can use it in your ABAP programs.
Comments
Post a Comment