You are on page 1of 18

ABAP Development for SAP HANA

https://open.sap.com/courses/a4h1

Source Code of Week 3

Version 1 - 15.10.2014

Only Demo content


Without any warranty
Contents
CDS DDL Sources ........................................................................................................................................ 3
ZDDLS_CDS_00_SIMPLE ............................................................................................................................ 3
ZDDLS_CDS_10_AGG_AND_LIT ................................................................................................................ 3
ZDDLS_CDS_11_ARITH_AND_CAST ......................................................................................................... 3
ZDDLS_CDS_12_CONDITIONAL_EXP ....................................................................................................... 4
ZDDLS_CDS_13A_BASE ............................................................................................................................. 4
ZDDLS_CDS_13B_VIEW_ON_VIEW ........................................................................................................... 5
ZDDLS_CDS_13C_VIEW_EXTENSION ....................................................................................................... 5
ZDDLS_CDS_14A_INPUT_PARAMETERS ................................................................................................. 5
ZDDLS_CDS_14B_CONSUMPTION ............................................................................................................ 6
ZDDLS_CDS_20_UNION .............................................................................................................................. 6
ZDDLS_CDS_21_JOIN ................................................................................................................................. 6
ZDDLS_CDS_30_ASSOC_SIMPLE ............................................................................................................. 7
ZDDLS_CDS_31A_ASSOC_TYPES ............................................................................................................ 7
ZDDLS_CDS_31B_ASSOC_TYPES ............................................................................................................ 8
ZDDLS_CDS_32A_ASSOC_FILTER ............................................................................................................ 8
ZDDLS_CDS_32B_ASSOC_FILTER ............................................................................................................ 8
ZDDLS_CDS_33A_ADV_ASSOC_FILTER .................................................................................................. 9
ZDDLS_CDS_33B_ADV_ASSOC_FILTER .................................................................................................. 9
ZDDLS_CDS_33C_ADV_ASSOC_FILTER .................................................................................................. 9
ABAP Reports ............................................................................................................................................ 11
ZR_CDS_00_CONSUMPTION ................................................................................................................... 11
ZR_CDS_01_CONSUMPTION_VWP ......................................................................................................... 11
ZR_OPENSQL_00_SYNTAX ...................................................................................................................... 12
ZR_OPENSQL_01_AGGREGATION .......................................................................................................... 12
ZR_OPENSQL_02_LITERALS ................................................................................................................... 13
ZR_OPENSQL_03_GENERIC_EXIST........................................................................................................ 13
ZR_OPENSQL_04_ARITH_EXPRESS ....................................................................................................... 13
ZR_OPENSQL_05_CONCAT ..................................................................................................................... 14
ZR_OPENSQL_10_CASE ........................................................................................................................... 14
ZR_OPENSQL_11_COALESCE ................................................................................................................. 15
ZR_OPENSQL_12_GROUP_BY ................................................................................................................ 15
ZR_OPENSQL_13_HAVING ....................................................................................................................... 16
ZR_OPENSQL_14_JOIN ............................................................................................................................ 16
ZR_OPENSQL_15_CLIENT_HANDLING ................................................................................................... 16

2
CDS DDL SOURCES
ZDDLS_CDS_00_SIMPLE
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_00'
define view ZCDSV_SIMPLE as select from snwd_so {

snwd_so.so_id as order_id,
snwd_so.gross_amount,
snwd_so.currency_code

}
ZDDLS_CDS_10_AGG_AND_LIT
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_10'
define view zcdsv_aggregations
as select from snwd_so as so
inner join snwd_bpa as bpa
on so.buyer_guid = bpa.node_key
{
key bpa.bp_id as customer_id,
bpa.company_name,
'Literal' as string_literal,
42 as integer_literal,
so.currency_code,
sum( so.gross_amount ) as total_gross_amount
}
group by
bpa.bp_id,
bpa.company_name,
so.currency_code
having sum( so.gross_amount ) > 1000
ZDDLS_CDS_11_ARITH_AND_CAST
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_11'
define view zcdsv_arithmetics
as select from snwd_so as so
inner join snwd_bpa as bpa
on so.buyer_guid = bpa.node_key
{
key bpa.bp_id as customer_id,
bpa.company_name,
so.currency_code,
( so.gross_amount - so.net_amount ) as tax_amount,
0.85 * cast( so.gross_amount as abap.fltp )
as reduced_gross_amount

3
}
ZDDLS_CDS_12_CONDITIONAL_EXP
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_12'
define view zcdsv_cond_exp
as select from snwd_so as so
left outer join snwd_so_inv_head as inv_head
on so.node_key = inv_head.so_guid
{
key so.so_id,
so.currency_code,
so.gross_amount,
case delivery_status
when ' ' then 'OPEN'
when 'D' then 'DELIVERED'
else delivery_status
end as delivery_status_long,

case
when so.gross_amount > 1000
then 'High Volume Sales Order'
else ' '
end as high_volumne_text,

coalesce( inv_head.payment_status, 'Not yet invoiced' ) as


payment_status
}

