Skip to Content
Technical Articles

Part#3. CDS View with Expressions and Session Variables

This is the 3rd blog post on the CDS detailed explanations after the Introduction post. Refer to the Introduction and blog post indexes here:
https://blogs.sap.com/2019/10/21/part1.-sap-cds-views-demystification/

As you saw in the previous blog post, in the Basic CDS view the data has been read with a simple ‘Select’ statement. So how about some business logic calculations? Can we have some Mathematical Expressions configured in the CDS view?  Yes, absolutely, let’s create a simple CDS view with an Expression to calculate the available seats in business class from table SFLIGHT;

Check the output below.  Column Seats_Avlbl is the result of a mathematical expression configured within CDS view.

 

=> ‘WHERE’ CLAUSE AND SESSION VARIABLES:

Let’s check how to add a ‘WHERE’ clause to filter the data and how to make use of ‘Session Variables’.  Similar to ABAP session variables like sy-datum for current Date, sy-uzeit for current Time etc. , CDS views editor also has session variables which can provide these values.  See below the syntax for Client, system_date, system_language, user can be determined within ‘WHERE ‘ clause.

 

Select ‘system_date’ to compare ‘Current Date’ with Flight Date.

@AbapCatalog.sqlViewName: 'ZSQL_VIEW_EXPRSN'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'CDS view with  Expression'
define view ZCDS_VIEW_EXPRESSION as select from sflight 
 {
    key carrid, 
    connid,
    seatsmax_b,
    seatsocc_b,
    fldate,
    (seatsmax_b - seatsocc_b) as Seats_Avlbl
    }
    
 
where fldate <= $session.system_date 

 

In summary, we saw how to add Expressions, Where Clause to the CDS view and how to use Session variables to get some system data.

 

Part# 4. Consume CDS View in an ABAP Program: Check the new ABAP syntax to consume CDS view.

 

Follow for upcoming blog posts: https://people.sap.com/kumarsanjeev

Keep learning..

SK

Be the first to leave a comment
You must be Logged on to comment or reply to a post.