CDS View
CDS View
SAP CDS views are optimized for data processing in the ABAP layer, making them faster and
more efficient than HANA views. This is because CDS views are processed within the ABAP
stack, while HANA views are processed within the database layer.
HANA CDS (Core data services) is a layer above “pure database” in order to define
semantically-enriched data models. In contrast to HANA modelling views a HANA
CDS document can define tables (called entities), views, table types, associations and
annotations.
Example
@AbapCatalog.sqlViewName: 'ZITEMS_XXX'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS View for "Use-cds-view" tutorial'
define view Z_Invoice_Items_2
as select from sepm_sddl_so_invoice_item
{
header.buyer.company_name,
sepm_sddl_so_invoice_item.sales_order_invoice_key,
sepm_sddl_so_invoice_item.currency_code,
sepm_sddl_so_invoice_item.gross_amount,
case header.payment_status
when 'P' then 'X'
else ' '
end
as payment_status,
ASSOCIATIONS are kind of Joins to fetch data from multiple tables on Join Conditions
but these are ‘JOINS ON-DEMAND’ i.e. they will only be triggered when user would
access the required data which needs the Association of tables. For example, your
CDS view has 4 Associations configured and user is fetching data for only 2 tables,
the ASSOICATION on other 2 tables will not be triggered and the system would
return the results quickly, so it enables really high turn-around time as compared to
regular SQL JOINS.
Look the figure below for more clarity on what we spoke above.
First, they must ensure that the class implements the interface
IF_AMDP_MARKER_HDB. Implementing this interface does not add
any interface methods, but simply flags the code as an AMDP class.
CLASS-METHODS calc_total_stock_act_amdp AMDP OPTIONS
IMPORTING
VALUE(iv_client) TYPE sy-mandt
" Client
VALUE(it_plant_sloc) TYPE
Location Type
VALUE(it_element_data) TYPE
/bsk/tt_element_data
VALUE(iv_reportind) TYPE /bsk/reportind
EXPORTING
VALUE(et_element_data) TYPE
RAISING cx_amdp_error.
* Data declarations
MATERIAL NVARCHAR(40),
PLANT NVARCHAR(4) ,
STORAGELOCATION NVARCHAR(4),
BATCH NVARCHAR(10),
BASEUNIT NVARCHAR(3),
STOCKQUANTITY DECIMAL(13,3),
CompanyCode NVARCHAR( 4)
);
custom annotations
Checkbox/ radiobutton in cds view
Annotations
https://blogs.sap.com/2014/12/04/enabling-search-in-sap-hana-using-cds/
@OData.publish: true
@Search.searchable: true
@Semantics.currencyCode
cast ('USD' as abap.cuky) as CurrencyCode,
@Semantics.amount.currencyCode: 'CurrencyCode'
zsflight.price as price,
Annotation Meaning
Scope: #View
Values:
Value Description
Boolean (true,
Defines whether a view is relevant for
Annotation Meaning
false)
search or not.
Default: true
Search.defaultSearchElemen
t Specifies that the element is to be considered in a
freestyle search (for example a SELECT…) where no
columns are specified.
Scope: #Element
Values:
Value Description
Boolean (true, Defines weather the element is to be
false) considered in a freestyle search.
Default: true
Search.ranking
Specifies how relevant the values of an element are for
ranking, if the freestyle search terms match the element
value.
Scope: #Element
Values:
Value Description
HIGH The element is of high relevancy; this
holds usually for ID and their
descriptions.
Annotation Meaning
MEDIUM The element is of medium relevancy;
this holds usually for other, important
element. This is the default.
LOW Although the element is relevant for
freestyle search, a hit in this element
has no real significance for a result
item's ranking.
Search.fuzzinessThreshold
Specifies the least level of fuzziness (with regard to
some comparison criteria passed at runtime) the element
has to have to be considered in a fuzzy search at all.
AMDP: http://www.sapyard.com/abap-on-sap-hana-part-ix-amdp-abap-managed-database-procedure/
The method returns the dynamic WHERE condition as a string which can then
be passed to the AMDP method. Let’s now go to the next step.
Below you can see a code simple showing how to apply the
dynamic WHERE clause in both cases; directly on a data source (table or view)
[CASE 1] or on an intermediate dataset (table variable) [CASE 2].
The APPLY_FILTER function expects two parameters. The first one is the
dataset to which you want to apply the filter and the second one is the
generated WHERE clause which is passed as a string argument. Find more
information about the APPLY_FILTER function in the SAP HANA SQLScript
reference.
Summary:
1. Static method COMBINE_SELTABS( ) of the new class CL_SHDB_SELTAB shall be used for the
conversion of SELECT-OPTIONS parameters (selection tables or range tables) into an
SQL WHERE clause when running on HANA DB.
o The optional parameter IV_CLIENT should be specified with 'CLIENT' or 'MANDT') w
applicable
o This class implementation is provided for HDB (refer to SAP Note 2124672 – SMP logi
required)
o ABAP 7.4 SP08 and higher is required in order to apply the above SAP Note
2. The class CL_LIB_SELTAB and its methods are obsolete
3. Use the SQLScript function APPLY_FILTER to apply the selection criteria to the selected data
the AMDP
o The function can be applied on database tables/views, HANA views (except Analytica
views) or table variables
@Semantics.errorHandling: {
errorAggregationMode: #COUNT,
errorAggregationThreshold: 5
Example:
@AbapCatalog.sqlViewName: 'ZCDS_ERROR_EXAMPLE'
@AccessControl.authorizationCheck: #NOT_REQUIRED
SFLIGHT.FLIGHTDATE as FlightDate,
@Semantics.errorHandling.errorAggregationMode: #SUM
@Semantics.errorHandling.errorAggregationThreshold: 10
count( * ) as ErrorCount
CDS Composition
The relationship between two objects is termed as Association.
In particular when one object owns another object, then that association is called as
Composition, but when one object uses another object then that association is called as
Aggregation.
Sales Order Header and Items are associated. So when SO header is deleted then SO items
are also deleted. So it’s a composition.
Similarly SO Header and Partners are associated. But when a SO header is deleted then the
partners are not deleted. So it is an aggregation.
A CDS composition is a specialized/more strict form of CDS association which defines the
CDS entity as the parent entity of the composition target. The composition target entity is
the child entity and it must define a TO-PARENT association to its parent.
It is not required to maintain ON condition for the composition. The ON condition is derived
automatically by the composition child TO_PARENT ON condition.