@AbapCatalog.sqlViewName: 'ZDDLS_CDS_13A'
define view zcdsv_base as select
from snwd_so as so
{
key so.so_id as order_id,
so.buyer_guid,
so.currency_code,
so.gross_amount
}
ZDDLS_CDS_13A_BASE
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_13A'
define view zcdsv_base as select
from snwd_so as so
{
key so.so_id as order_id,
so.buyer_guid,

4
so.currency_code,
so.gross_amount
}
ZDDLS_CDS_13B_VIEW_ON_VIEW
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_13B'
define view zcdsv_view_on_view as select
from zcdsv_base
inner join snwd_bpa as bpa
on bpa.node_key = zcdsv_base.buyer_guid
{
key bpa.bp_id,
bpa.company_name,
zcdsv_base.currency_code,
zcdsv_base.gross_amount
}
ZDDLS_CDS_13C_VIEW_EXTENSION
@AbapCatalog.sqlViewAppendName: 'ZDDLS_CDS_13C'
extend view zcdsv_base with zcdsv_customer_extension
{
so.delivery_status,
so.billing_status,
so.created_at,
so.created_by
}
ZDDLS_CDS_14A_INPUT_PARAMETERS
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_14A'
define view zcdsv_with_input_parameters
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

5
ZDDLS_CDS_14B_CONSUMPTION
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_14B'
define view zcdsv_consume_param_view
as select from zcdsv_with_input_parameters( customer_name :
'SAP' ) as vwp
{
vwp.param_customer_name
}
ZDDLS_CDS_20_UNION
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_20'
define view zcdsv_union as
select from snwd_so as so
inner join snwd_bpa as bpa
on so.buyer_guid = bpa.node_key
{
key bpa.bp_id,
bpa.company_name,
sum( gross_amount ) as total_gross_amount,
'small' as category
}
group by bpa.bp_id, bpa.company_name
having sum( gross_amount ) < 10000000

union all

select from snwd_so as so


inner join snwd_bpa as bpa
on so.buyer_guid = bpa.node_key
{
key bpa.bp_id,
bpa.company_name,
sum( gross_amount ) as total_gross_amount,
'large' as category
}
group by bpa.bp_id, bpa.company_name
having sum( gross_amount ) >= 10000000
ZDDLS_CDS_21_JOIN
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_21'
define view zcdsv_join as
select from snwd_so as so
inner join snwd_bpa as bpa
on so.buyer_guid = bpa.node_key

6
left outer join snwd_so_inv_head as inv_head
on so.node_key = inv_head.so_guid
{
key so.so_id,
bpa.company_name,
so.delivery_status,
inv_head.payment_status
}
ZDDLS_CDS_30_ASSOC_SIMPLE
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_30'
define view zcdsv_simple_assoc_examples as
select from snwd_so as so

association [1] to snwd_bpa as business_partners


on so.buyer_guid = business_partners.node_key

-- left outer join snwd_so_inv_head as inv_head


-- on so.node_key = inv_head.so_guid
association [0..1] to snwd_so_inv_head as invoice_headers
on so.node_key = invoice_headers.so_guid
{
key so.so_id as order_id,
so.delivery_status,
invoice_headers.payment_status,
invoice_headers.currency_code,
sum( invoice_headers.gross_amount )
as total_gross_amount
}
where business_partners.company_name = 'SAP'
group by so.so_id,
so.delivery_status,
invoice_headers.payment_status,
invoice_headers.currency_code
having sum( invoice_headers.gross_amount ) > 3000

