Vous êtes sur la page 1sur 22

CDS Extension Hands On

By Rocky Agarwal, Sakti Mishra, Yeshwant More

nly with
o
e
lid
s
e
tl
ti
is
th
Use

Internal

an image

Agenda

Introduction

Basic Features

SP8 Features

CDS view Extension

Enhancement Without Modification


Automatic Extension

Advantage

Limitations

Hands Exercises Contd..


2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

Introduction for Core Data Services


HANA introduces a Paradigm Shift in Programming Model

UI Rendering

UI/Application Layer
( client-side)

REST (OData)

Middle Tier
Service Layer

Database

2015 SAP SE or an SAP affiliate company. All rights reserved.

HANA database

More focus on DB
SQL as the basis

Internal

Tools ABAP on Eclipse

Basic Features

Create a Basic View


@AbapCatalog.sqlViewName: 'TT_BASICVIEW_1'
define view TT_BASICVIEW as select from tcurc {
key waers,
isocd,
gdatu
}

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

Where Clause

@AbapCatalog.sqlViewName: 'TT_BASICVIEW_2'
define view TT_BASICVIEW_WHERE as select from tcurc {
key waers,
isocd,
gdatu
} where waers = 'EUR'

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

Group By and Having


@AbapCatalog.sqlViewName: 'TT_BASICVIEW_3'
define view TT_BASICVIEW_HAVING as select from tcurc {
key waers,
isocd,
gdatu
}
group by waers, isocd, gdatu
having waers like 'E%'

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

Join Conditions Create Joins


@AbapCatalog.sqlViewName: 'TT_BASICVIEW_4'
define view TT_BASICVIEW_JOIN
as select from tcurc as c left outer join tcurx as x on
c.waers = x.currkey {
c.mandt as SAPClient,
c.waers as Currency,
x.currdec as CurrencyDecimal,
c.isocd as CurrencyISOCode
}

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

SP8 Features

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

10

CASE Expressions
Simple CASE
Operates like a switch statement
Does not allow to check for NULL values
(check against NULL always results in
Unknown)

2015 SAP SE or an SAP affiliate company. All rights reserved.

Searched CASE
Operates like an if elseif construct
Evaluates a series of boolean expressions and
returns the first expression that evaluates to
true
Allows checks like IS NULL or IS NOT NULL

Internal

11

Case Statements

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

12

