SAP ABAP CDS View

Here is an example of a simple CDS View and how you can use it in an ABAP program:


  1. 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.

  1. 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:

sql
REPORT 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

Popular posts from this blog

IT Asset Management Dengan PHP MySQL

PHP MySql CRUD Dengan Konsep MVC

Cara Sederhana Multi Insert Data Dengan PHP - MySQL