ZDDLS_CDS_31A_ASSOC_TYPES
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_31A'
define view zcdsv_assoc_types as
select from snwd_so as so
association [1] to snwd_bpa as business_partners
on so.buyer_guid = $projection.buyer_guid
association [0..1] to snwd_so_inv_head as invoice_headers

7
on so.buyer_guid = invoice_headers.so_guid
{
key so.so_id as order_id,
so.delivery_status,
-- ad-hoc association
invoice_headers.payment_status,

--exposed association
-- field used in the ON condition
so.buyer_guid,
-- exposing association business_partners
business_partners
}
ZDDLS_CDS_31B_ASSOC_TYPES
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_31B'
define view zcdsv_assoc_types_consumption as
select from zcdsv_assoc_types
{
zcdsv_assoc_types.business_partners.bp_id
}
ZDDLS_CDS_32A_ASSOC_FILTER
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_32A'
define view zcdsv_filter_example_base as
select from snwd_so_inv_head as invoice_header
association[1..*] to snwd_so_inv_item as invoice_items
on $projection.header_guid = invoice_items.parent_key
{
invoice_header.so_guid as order_guid,
invoice_header.node_key as header_guid,
invoice_items
}
ZDDLS_CDS_32B_ASSOC_FILTER
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_32B'
define view zcdsv_filter_example_vov as
select from snwd_so as so
association [1] to snwd_bpa as business_partners
on so.buyer_guid = business_partners.node_key
association [0..1] to zcdsv_filter_example_base as
invoice_headers
on so.node_key = invoice_headers.order_guid
{
key so.so_id as order_id,

8
-- value 01 means customer
business_partners[ bp_role = '01' ].company_name as
customer_name,

-- filter 1..n association on first position


invoice_headers.invoice_items[1: inv_item_pos =
'0000000010'].currency_code,
invoice_headers.invoice_items[1: inv_item_pos =
'0000000010'].gross_amount
}
where invoice_headers.header_guid is not null
ZDDLS_CDS_33A_ADV_ASSOC_FILTER
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_33A'
define view zcdsv_adv_filter_example_base
as select from snwd_texts
{
parent_key as product_text_guid,
language,
text
}
ZDDLS_CDS_33B_ADV_ASSOC_FILTER
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_33B'
define view zcdsv_adv_filter_example_l1
as select from snwd_pd as pd
association [1..*] to zcdsv_adv_filter_example_base as texts
on texts.product_text_guid = $projection.text_guid
{
key pd.product_id,
pd.desc_guid as text_guid,
texts
}
ZDDLS_CDS_33C_ADV_ASSOC_FILTER
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_33C'
define view zcdsv_adv_filter_example_l2
with parameters lang : abap.lang
as select from zcdsv_adv_filter_example_l1 as product
{
$parameters.lang as langu_filter_param,
coalesce (
product.texts[1: language = $parameters.lang
].text,

9
product.texts[1: language = 'E'
].text
) as product_description
}

10
ABAP REPORTS
ZR_CDS_00_CONSUMPTION
REPORT zr_cds_00_consumption.

"data definition using a CDS view as type


DATA lt_cds TYPE STANDARD TABLE OF zcdsv_simple.

"consumption of a CDS view


SELECT * FROM zcdsv_simple INTO TABLE @lt_cds.
ZR_CDS_01_CONSUMPTION_VWP
REPORT zr_cds_01_consumption_vwp.

DATA lv_cust_name TYPE c LENGTH 80 VALUE 'SAP'.

"plenty of ABAP application logic

"check if the feature is supported by the underlying database


DATA(lv_feature_supported) =
cl_abap_dbfeatures=>use_features(
EXPORTING
requested_features = VALUE #( (
cl_abap_dbfeatures=>views_with_parameters ) )
).

"if the feature is supported, select from the CDS view and
"provide a value for the input parameter
IF lv_feature_supported = abap_true.
##DB_FEATURE_MODE[VIEWS_WITH_PARAMETERS]
SELECT *
FROM zcdsv_with_input_parameters( customer_name = 'SAP' )
INTO TABLE @DATA(lt_result).
ELSE.
"do some alternative coding here
ENDIF.

"even more awesome application logic

"display of results
cl_demo_output=>display_data( lt_result ).

