Data Modeling with CDS Views – Part 3: Session Information, Case, Cast, Where, Input Parameter & Consumption filter
Introduction
This blog series will contain the basics of data modeling with CDS Views. The Part 1 will give you some information about the Syntax of a CDS view, Joins, Associations and Unions. You can find this blog here Data Modeling with CDS Views – Part 1: Basics .
Part 2 has handled the topics Currency/Unit Conversion & Date Functions. You can find this blog here Data Modeling with CDS Views – Part 2: Currency/Unit Conversion & Date Functions.
The goal of this final blog of the mini-series is to show you some more basic information about Data Modeling in ABAP CDS Views. It should give you a feeling how data modelling works and will therefore use easy and short examples.
Session Information
Session Information is created during runtime and can be used in fields or also filters.
The snippet shown above will provide you the client, date, language, and user from the person who runs this CDS-View. As result you get the required information.
Case
The concept of a case statement is the same as in any other programming language. As a small example below, see a case statement “when the “connid” is 0017 then the column “test” is 1 – otherwise it is 2”.
Several cases can be used in one statement.
Cast
With the Keyword CAST you can transfer an object to another possible data type. There is a matrix provided by SAP which combinations are allowed, not allowed, or only allowed under certain circumstances.
In this example an constant unconverted date will be transferred to the data type abap.dats – so a date field.
Where
Like in standard SQL, a where clause can be used to make a condition in the select statement which elements shall be read or filtered.
When executing this code snippet/where clause then only date having an erdat higher than 01.01.2018 will be taken into account. Of course, several where clauses can be used with a logical operator. You can use for example input parameters in the where clause as well. We are showing an example within the next chapter dealing with Input Parameter.
One important remark: a * cardinality can cause issues with associations when using Where clauses.
Input Parameter
By using an input parameter, you can add a prompt to select values.
Important to know is that the input parameter represents always single value and mandatory.
To create a parameter, you have to use the keyword WITH PARMETER when creating a view.
The field p_comp is the paramenter_name and after the symbol “:” the parameter_type follows – in this case bukrs_vf.
After defining parameter p_comp you can use it in your CDS View. As you can see, here we are filtering only the input company codes:
After execution, the user needs to enter then a company code in the prompt.
As result only data with company “1010” will be provided in the result.
There is also a detailed blog about this topic which you can found on https://www.zpartner.eu/dynamische-befuellung-von-variablen-mit-abap-cds/.
Consumption Filter
Using the annotation “consumption.filter” you can define a filter which restricts the data during consumption. The advantage of this concept is that you have more options. You can make the filter optional or mandatory, use default values or make single/multiple value etc. But this concept comes with some downsides as well. A consumption filter cannot be explicitly passed down to an underlying view. Furthermore it also depends on your front-end tool; if a consumption filter is working or not. It might be the case that the defined consumption filter is ignored.
The following code snippet shows you the annotation, to get a feeling of how the filter works:
Conclusion
In this blog you learned about some basic concepts about data modelling in ABAP CDS Views.