F2
F2
F2
Provide SQL VIEW NAME, provide a meaningful end user text label, provide view type as BASIC.
On the define view statement, provide the select form as “SFLIGHT”, mention the field names marking key fields. Activate
and execute the view.
@AbapCatalog.sqlViewName: ‘ZSFLIGHTVIEW’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Flight Information’
@VDM.viewType: #BASIC
define view Zflight_View as select from sflight{
key sflight.carrid as AirlineCode,
key sflight.connid as ConnectionNumber,
key sflight.fldate as FlightDate,
sflight.price as Airfare,
sflight.currency as FareCurrency,
sflight.planetype as PlaneCategory,
sflight.seatsmax as MaxAvailableSeats,
sflight.seatsocc as OccupiedSeats
}
Here is the CDS view output.
Once the DATA Definition is activated, ddl sql view is created.
ABAP CDS on HANA-2
Open HANA Studio. Goto ABAP perspective. Open the project, Navigate to the package. Right click on the package & select
New->Other ABAP Repository Object.
Expand Code Data Services & select DDL Source and finally select NEXT button.
@AbapCatalog.sqlViewName: ‘sql_view_name’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: ‘Flight CDS View with parameter’
define view Zflight_Param_View as select from data_source_name {
}
Provide the code, declare an input parameter as ‘flight_code’ with data element type ‘s_carr_id’ and make use of that in the
where condition.
@AbapCatalog.sqlViewName: ‘ZSFLIGHTPARVIEW’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Flight CDS View with parameter’
@VDM.viewType: #BASIC
define view Zflight_Param_View
with parameters flight_code: s_carr_id
as select from sflight{
key sflight.carrid as AirlineCode,
key sflight.connid as ConnectionNumber,
key sflight.fldate as FlightDate,
sflight.price as Airfare,
sflight.currency as FareCurrency,
sflight.planetype as PlaneCategory,
sflight.seatsmax as MaxAvailableSeats,
sflight.seatsocc as OccupiedSeats
} where carrid = $parameters.flight_code
Activate the cds view. After activation, ddl sql view is created.
In the selection condition, we can see the input condition.
So here is the output. To change the input parameter value, select the Parameter link.
Try with a different input value.
ABAP CDS on HANA-3
We have the CDS view CDS view with Parameter . Now let’s use this CDS view in the report/program select query.
Program Code:
To check if DB supports VIEWS_WITH_PARAMETERS usually class CL_ABAP_DBFEATURES is used but now we can
directly use ##db_feature_mode[views_with_parameters] as a part of SQL query.
@AbapCatalog.sqlViewName: ‘ZSFLIGHT_EXP’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Flight Information’
@VDM.viewType: #BASIC
Run the View/Data Preview- The column heading appears same as the column names.
We can explicitly define the Column Name list as below(list name numbers should be equal to the selection column
numbers)
@AbapCatalog.sqlViewName: ‘ZSFLIGHT_EXP’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Flight Information’
@VDM.viewType: #BASIC
define view Zflight_Exp_View
// Expicit Name List
(Code, AirName, FDate, Fare,Curr, Cat, Max_seat, Ava_seat)
as select from sflight{
key sflight.carrid,
key sflight.connid,
key sflight.fldate,
sflight.price,
sflight.currency,
sflight.planetype,
sflight.seatsmax,
sflight.seatsocc
}
The output list column names same as what is mentioned in the explicit name list.
Even we have another way of explicitly defining column names by using ‘as expcolumnname, in the selection list.
@AbapCatalog.sqlViewName: ‘ZSFLIGHT_EXP’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Flight Information’
@VDM.viewType: #BASIC
define view Zflight_Exp_View
// Expicit Name List
// (Code, AirName, FDate, Fare,Curr, Cat, Max_seat, Ava_seat)
as select from sflight{
key sflight.carrid as Code,
key sflight.connid as AirName,
key sflight.fldate as FDate,
sflight.price as Fare,
sflight.currency as Curr,
sflight.planetype as Cat,
sflight.seatsmax as MaxSeat,
sflight.seatsocc as AvaSeat
}
The output list column names appear same as what is mentioned in the selection list.
Inner Join/Join
Left Outer Join
Right Outer Join
The post shows a simple Inner Join between data sources form SCARR & SPFLI table.
In HANA studio, open ABAP perspective. From Project explorer, right click on the package and choose New->Other
ABAP Repository Object
Provide aSQL View Name & code lines. Save & Activate.
@AbapCatalog.sqlViewName: ‘ZFLIGHT_JOIN’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Flight with Inner Join CDS View’
define view Zflight_Join_Vw
as select from spfli
join scarr
on spfli.carrid = scarr.carrid
{
key spfli.carrid,
key scarr.carrname,
key spfli.connid,
spfli.countryfr,
spfli.cityfrom,
spfli.airpfrom,
spfli.countryto,
spfli.cityto,
spfli.airpto
}
ABAP CDS on HANA-6 Case Expression in CDS View Create a CDS view and use below case expression.
@AbapCatalog.sqlViewName: ‘ZFLIGHT_VW’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: ‘Use of Case Expression in CDS view’
define view Zflight_Case_Exp
as select from spfli {
key spfli.carrid,
key spfli.connid,
spfli.countryfr,
spfli.countryto,
case
when distance >= 10000 then ‘Long Way Journey’
when distance >= 5000 and distance < 10000 then ‘Medium Way Journey’
else ‘Short Way journey’
end as journey_type
}
Aggregate Functions:
@AbapCatalog.sqlViewName: ‘ZFLIGHT_VW’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: ‘Flight View with Aggregation Operation’
define view Zflight_View
as select from sflight
{
key sflight.carrid,
key sflight.connid,
sum( price ) as Total_Amount,
sflight.currency
} group by carrid, connid, currency
Data Preview:
@AbapCatalog.sqlViewName: ‘ZFLIGHT_VW’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: ‘Flight View with Aggregation Operation’
define view Zflight_View
as select from sflight
{
key sflight.carrid,
key sflight.connid,
sum( price ) as Total_Amount,
sflight.currency,
count( *) as Lines
} group by carrid, connid, currency
Data Preview
CDS View-
@AbapCatalog.sqlViewName: ‘ZFLIGHT_VW’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: ‘Flight View with Aggregation Operation’
define view Zflight_View
as select from sflight as s
{
key s.carrid,
key s.connid,
key s.fldate,
s.price,
s.currency,
s.seatsmax – s.seatsocc as Avai_Seats_in_Economy_Class,
s.seatsmax_b – s.seatsocc_b as Avai_Seats_in_Business_Class,
s.seatsmax_f – s.seatsocc_f as Avai_Seats_in_First_Class
}
Data Preview
The CDS view with currency_conversion function, pass the amount to be converted, source currency, target currency and the
exchange rate date.
@AbapCatalog.sqlViewName: ‘ZFLIGHT_VW’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: ‘Flight View with Aggregation Operation’
define view Zflight_View
with parameters p_curr: abap.cuky(5),
p_dat:abap.dats
as select from sflight as s
{
key s.carrid,
key s.connid,
key s.fldate,
currency_conversion( amount => s.price,
source_currency => s.currency,
target_currency => :p_curr,
exchange_rate_date => :p_dat ) as local_amount,
:p_curr as local_currency
} where carrid = ‘AA’
Data Preview: provide the target currency and the date.
Here is the amount in the target currency.
Numeric Functions
ABS(arg)
CEIL(arg)
DIV(arg1, arg2)
FLOOR(arg)
MOD(arg1, arg2)
ROUND(arg, pos)
String Functions
CONCAT(arg1, arg2)
@AbapCatalog.sqlViewName: ‘ZFLIGHT_VW’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: ‘Demo View SQL Functions’
define view Zflight_View
as select from spfli as s
{
key s.carrid,
key s.connid,
concat_with_space(s.cityfrom, s.cityto, 4 ) as City_From_To
}
Data Preview
ABAP CDS on HANA-11
@AbapCatalog.sqlViewName: ‘ZFLIGHT_AVW’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: ‘Flight CDS View with association’
define view Zflight_Asso_Vw
as select from spfli
association [1..1] to scarr as _scarr
on $projection.carrid = _scarr.carrid
{
spfli.carrid,
spfli.connid,
spfli.countryfr,
spfli.cityfrom,
spfli.airpfrom,
spfli.countryto,
spfli.cityto,
spfli.airpto,
/* Associations */
_scarr
}
Data Preview- No particular field of _SCARR added to the select . So in data preview only fields of spfli table are shown.
To add particular field of association _scarr, it can be added by the alias name and dot followed by field name
Data Preview.
Create a CDS view and we have the view type as ‘BASIC’ view
To publish this as oData, add the annotation as: @OData.publish: true
@AbapCatalog.sqlViewName: ‘ZFLIGHT_VW’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Flight View CDS’
@VDM.viewType: #BASIC
@OData.publish: true
define view Zflight_View
as select from sflight
{
key sflight.carrid,
key sflight.connid,
key sflight.fldate,
sflight.price,
sflight.currency,
sflight.planetype,
sflight.seatsmax,
sflight.seatsocc
}
Activate the view. Then a symbol would appear at the line: @OData.publish: true, whihc tells that a service
‘ZFLIGHT_VIEW_CDS’ created and we have to add this service on Tx- /IWFND/MAINT_SERVICE
Go to the Tx- /IWFND/MAINT_SERVICE, click on Add Service button
Provide the alias name, and the service name and hit enter key.
The created service will appear in the list. Select the service and click on Add Selected Service.
Select local object and continue.
In the hana studio, activate the the view and a new symbol appears at the line’@OData.publish: true’ . Put the cursor on the
mark
To execute the view and get the details, change the url sap/opu/odata/sap/ZFLIGHT_VIEW_CDS/Zflight_View
It returns all the entries.
ABAP CDS on HANA-13
Different CDS views types are allowed to create and those are :-
1. Basic– Views that form the core data basis without data redundancies.
2. Composite– Views that provide data derived and/or composed from the BASIC views.
3. Consumption– Views that serve for specific application purposes and may be defined based upon public interface (for
example, BASIC and COMPOSITE) views.
4. Extension– A HANA view can be used as abap CDS view called Extension view.
A CDS interface view cam be made as Public or private. Private interface (for example, BASIC & COMPOSITE) views
represent technical helper views which may only be used by their defining responsibilities.
CDS Annotations: A CDS annotation adds metadata to the view that expands the syntax options of SQL.
1. Core annotations – It can be specified in the source code of any ABAP CDS object & are evaluated by run time environment.
2. UI annotations -It allows to focus OData UI vocabulary on usage patterns of data in UIs representing certain semantic views
of data. The UI annotations used are evaluated by the SADL framework, which means that SADL translates CDS annotations
into the corresponding OData annotations.
3. Search annotations – It allows to define search fields as filters . The annotations used are also evaluated by the SADL
framework.
4. Analytics annotations – It provides data elements with aggregation behavior in analytical scenarios.
An example of – a Public BASIC CDS Interface View
@VDM.viewType: #BASIC
define view Flight ... { key Carrid, ... }
@VDM.private: true
@VDM.viewType: #COMPOSITE
define view FlightDetails ... { key Carrid, ... }
ABAP CDS on HANA-14
@AbapCatalog.sqlViewName: ‘ZFLIGHTVW1’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Basic Airline View’
@VDM.viewType: #BASIC
@Analytics.dataCategory: #DIMENSION
define view Zflight_View1 as select from scarr
{
key scarr.carrid as AirlineID,
scarr.carrname as AirlineName,
@Semantics.currencyCode: true
scarr.currcode as AirlineCurrency
}
Data Preview
Creation of Consumption view on basic view- which is oData publish enabled
@AbapCatalog.sqlViewName: ‘ZFLIGHTVW2’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Consumption Flight View’
@VDM.viewType: #CONSUMPTION
@OData.publish: true
define view Zflight_View2 as select from Zflight_View1
{
key Zflight_View1.AirlineID,
Zflight_View1.AirlineName,
Zflight_View1.AirlineCurrency
}
Data Preview
ABAP CDS on HANA-15
This post describes creating several BASIC views & then creating a COMPOSITE view by using the BASIC views & finally
creating a CONSUMPTION view from the COMPOSITE view which is enabled for analytic query.
1. Creating 3 BASIC VIEWs
2. Creating 1 COMPOSITE VIEW
3. Creating 1 CONSUMPTION VIEW which is enabled for analytics
Basic View 1 on SCARR table- Its a DIMENSION type as it hits the DB table directly
@AbapCatalog.sqlViewName: ‘ZVWSCARR’
@ClientDependent: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Airlline’
@VDM.viewType: #BASIC
@Analytics.dataCategory: #DIMENSION
@Analytics.dataExtraction.enabled: true
@ObjectModel.representativeKey: ‘Airline’
define view ZDemo_Scarr as select from scarr
{
key scarr.carrid as Airline,
@Semantics.text: true
scarr.carrname as AirName,
@Semantics.currencyCode: true
scarr.currcode as AirCurrency,
scarr.url as AirlineUrl
}
Data Preview
Basic View 2 on SPFLI table- Its a DIMENSION type as it hits the DB table directly with association to the basic view
ZDemo_Scarr.
@AbapCatalog.sqlViewName: ‘ZVWSPFLI’
@ClientDependent: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Airline Connection’
@Analytics.dataCategory: #DIMENSION
@Analytics.dataExtraction.enabled: true
@VDM.viewType: #BASIC
@ObjectModel.representativeKey: ‘AirConnection’
define view ZDemo_Spfli as select from spfli
association [0..1] to ZDemo_Scarr as _Scarr
on $projection.Airline = _Scarr.Airline
{
@ObjectModel.foreignKey.association: ‘_Scarr’
key spfli.carrid as Airline,
key spfli.connid as AirConnection,
spfli.countryfr as DepartureCountry,
spfli.countryto as ArrivalCountry,
@Semantics.quantity.unitOfMeasure: ‘AirDistanceUnit’
@DefaultAggregation:#SUM
spfli.distance as AirDistance,
@Semantics.unitOfMeasure: true
spfli.distid as AirDistanceUnit,
_Scarr
}
Data Preview
Basic View 3 on SFLIGHT table- with association to view ZDemo_Scarr and view ZDemo_Spfli. Several semantics are
used for different fields on aggregation and currency & unit.
@AbapCatalog.sqlViewName: ‘ZVWSFLIGHT’
@ClientDependent: true
@AccessControl.authorizationCheck:#NOT_REQUIRED
@EndUserText.label: ‘Airline Schedule’
@Analytics.dataCategory: #CUBE
@Analytics.dataExtraction.enabled: true
@VDM.viewType: #BASIC
define view ZDemo_Sflight as select from sflight
association [0..1] to ZDemo_Scarr as _Scarr
on $projection.Airline = _Scarr.Airline
Data Preview
All fields of scarr, splfi, sflight are available in the view ZDemo_Sflight due to the association with views ZDemo_Scarr
& ZDemo_Spfli
Now next step is to build a composite view on the basic view ZDemo_Sflight and select as many fields from the
view ZDemo_Sflight so that this composite view can expose many fields to the consumption views for analytic analysis.
@AbapCatalog.sqlViewName: ‘ZVWCSFLIGHT’
@ClientDependent: true
@AccessControl.authorizationCheck:#NOT_REQUIRED
@EndUserText.label: ‘Airline Schedule by Country’
@Analytics.dataCategory: #CUBE
@VDM.viewType: #COMPOSITE
define view ZDemo_Csflight as select from ZDemo_Sflight
{
@ObjectModel.foreignKey.association: ‘_Scarr’
key ZDemo_Sflight.Airline,
@ObjectModel.foreignKey.association: ‘_Spfli’
key ZDemo_Sflight.AirConnection,
key ZDemo_Sflight.FlightDate,
@Semantics.amount.currencyCode: ‘FlightCurrency’
@DefaultAggregation:#MIN
ZDemo_Sflight.FlightPrice,
@Semantics.currencyCode: true
ZDemo_Sflight.FlightCurrency,
@DefaultAggregation: #SUM
ZDemo_Sflight.MaximumAvaSeats,
@DefaultAggregation: #SUM
ZDemo_Sflight.NumberOfAvaSeats,
/* Associations */
ZDemo_Sflight._Scarr,
ZDemo_Sflight._Spfli,
ZDemo_Sflight._Scarr.AirName,
ZDemo_Sflight._Scarr.AirlineUrl,
ZDemo_Sflight._Spfli.DepartureCountry,
ZDemo_Sflight._Spfli.ArrivalCountry
}
Data Preview
Next step is to create a CONSUMPTION View from the COMPOSITE view- ZDemo_Csflight and enabling it for analytics.
In the analytics view fields are marked as rows or columns, other fields can be left .
Here the filter semantics provided for the field ‘Departure Country’ and with mandatory option means when we this view is
analyzed with analytic engine, we have to pass the departure country value for selection.
@AbapCatalog.sqlViewName: ‘ZVWSFLIGHTQUERY’
@ClientDependent: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Flight Analytic Query’
@Analytics.query: true
@VDM.viewType: #CONSUMPTION
@OData.publish: true
define view ZDemo_Sflight_Query as select from ZDemo_Csflight
{
@AnalyticsDetails.query.axis: #ROWS
ZDemo_Csflight.Airline,
@AnalyticsDetails.query.axis: #ROWS
ZDemo_Csflight.AirConnection,
@AnalyticsDetails.query.axis: #ROWS
ZDemo_Csflight.FlightDate,
@Consumption.filter: {selectionType: #SINGLE, multipleSelections: false, mandatory: true }
@AnalyticsDetails.query.axis: #ROWS
@EndUserText.label: ‘Departure Country’
ZDemo_Csflight.DepartureCountry,
@AnalyticsDetails.query.axis: #ROWS
@EndUserText.label: ‘Arrival Country’
ZDemo_Csflight.ArrivalCountry ,
@AnalyticsDetails.query.axis: #COLUMNS
ZDemo_Csflight.FlightPrice,
ZDemo_Csflight.FlightCurrency,
ZDemo_Csflight.MaximumAvaSeats,
ZDemo_Csflight.NumberOfAvaSeats
}
Data Preview
Next thing is to test the Consumption View in the analytic tool. In a BW system you can find the Tx- RSRT
Here we have to provide for consumption sql view name which is enabled for analytic query and provide 2C before it and hit
enter key.
Now the actual query is on the composite view used in the consumption view. Execute.
As we have made Departure Country as a mandatory filter option in the consumption view, it asks for a value. Provide a
value & Execute.
The list of values appear with 131 records. Let’s further filter the selection for Airline(CARRID) & Connection
Number(CONNID). Select the filter button for each.
Now with the filters we have few records now select Graphical Display.
A graphical view is displayed.
ABAP CDS on HANA-16
Create a CDS view on the table SCARR & add all columns in the selection marking the key field.
Data preview. So all the seelcted fields of SPFLI appear in the preview. Now to see the association, select a line and from the
header arrow you can navigate to the association.
Now lets create a new CDS view on the table SFLIGHT with selected fields and marking the key fields.
Data Preview.
Now let’s use the view Ztest_Sflight in the already created view Ztest_Spfli as an association with cardinality as [0..*]. No
fields selected from the association Ztest_Sflight but the association alias name itself added in the selection so that it can be
used in some other view to added fields from the association using path expression.
Do a data preview. Select one line and right click, chose Follow Association.
Here we have two association one with cardinality [0..1] for Ztest_Scarr and second with cardinality [0..*] for Ztest_Sflight.
Choose the _Sflight association.
All the matching records with key carrid- AA & connid- 0017 displayed in the data preview from the association
Ztest_Sflight.