11
ZR_OPENSQL_00_SYNTAX
REPORT zr_opensql_00_syntax.

TYPES: BEGIN OF ty_so_amount,


so_id TYPE snwd_so-so_id,
currency_code TYPE snwd_so-currency_code,
gross_amount TYPE snwd_so-gross_amount,
END OF ty_so_amount.

DATA lt_so_amount TYPE STANDARD TABLE OF ty_so_amount.

"using the "old" Open SQL syntax


SELECT so_id
currency_code
gross_amount
FROM snwd_so
INTO TABLE lt_so_amount.

"using the "new" Open SQL syntax


SELECT so_id,
currency_code,
gross_amount
FROM snwd_so
INTO TABLE @lt_so_amount.

"demo of the result set type inference


SELECT so_id,
currency_code,
gross_amount,
delivery_status
FROM snwd_so
INTO TABLE @DATA(lt_result).
ZR_OPENSQL_01_AGGREGATION
REPORT zr_opensql_01_aggregation.

SELECT bp_id,
company_name,
so~currency_code,
SUM( so~gross_amount ) AS total_gross_amount
FROM snwd_so AS so
INNER JOIN snwd_bpa AS bpa
ON so~buyer_guid = bpa~node_key

12
INTO TABLE @DATA(lt_result)
GROUP BY bp_id, company_name, so~currency_code.

cl_demo_output=>display_data( value = lt_result ).


ZR_OPENSQL_02_LITERALS
REPORT zr_opensql_01_aggregation.

SELECT bp_id,
company_name,
so~currency_code,
SUM( so~gross_amount ) AS total_gross_amount
FROM snwd_so AS so
INNER JOIN snwd_bpa AS bpa
ON so~buyer_guid = bpa~node_key
INTO TABLE @DATA(lt_result)
GROUP BY bp_id, company_name, so~currency_code.

cl_demo_output=>display_data( value = lt_result ).


ZR_OPENSQL_03_GENERIC_EXIST
REPORT zr_opensql_03_generic_exist.

DATA lv_exists TYPE abap_bool VALUE abap_false.

"generic check if a sales order exists


SELECT SINGLE @abap_true
FROM snwd_so
INTO @lv_exists.

IF lv_exists = abap_true.
"do some awesome application logic
ELSE.
"no sales order exists
ENDIF.
ZR_OPENSQL_04_ARITH_EXPRESS
REPORT zr_opensql_04_arith_express.

DATA lv_discount TYPE p LENGTH 1 DECIMALS 1 VALUE '0.8'.

SELECT ( 1 + 1 ) AS two,
( @lv_discount * gross_amount ) AS red_gross_amount,
CEIL( gross_amount ) AS ceiled_gross_amount
FROM snwd_so
INTO TABLE @DATA(lt_result).

13
ZR_OPENSQL_05_CONCAT
REPORT zr_opensql_05_concat.

SELECT company_name
&& ' (' && legal_form && ')'
FROM snwd_bpa
INTO TABLE @DATA(lt_result).
ZR_OPENSQL_10_CASE
REPORT zr_opensql_10_case.

"simple case
SELECT so_id,
CASE delivery_status
WHEN ' ' THEN 'OPEN'
WHEN 'D' THEN 'DELIVERED'
ELSE delivery_status
END AS delivery_status_long
FROM snwd_so
INTO TABLE @DATA(lt_simple_case).

"searched case
SELECT so_id,
CASE WHEN gross_amount > 1000
THEN 'High volume sales order'
ELSE ' '
END AS volumn_order
FROM snwd_so
INTO TABLE @DATA(lt_searched_case).

"searched case - more advanced


DATA lv_discount TYPE p LENGTH 1 DECIMALS 1 VALUE '0.8'.
SELECT so_id,
company_name,
gross_amount AS orig_amount,
CASE WHEN company_name = 'SAP'
THEN ( gross_amount * @lv_discount )
ELSE gross_amount
END AS discount_amount
FROM snwd_so AS so
INNER JOIN snwd_bpa AS bpa
ON so~buyer_guid = bpa~node_key

14
INTO TABLE @DATA(lt_adv_example_searched_case).
ZR_OPENSQL_11_COALESCE
REPORT zr_opensql_11_coalesce.

SELECT so_id,
so~gross_amount AS so_amount,
inv_head~gross_amount AS inv_amount,

