CDS Part 6. Basic Expressions & Operations Available for CDS View – I

Our ABAP for SAP HANA series has been a good starting point for many SAP HANA enthusiasts. It has helped hundreds of ABAPers to upgrade their skill and get into HANA projects. Many of our loyal readers requested us to put some more practical aspects of Core Data Services. So in this series, we would look into some untouched aspects of CDS Views.

Basic understanding of the Operations available for usage within CDS Views have been covered in this post. If you have not performed the Operations then this would be a useful 10 minutes read for you. If you have already started working with Operations in CDS and you are an Expert, still, going through this article would add on to your existing knowledge and it can help you join the missing links (if any). We promise, even the Pro would have some surprises and some take away points.

Different Aspects:

  • Literals

Other name Constants. They are of Two kinds viz Numeric and Character.

Eg. Numeric : 1000, 52 ; Character : ‘SAPSPOT’,’Europe’, ‘HANA’, ‘Ruthvik’.

  • Fields of Data Sources

If put simple, they are the fields of the database tables. But, they too have an ALIAS, that is ALIAS. Come on !!! I meant, an alternative name that is in turn ALIAS. It’s like Alias of Ruthvik is Ruthvik. ?

VIP (Very Important Point) – ALIAS play a very important role while we create Associations between different CDS Views/Database Tables.

Do brush up your Associations concepts.

  • Parameters

Anything that a user is expected to provide as input to CDS, qualifies as a parameter. Remember that CDS is PARAMETERISED!!!

The parameter must be prefixed by a colon (:) or $parameters.

In order to code with parameters, the code temple can be obtained automatically while creating CDS View itself. Huh!! Programmers do not need to by-heart them.

Follow as shown below:

Session variables

ABAPer’s blood sample will surely have “SY-SUBRC”, a system variable.
SAP is very strict, diktat is like “The variable is case-sensitive. $session.vname, $Session.Vname, and $SESSION.VNAME can all be used. No other spellings are allowed.” Same goes with CDS, session variables. Following are examples:

Variable Usage
user Current user name, the nominal value of the ABAP system field sy-uname 
client  Current client. The default value is the nominal value of the ABAP system field by-mandt. In reads with an Open SQL statement (with the statement USING CLIENT) and in calls of an AMDP method from ABAP (in whose declaration the addition AMDP OPTIONS CDS SESSION CLIENT is specified), the value specified here. 
system_language  Text environment language of the current internal session, the nominal value of the ABAP system field by-langu 
system_date  Current system date of the AS ABAP, the nominal value of the ABAP system field sy-datum 

Case distinction

Self Explanatory. Following can give you an idea :

Simple case distinction1. ... CASE operand
WHEN operand1 THEN result1
[WHEN operand2 THEN result2]
...
[ELSE resultn]
END ...Complex case distinction2. ... CASE WHEN cond_expr1 THEN result1
[WHEN cond_expr2 THEN result2]
[WHEN cond_expr3 THEN result3]
...
[ELSE resultn]
END ...

Conditions

Well, Well, this is interesting. The logical way of coding. How ?

Only two possible results – TRUE or FALSE. RIGHT or WRONG. ABAP_TRUE or ABAP_FALSE. UNDERSTAND or DONT UNDERSTAND.

The following are the variants:

  • Comparisons with relational operators

Operator True if 
lhs = rhs Value of lhs is equal to the value of rhs 
lhs <> rhs  Value of lhs is not equal to the value of rhs 
lhs < rhs  Value of lhs is less than the value of rhs 
lhs > rhs  Value of lhs is greater than the value of rhs 
lhs <= rhs  Value of lhs is less than or equal to the value of rhs 
lhs <= rhs  Value of lhs is greater than or equal to the value of rhs

  • Interval comparisons. Remember RANGES is ABAP ?? Yes, that is how intervals are captured.Simple in Open SQL, we have ‘BETWEEN‘ as keyword.

When simplified it takes the following form:

“… lhs >= rhs1 AND lhs <= rhs2 … “.

  • Pattern comparisons. We all know (at least that is expected), how CS, CP, NS, NP so on work….Don’t know? OKAY. Those are used on strings or character variables. (Wont be telling more). Basic Rules in SQL:
    – Only character-like data types can be used
    – Only character-like literals without domain prefix are allowed
    – The wildcard characters % for any strings and _ for any character can be used in the pattern

Note : Additionally we have something called as ESCAPE character. Syntax is as follows:

… lhs LIKE rhs [ESCAPE esc]…

  • Checks on the NULL Value

…. lhs IS [NOT] NULL …

Time for an example to cover all the above.

@AbapCatalog.sqlViewName: 'ZFLG_CDS_OP_V'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Operations'
define view ZFLG_CDS_OP
      with parameters p_date : abap.dats
  as select from sflight as a
{
  key a.carrid as FlgCarr,
  key a.connid as FlgConn,
  key case ( a.planetype )
  when '737-400' then 'BOEING'
  when 'A340-600' then 'AIRBUS'
  else 'OTHERS'
  end  as FlgType,
  key a.fldate,
     case 
     when a.price is null then 'error'
     when  a.price < 200  then 'Budget'
     when  a.price >= 200 and
           a.price < 400 then 'Business'
     else 'Very Costly'
            end      as flight_type,
  $session.system_language as Language        
}
    where a.fldate = $parameters.p_date;

Once you code and check what has been used:

Not Marked in the above screenshot. Literals, Alias. Please search them. If you cannot find, leave your comment. We will respond.

Excited to know the result !!! Do a Data Preview.

It just gets longer. Culprit ??? Parameter.

This is an unique feature we shall be missing every time we discuss. Never Forget

“CDS is Parameterised”

Provide the date. I knew it before hand.. I had already run my code without parameter.

Result :

Following need a lot of emphasis and examples.

  • Arithmetic expressions
  • Aggregate expressions
  • Type Casting