0% found this document useful (0 votes)
23 views15 pages

CDS View

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views15 pages

CDS View

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Difference between ABAP CDS AND HANA CDS views

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 *//


header
}

where currency_code = 'USD'

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.

Associations are defined with ‘Cardinality’. Syntax : association[<cardinality>]


Let us check how an AMDP Class and Method looks in the real scenario.
Looking at the Class DEFINITION, we can guess that the method “get_po_data”
can be an AMDP method as it meets the pre-requisite of passing all parameters
by VALUE. But, just by looking the definition, we cannot say for sure if it really is
an AMDP method. However, we can say for sure that the second method
“display_po_data” is NOT an AMDP method as it does not meet the basic
requirement of passing by VALUE.

To confirm, if the method “get_po_data” is really an AMDP method, we need to


look at the IMPLEMENTATION. In the implementation, if you find the keyword “BY
DATABASE PROCEDURE”, it is AMDP method.

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

READ-ONLY CDS SESSION CLIENT iv_client

IMPORTING
VALUE(iv_client) TYPE sy-mandt

" Client

VALUE(it_material) TYPE tt_material

" List of Materials

VALUE(iv_uom) TYPE /bsk/qtyunit

" Unit of MeASure Input

VALUE(iv_start_date) TYPE dats

" Start Date of 'After N Day'

VALUE(iv_end_date) TYPE dats

" End Date of 'After N Day'

VALUE(iv_start_date_n) TYPE dats

VALUE(iv_end_date_n) TYPE dats

VALUE(iv_only_parent) TYPE abap_bool

" Flag for only parent material

VALUE(it_plant_sloc) TYPE

/bsk/tt_plant_sloc_loc " List of Plant & Storage Location with

Location Type

VALUE(it_element_data) TYPE

/bsk/tt_element_data
VALUE(iv_reportind) TYPE /bsk/reportind

" Report Indicator

EXPORTING

VALUE(et_element_data) TYPE

/bsk/tt_element_data " Buffer Data

RAISING cx_amdp_error.

* Data declarations

DECLARE lv_date NVARCHAR( 8);

DECLARE lv_lang NVARCHAR( 1);

DECLARE lt_stock table (

Client NVARCHAR( 3),

MATERIAL NVARCHAR(40),

PLANT NVARCHAR(4) ,

STORAGELOCATION NVARCHAR(4),

BATCH NVARCHAR(10),

BASEUNIT NVARCHAR(3),

STOCKQUANTITY DECIMAL(13,3),

deliverydate NVARCHAR( 8),

CompanyCode NVARCHAR( 4)

);

Session variable in CDS view


$session.user as system_user,
$session.client as system_client,
$session.system_language as system_language,
$session.system_date as system_date

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

Search.searchable Defines if a CDS view or entity is generally relevant for


search scenarios. This annotation must be set in case
other search-related annotations are being defined for
elements of the respective CDS view or entity. The
annotation offers a general switch and a means to
quickly detect whether a view is relevant or not.

Scope: #View

Evaluation Runtime (Engine): Interpreted by Enterprise


Search and SADL

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.

Usually, such a search must not operate on all elements


– for performance reasons, and because not all elements
(e.g. internal keys) do qualify for this kind of access.

Scope: #Element

Evaluation Runtime (Engine): Interpreted by Enterprise


Search and SADL

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

Evaluation Runtime (Engine) : Interpreted by


Enterprise Search

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/

Handling of SELECT-OPTIONS parameters within AMDP

Simply explained, the handling of SELECT-OPTIONS parameters in the context


of AMDPs requires two steps:

1. Conversion of the selection tables into an SQL WHERE clause using


method CL_SHDB_SELTAB=>COMBINE_SELTABS( )
2. Handling of dynamic WHERE clauses within the AMDP method using the
function APPLY_FILTER

Step 1: Conversion of SELECT-OPTIONS parameters into an


SQL WHERE clause

The new class CL_SHDB_SELTAB - especially its static


method COMBINE_SELTABS
As shown above, you just have to pass an internal table (defined here using
the new value operator VALUE) filled with as many SELECT-
OPTIONS parameters as required by your scenario. The name of the relevant
field (NAME) and of the data reference to the corresponding SELECT-
OPTIONS table (DREF) is required for each entry. In case of relevance, it is
recommended to specify the exporting
parameter IV_CLIENT_FIELD with 'CLIENT' or 'MANDT' (depending on the
related table field name) to ensure the addition of the client filter to
the WHERE clause.

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.

Step 2: Handling of dynamic WHERE clauses within the AMDP


method

What needs to be done is very simple: The SQLScript


statement APPLY_FILTER is used to apply the selection criteria to the
relevant dataset which can be a database table/view, a HANA view (except
Analytical view) or an intermediate table variable.

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

Exception handling in CDS view - @Semantics.errorHandling annotation

@Semantics.errorHandling: {

errorAggregationMode: #COUNT,

errorAggregationThreshold: 5

Example:

@AbapCatalog.sqlViewName: 'ZCDS_ERROR_EXAMPLE'

@AccessControl.authorizationCheck: #NOT_REQUIRED

@EndUserText.label: 'Example CDS View with Error Handling'

define view ZExample_CDS_ErrorHandling

as select from SFLIGHT

key SFLIGHT.CARRID as CarrierID,

key SFLIGHT.CONNID as ConnectionID,

SFLIGHT.FLIGHTDATE as FlightDate,

@Semantics.errorHandling.errorAggregationMode: #SUM

@Semantics.errorHandling.errorAggregationThreshold: 10

count( * ) as ErrorCount

group by SFLIGHT.CARRID, SFLIGHT.CONNID, SFLIGHT.FLIGHTDATE;

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.

The example CDSs are-

You might also like