CASE Expressions
Simple CASE
@AbapCatalog.sqlViewName: 'ZSFIN_CASE_01'
define view zsfin_case_simple as select from snwd_so as so
{
key so.so_id as order_id,
case so.lifecycle_status
when 'N' then 'New'
when 'P' then 'In Progress'
when 'C' then 'Closed'
when 'X' then 'Cancelled'
else 'Unknown'
end as lifecycle_status

Searched CASE
@AbapCatalog.sqlViewName: 'ZSFIN_CASE_02'
define view zsfin_case_searched as select from snwd_so as so
left outer join snwd_so_inv_head as inv_head
on inv_head.so_guid = so.node_key
{
key so.so_id as order_id,
so.currency_code,
so.gross_amount,
case
when
when
when
else
end as

so.gross_amount < 1000 then 'C'


so.gross_amount > 1000 and so.gross_amount < 2000 then 'D'
so.gross_amount > 2000
then 'S'
'?'
category,

case
when inv_head.node_key is null then 'Not yet invoiced'
else
'Invoice sent'
end as invoice_info
}

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

13

Division
Division
Floating Point operands
Integer-based division
Decimal-based division

@AbapCatalog.sqlViewName: 'ZSFIN_DIV'
define view ZSFIN_DIVISION as
select from snwd_so_inv_head
{
// division by / only possible for floats
cast( gross_amount as abap.fltp ) / 2.0 as div_using_schraeger,
// INTEGER-based Division:
div( gross_amount, 2 ) as int_base_division,
// DECIMAL-based Division:
// division using function division (arg1, arg2, precision)
division( gross_amount, 2, 4 ) as decimal_based_division
}

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

14

Views with Input Parameters


View with input parameters are not
supported on all DBs!!!
DBSYS dependent
Syntax:
with parameters <name> :
<type>
Consumption
Dependent views

@AbapCatalog.sqlViewName: 'ZSFIN_PARAM_01'
define view ZSFIN_PARAMETER_01
with parameters customer_name : abap.char(80)
as select from snwd_so as so
join snwd_bpa as bpa on bpa.node_key = so.buyer_guid
{
key so.so_id as order_id,
$parameters.customer_name as param_customer_name,
case
when bpa.company_name = $parameters.customer_name then 'Found it!'
else
'Not found'
end as found_customer
}
where bpa.company_name = $parameters.customer_name

OpenSQL

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

15

Views with Input Parameters (Consumption)

@AbapCatalog.sqlViewName: 'ZSFIN_PARAM_01'
define view ZSFIN_PARAMETER_01
with parameters customer_name : abap.char(80)
as select from snwd_so as so
join snwd_bpa as bpa on bpa.node_key = so.buyer_guid
{
key so.so_id as order_id,
$parameters.customer_name as param_customer_name,
case
when bpa.company_name = $parameters.customer_name
then 'Found it!
else
'Not found'
end as found_customer
}
where bpa.company_name = $parameters.customer_name

@AbapCatalog.sqlViewName: 'ZSFIN_PARAM_02'
define view ZSFIN_PARAMETER_02
as select from ZSFIN_PARAMETER_01( customer_name : 'SAP' )
as vwp
{
vwp.param_customer_name
}

@AbapCatalog.sqlViewName: 'ZSFIN_PARAM_03'
define view ZSFIN_PARAMETER_03
as select from snwd_so_inv_head as inv_head
association to ZSFIN_PARAMETER_01 as vwp_assoc
on inv_head.buyer_guid = vwp_assoc.order_guid
{
key inv_head.node_key as invoice_guid,
inv_head.buyer_guid,
vwp_assoc
}

@AbapCatalog.sqlViewName: 'ZSFIN_PARAM_04'
define view ZSFIN_PARAMETER_04
as select from ZSFIN_PARAMETER_03 as vwp
{
vwp.vwp_assoc( customer_name : 'SAP' ).param_customer_name
}

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

16

View Extensions

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

17

View Extension
Extend existing/delivered CDS View with:
Table column
Arithmetical & CASE expressions
Literals

@AbapCatalog.sqlViewName: 'ZSFIN_BASE_VIEW'
define view ZSFIN_BASIC_VIEW
as select from snwd_bpa as bpa
left outer join snwd_so_inv_head as inv_head
on bpa.node_key = inv_head.buyer_guid
{
key bpa.bp_id as customer_id,
bpa.company_name as customer_name,
inv_head.node_key

as invoice_guid

Extension technique:
Append to base view
Not allowed on Views:
including Grouping, e.g. Aggregation
including UNIONs

@AbapCatalog.sqlViewAppendName: 'ZSFIN_EXT_01'
extend view ZSFIN_BASIC_VIEW with ZSFIN_EXTEND_01
{
inv_head.currency_code,
inv_head.gross_amount,
'X' as my_abap_true,
( 2 * inv_head.gross_amount ) as double_amount,
case inv_head.payment_status
when 'P' then 'paid'
else
'not yet paid'
end as some_case
}

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

18

Example

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

19

Limitations
Limitation of $Extension:
>.INCLUDE is not supported implicitly
>Doesnt support proxy redirect views
Limitation of extend view
Not allowed on Views:
including Grouping, e.g. Aggregation
including UNIONs

2015 SAP SE or an SAP affiliate company. All rights reserved.

Internal

20

CDS Hands On
Internal

Lets Start

Thank you

2015 SAP SE or an SAP affiliate company. All rights reserved.