"potential invoice amount


COALESCE( inv_head~gross_amount, so~gross_amount )
AS expected_amount

FROM snwd_so AS so
LEFT OUTER JOIN snwd_so_inv_head AS inv_head
ON inv_head~so_guid = so~node_key
INTO TABLE @DATA(lt_result).
ZR_OPENSQL_12_GROUP_BY
REPORT zr_opensql_12_group_by.

SELECT bp_id,
company_name,
so~currency_code,
SUM( so~gross_amount ) AS total_amount,
CASE
WHEN so~gross_amount < 1000
THEN 'X'
ELSE ' '
END AS low_volume_flag,
COUNT( * ) AS cnt_orders
FROM snwd_so AS so
INNER JOIN snwd_bpa AS bpa
ON bpa~node_key = so~buyer_guid
INTO TABLE @DATA(lt_result)
GROUP BY
bp_id, company_name,
so~currency_code,
CASE
WHEN so~gross_amount < 1000
THEN 'X'
ELSE ' '
END
ORDER BY company_name.

15
cl_demo_output=>display_data( value = lt_result ).
ZR_OPENSQL_13_HAVING
REPORT zr_opensql_13_having.

SELECT bp_id,
company_name,
so~currency_code,
SUM( so~gross_amount ) AS total_amount
FROM snwd_so AS so
INNER JOIN snwd_bpa AS bpa
ON bpa~node_key = so~buyer_guid
INTO TABLE @DATA(lt_result)
WHERE so~delivery_status = ' '
GROUP BY
bp_id,
company_name,
so~currency_code
HAVING SUM( so~gross_amount ) > 10000000.

cl_demo_output=>display_data( value = lt_result ).


ZR_OPENSQL_14_JOIN
REPORT zr_opensql_14_join.

SELECT
so_id,
bp_id,
gross_amount
FROM snwd_so AS so
RIGHT OUTER JOIN snwd_bpa AS bpa
ON so~buyer_guid = bpa~node_key
AND so~gross_amount > 100000
INTO TABLE @DATA(lt_result).
ZR_OPENSQL_15_CLIENT_HANDLING
REPORT zr_opensql_15_client_handling.

SELECT
bp_id,
company_name,
so~currency_code,
so~gross_amount
FROM snwd_so AS so
INNER JOIN snwd_bpa AS bpa
ON so~buyer_guid = bpa~node_key

16
"client 111 does not exist in the system
USING CLIENT '111'
INTO TABLE @DATA(lt_result).

cl_demo_output=>display_data( value = lt_result ).

17
www.sap.com

© 2014 SAP SE or an SAP affiliate company. All rights reserved.


No part of this publication may be reproduced or transmitted in any form
or for any purpose without the express permission of SAP SE or an SAP
affiliate company.
SAP and other SAP products and services mentioned herein as well as their
respective logos are trademarks or registered trademarks of SAP SE (or an
SAP affiliate company) in Germany and other countries. Please see
http://www.sap.com/corporate-en/legal/copyright/index.epx#trademark for
additional trademark information and notices. Some software products
marketed by SAP SE and its distributors contain proprietary software
components of other software vendors.
National product specifications may vary.
These materials are provided by SAP SE or an SAP affiliate company for
informational purposes only, without representation or warranty of any kind,
and SAP SE or its affiliated companies shall not be liable for errors or
omissions with respect to the materials. The only warranties for SAP SE or
SAP affiliate company products and services are those that are set forth in
the express warranty statements accompanying such products and services,
if any. Nothing herein should be construed as constituting an additional
warranty.
In particular, SAP SE or its affiliated companies have no obligation to pursue
any course of business outlined in this document or any related presentation,
or to develop or release any functionality mentioned therein. This document,
or any related presentation, and SAP SE’s or its affiliated companies’
strategy and possible future developments, products, and/or platform
directions and functionality are all subject to change and may be changed by
SAP SE or its affiliated companies at any time for any reason without notice.
The information in this document is not a commitment, promise, or legal
obligation to deliver any material, code, or functionality. All forward-looking
statements are subject to various risks and uncertainties that could cause
actual results to differ materially from expectations. Readers are cautioned
not to place undue reliance on these forward-looking statements, which
speak only as of their dates, and they should not be relied upon in making
purchasing decisions.

You might also like