Add CDS view entity example
This commit is contained in:
466
src/zcl_demo_abap_cds_ve.clas.abap
Normal file
466
src/zcl_demo_abap_cds_ve.clas.abap
Normal file
@@ -0,0 +1,466 @@
|
||||
***********************************************************************
|
||||
*
|
||||
* ABAP cheat sheet: CDS View Entities
|
||||
*
|
||||
* -------------------------- PURPOSE ----------------------------------
|
||||
* - Example to demonstrate CDS view entities. See the CDS view entities
|
||||
* that are used in the example for more details.
|
||||
* - Topics covered: Operands, expressions, and built-in functions in the
|
||||
* SELECT list of CDS view entities, input parameters, joins,
|
||||
* associations
|
||||
* - Note: In ADT, check out the CDS view entities used in this example
|
||||
* by holding down CTRL and clicking on the CDS view entity. This will
|
||||
* take you to the artifact. There you can choose F8 to open the data
|
||||
* preview.
|
||||
|
||||
* ----------------------- GETTING STARTED -----------------------------
|
||||
* - Open the class with the ABAP Development Tools (ADT).
|
||||
* - Choose F9 to run the class.
|
||||
* - Check the console output.
|
||||
* - To understand the context and the ABAP syntax used, refer to the
|
||||
* notes included in the class as comments or refer to the respective
|
||||
* topic in the ABAP Keyword Documentation.
|
||||
* - Due to the amount of console output, the examples contain numbers
|
||||
* (e.g. 1) ..., 2) ..., 3) ...) for the individual example sections.
|
||||
* Also, the variable name is displayed in most cases. So to find
|
||||
* the relevant output in the console easier and faster, just search
|
||||
* for the number/variable name in the console (CTRL+F in the console)
|
||||
* or use the debugger.
|
||||
*
|
||||
* ----------------------------- NOTE -----------------------------------
|
||||
* The code presented in this class is intended only to support the ABAP
|
||||
* cheat sheets. It is not intended for direct use in a production system
|
||||
* environment. The code examples in the ABAP cheat sheets are primarily
|
||||
* intended to provide a better explanation and visualization of the
|
||||
* syntax and semantics of ABAP statements, not to solve concrete
|
||||
* programming tasks. For production application programs, you should
|
||||
* always work out your own solution for each individual case. There is
|
||||
* no guarantee for the correctness or completeness of the code.
|
||||
* Furthermore, there is no legal responsibility or liability for any
|
||||
* errors or their consequences that may occur when using the the example
|
||||
* code.
|
||||
*
|
||||
***********************************************************************
|
||||
"! <p class="shorttext synchronized">ABAP cheat sheet: CDS view entities</p>
|
||||
"! Example to demonstrate CDS view entities.<br>Choose F9 in ADT to run the class.
|
||||
CLASS zcl_demo_abap_cds_ve DEFINITION
|
||||
PUBLIC
|
||||
FINAL
|
||||
CREATE PUBLIC .
|
||||
|
||||
PUBLIC SECTION.
|
||||
INTERFACES:
|
||||
if_oo_adt_classrun.
|
||||
|
||||
CLASS-METHODS class_constructor.
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
CLASS zcl_demo_abap_cds_ve IMPLEMENTATION.
|
||||
|
||||
METHOD class_constructor.
|
||||
"Filling demo database tables.
|
||||
zcl_demo_abap_flight_tables=>fill_dbtabs( ).
|
||||
|
||||
"Some more database table insertions for this particular example
|
||||
MODIFY zdemo_abap_carr FROM TABLE @( VALUE #(
|
||||
( carrid = 'SQ'
|
||||
carrname = 'Singapore Airlines'
|
||||
currcode = 'SGD'
|
||||
url = 'http://www.singaporeair.com ' )
|
||||
( carrid = 'QF'
|
||||
carrname = 'Qantas Airways'
|
||||
currcode = 'AUD'
|
||||
url = 'http://www.qantas.com.au' ) ) ).
|
||||
|
||||
MODIFY zdemo_abap_flsch FROM TABLE @( VALUE #(
|
||||
( carrid = 'UA'
|
||||
connid = 3517
|
||||
countryfr = 'DE'
|
||||
cityfrom = 'FRANKFURT'
|
||||
airpfrom = 'FRA'
|
||||
countryto = 'US'
|
||||
cityto = 'NEW YORK'
|
||||
airpto = 'JFK'
|
||||
fltime = 495
|
||||
deptime = '104000'
|
||||
arrtime = '125500'
|
||||
distance = 6162
|
||||
distid = 'KM'
|
||||
fltype = ''
|
||||
period = 0 ) ) ).
|
||||
|
||||
MODIFY zdemo_abap_fli FROM TABLE @( VALUE #( ( carrid = 'UA' ) ) ).
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD if_oo_adt_classrun~main.
|
||||
|
||||
DATA(output) = NEW zcl_demo_abap_display( out ).
|
||||
|
||||
output->display( `ABAP Cheat Sheet: CDS View Entities` ).
|
||||
|
||||
output->display( `1) Operands, expressions and built-in functions ` &&
|
||||
`in a CDS view entity` ).
|
||||
|
||||
"The following ABAP SQL SELECT statement uses a CDS view entity as
|
||||
"the data source. All data is retrieved. The sample CDS view entity
|
||||
"uses many operands, expressions and built-in functions to demonstrate
|
||||
"various syntax options. In addition, the view contains an input
|
||||
"parameter that must be provided with an actual parameter and
|
||||
"specified in the ABAP SQL SELECT statement.
|
||||
"In ADT, check out the CDS view entity by holding down CTRL and clicking
|
||||
"on the CDS view entity. This will take you to the artifact. There you
|
||||
"can choose F8 to open the data preview.
|
||||
|
||||
SELECT *
|
||||
FROM zdemo_abap_cds_ve_sel( p_smax = 20 )
|
||||
ORDER BY CarrierId
|
||||
INTO TABLE @DATA(select_from_cds).
|
||||
|
||||
output->display( input = select_from_cds name = `select_from_cds` ).
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `2) Aggregate Expressions` ).
|
||||
|
||||
"The following ABAP SQL SELECT statement uses a CDS view entity as
|
||||
"the data source. All data is retrieved. The sample CDS view entity
|
||||
"uses aggregate expressions.
|
||||
|
||||
SELECT *
|
||||
FROM zdemo_abap_cds_ve_agg_exp
|
||||
ORDER BY carrid
|
||||
INTO TABLE @DATA(agg_expr).
|
||||
|
||||
output->display( input = agg_expr name = `agg_expr` ).
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `3) Joins` ).
|
||||
|
||||
"The following ABAP SQL SELECT statement uses a CDS view entity as
|
||||
"the data source. All data is retrieved. The sample CDS view entity
|
||||
"contains multiple joins.
|
||||
"The CDS view entity is designed to contain different join variants
|
||||
"in one artifact. There, you can comment in/out code sections to
|
||||
"check out the individual join variants. Therefore, the result of
|
||||
"the following SELECT statement depends on which section you have
|
||||
"commented in in the CDS view entity.
|
||||
|
||||
SELECT *
|
||||
FROM zdemo_abap_cds_ve_joins
|
||||
ORDER BY carrid
|
||||
INTO TABLE @DATA(cds_joins).
|
||||
|
||||
output->display( input = cds_joins name = `cds_joins` ).
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `4) Excursion: ABAP SQL and joins` ).
|
||||
|
||||
"The following ABAP SQL SELECT statements uses a CDS view entity as
|
||||
"the data source. The sample CDS view entity contains multiple
|
||||
"joins.
|
||||
"The following ABAP SQL SELECT statements are intended to reproduce
|
||||
"the different joins that are performed by the CDS view entity.
|
||||
"Inner, left and right outer, and cross joins are covered. The data
|
||||
"sources for the SELECT statements are the database tables.
|
||||
"Note:
|
||||
"- The prefix ~ is used in the ABAP SQL statements instead of . in
|
||||
" the CDS view entity.
|
||||
"- To demonstrate the handling of null values, some SELECT statements
|
||||
"contain the coalesce function and CASE expressions similar to the
|
||||
"CDS view entity.
|
||||
|
||||
output->display( `----- Inner join -----` ).
|
||||
|
||||
SELECT _carr~carrid,
|
||||
_carr~carrname,
|
||||
_flsch_in~cityfrom AS cityfr_in,
|
||||
_flsch_in~cityto AS cityto_in
|
||||
FROM zdemo_abap_carr AS _carr
|
||||
INNER JOIN zdemo_abap_flsch AS _flsch_in
|
||||
ON _carr~carrid = _flsch_in~carrid
|
||||
ORDER BY _carr~carrid
|
||||
INTO TABLE @DATA(sql_inner_join).
|
||||
|
||||
output->display( input = sql_inner_join name = `sql_inner_join` ).
|
||||
|
||||
output->display( `----- Left outer join -----` ).
|
||||
|
||||
SELECT _carr~carrid,
|
||||
_carr~carrname,
|
||||
_flsch_lo~cityfrom AS cityfr_lo,
|
||||
coalesce( _flsch_lo~cityto, '???' ) AS cityto_lo
|
||||
FROM zdemo_abap_carr AS _carr
|
||||
LEFT OUTER JOIN zdemo_abap_flsch AS _flsch_lo
|
||||
ON _carr~carrid = _flsch_lo~carrid
|
||||
ORDER BY _carr~carrid
|
||||
INTO TABLE @DATA(sql_left_outer_join).
|
||||
|
||||
output->display( input = sql_left_outer_join name = `sql_left_outer_join` ).
|
||||
|
||||
output->display( `----- Right outer join -----` ).
|
||||
|
||||
SELECT _carr~carrid,
|
||||
_carr~carrname,
|
||||
CASE WHEN _carr~url IS NOT NULL THEN _carr~url
|
||||
ELSE '!!!'
|
||||
END AS url_ro,
|
||||
_flsch_ro~cityfrom AS cityfr_ro,
|
||||
_flsch_ro~cityto AS cityto_ro
|
||||
FROM zdemo_abap_carr AS _carr
|
||||
RIGHT OUTER JOIN zdemo_abap_flsch AS _flsch_ro
|
||||
ON _carr~carrid = _flsch_ro~carrid
|
||||
ORDER BY _carr~carrid
|
||||
INTO TABLE @DATA(sql_right_outer_join).
|
||||
|
||||
output->display( input = sql_right_outer_join name = `sql_right_outer_join` ).
|
||||
|
||||
output->display( `----- Cross join -----` ).
|
||||
|
||||
SELECT _carr~carrid,
|
||||
_carr~carrname,
|
||||
_flsch_cr~cityfrom AS cityfr_cr,
|
||||
_flsch_cr~cityto AS cityto_cr
|
||||
FROM zdemo_abap_carr AS _carr
|
||||
CROSS JOIN zdemo_abap_flsch AS _flsch_cr
|
||||
ORDER BY _carr~carrid
|
||||
INTO TABLE @DATA(sql_cross_join).
|
||||
|
||||
output->display( input = sql_cross_join name = `sql_cross_join` ).
|
||||
|
||||
"Just a check what join example is currently commented in
|
||||
IF cds_joins = sql_inner_join.
|
||||
|
||||
output->display( `In the example CDS view entity, the inner join example is commented in.` ).
|
||||
|
||||
ELSEIF cds_joins = sql_left_outer_join.
|
||||
|
||||
output->display( `In the example CDS view entity, the left outer join example is commented in.` ).
|
||||
|
||||
ELSEIF cds_joins = sql_right_outer_join.
|
||||
|
||||
output->display( `In the example CDS view entity, you have the right outer join example is commented in.` ).
|
||||
|
||||
ELSEIF cds_joins = sql_cross_join.
|
||||
|
||||
output->display( `In the example CDS view entity, you have the cross join example is commented in.` ).
|
||||
|
||||
ELSE.
|
||||
|
||||
output->display( `In the example CDS view entity, there is some other code present.` ).
|
||||
|
||||
ENDIF.
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `Associations` ).
|
||||
|
||||
output->display( `5) Selecting data from a CDS view that contains associations` ).
|
||||
|
||||
"The following ABAP SQL SELECT statement uses a CDS view entity as
|
||||
"the data source. All data is retrieved. The sample CDS view entity
|
||||
"contains multiple associations.
|
||||
"Some fields of some associations are used in the element list of the
|
||||
"CDS view entity. This data is included in the result set. Some
|
||||
"associations are exposed but no fields of those associations are
|
||||
"included in the element list. Therefore, no join is instantiated on
|
||||
"the database and no data from these exposed assocations is included
|
||||
"in the result set.
|
||||
|
||||
SELECT *
|
||||
FROM zdemo_abap_cds_ve_assoc
|
||||
ORDER BY carrier
|
||||
INTO TABLE @DATA(assoc).
|
||||
|
||||
output->display( input = assoc name = `assoc` ).
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `Using exposed associations in ABAP SQL statements: ...` ).
|
||||
|
||||
"The following examples use path expressions to access the association
|
||||
"targets of exposed associations.
|
||||
|
||||
output->display( `6) ... SELECT clause` ).
|
||||
|
||||
"The following ABAP SQL SELECT statement uses a CDS view entity as
|
||||
"the data source. The statement uses an exposed association.
|
||||
"The SELECT list contains fields from the exposed association. Only in
|
||||
"this case (when a consumer, such as an ABAP SQL statement, requests
|
||||
"data) is the join instantiated on the database.
|
||||
"Note:
|
||||
"- No attributes are specified for the path expression (attributes
|
||||
" are covered in examples further down). In particular, a join type is
|
||||
" not explicitly specified. In such a case, the join type depends on
|
||||
" the place where the path expression is used. Since the path expression
|
||||
" is used in the SELECT list of an ABAP SQL SELECT statement (where
|
||||
" fields are specified), a LEFT OUTER JOIN is used by default.
|
||||
"- The coalesce function is included for a field to handle null values.
|
||||
SELECT carrier,
|
||||
\_carr3-carrname,
|
||||
coalesce( \_carr3-url, '###' ) AS cityto_lo
|
||||
FROM zdemo_abap_cds_ve_assoc
|
||||
ORDER BY carrier
|
||||
INTO TABLE @DATA(assoc_exp_select).
|
||||
|
||||
output->display( input = assoc_exp_select name = `assoc_exp_select` ).
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `7) ... FROM clause` ).
|
||||
|
||||
"The following ABAP SQL SELECT statement uses a CDS view entity as
|
||||
"the data source. All data is retrieved.
|
||||
"In this case, an exposed association is used in the FROM clause.
|
||||
"Note:
|
||||
"- No join type is explicitly specified. As mentioned above, the
|
||||
" position of the path expression is important. In this case (when
|
||||
" used in the FROM clause), an INNER JOIN is used by default.
|
||||
"- You can open the data preview for the CDS view entity used and
|
||||
" compare the result set with the output here. Due to the inner
|
||||
" join, non-existent 'carrid' values in the association target are
|
||||
" not contained in the result set.
|
||||
|
||||
SELECT *
|
||||
FROM zdemo_abap_cds_ve_assoc\_carr3 AS _exp
|
||||
ORDER BY carrid
|
||||
INTO TABLE @DATA(assoc_exp_from).
|
||||
|
||||
output->display( input = assoc_exp_from name = `assoc_exp_from` ).
|
||||
|
||||
"The following ABAP SQL SELECT statement is intended to reproduce
|
||||
"the data retrieval as above.
|
||||
"The statement uses the same CDS view entity as data source that
|
||||
"is used by the CDS view entity above as data source.
|
||||
"An inner join is performed on a database table (the _carr3
|
||||
"association from above has this table defined as the association
|
||||
"target in the CDS view entity) since the inner join is used by
|
||||
"default above so as to reproduce the effect. The result set
|
||||
"should be the same as above.
|
||||
|
||||
SELECT _carr~mandt,
|
||||
_carr~carrid,
|
||||
_carr~carrname,
|
||||
_carr~currcode,
|
||||
_carr~url
|
||||
FROM zdemo_abap_cds_ve_assoc_e AS _cds
|
||||
JOIN zdemo_abap_carr AS _carr ON _cds~carrid = _carr~carrid
|
||||
ORDER BY _carr~carrid
|
||||
INTO TABLE @DATA(sql_repr).
|
||||
|
||||
output->display( input = sql_repr name = `sql_repr` ).
|
||||
|
||||
IF sql_repr = assoc_exp_from.
|
||||
output->display( `The result sets are the same.` ).
|
||||
ELSE.
|
||||
output->display( `The result sets are differrent.` ).
|
||||
ENDIF.
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `8) ... Specifying attributes ` ).
|
||||
|
||||
"The following ABAP SQL SELECT statement uses a CDS view entity as
|
||||
"the data source. The statement uses an exposed association.
|
||||
"The SELECT list contains a path expression that is specified with
|
||||
"an attribute.
|
||||
"Note:
|
||||
"- Cardinality, join types, and filter conditions can be used as
|
||||
" attributes.
|
||||
"- In the example, only the cardinality is specified.
|
||||
"- The cardinality can be specified to prevent syntax warnings/errors
|
||||
" in cases where the cardinality of the association does not match
|
||||
" the way it is used in a path expression.
|
||||
"- The example does not explicitly specify a join type. As mentioned
|
||||
" above, if not explicitly specified, the join type depends on where
|
||||
" the path expression is used. Here, a column is specified in the
|
||||
" SELECT list. This means that a LEFT OUTER JOIN is used by default.
|
||||
"- The result set should contain an entry for 'UA' having an initial
|
||||
" value for 'fldate'.
|
||||
|
||||
SELECT carrid,
|
||||
connid,
|
||||
cityfrom,
|
||||
cityto,
|
||||
"Without specifying the cardinality, the following warning
|
||||
"occurs: Using association "_FLI" can increase the cardinality
|
||||
"of the results set
|
||||
"\_fli-fldate
|
||||
|
||||
\_fli[ (*) ]-fldate AS flightdate
|
||||
"The specification above corresponds to the following specification
|
||||
"that includes an explicit specification of LEFT OUTER
|
||||
"\_fli[ (*) LEFT OUTER ]-fldate
|
||||
FROM zdemo_abap_cds_ve_assoc_e
|
||||
ORDER BY carrid, connid, flightdate
|
||||
INTO TABLE @DATA(assoc_attr_card).
|
||||
|
||||
output->display( input = assoc_attr_card name = `assoc_attr_card` ).
|
||||
|
||||
"Specifying the join type explicitly
|
||||
"- INNER, LEFT/RIGHT OUTER are possible
|
||||
"- The join type can only be specified together with the cardinality.
|
||||
"- In the result set of the example, the 'UA' entry should not be
|
||||
" contained.
|
||||
|
||||
SELECT carrid,
|
||||
connid,
|
||||
cityfrom,
|
||||
cityto,
|
||||
\_fli[ (*) INNER ]-fldate AS flightdate
|
||||
FROM zdemo_abap_cds_ve_assoc_e
|
||||
ORDER BY carrid, connid, flightdate
|
||||
INTO TABLE @DATA(assoc_attr_joty).
|
||||
|
||||
output->display( input = assoc_attr_joty name = `assoc_attr_joty` ).
|
||||
|
||||
"Specifying conditions
|
||||
"- Filter conditions can be specified for the current association
|
||||
"- The addition WHERE is optional in cases where the filter condition
|
||||
" is the only attribute specified in the square brackets.
|
||||
"- When the association is instantiated as a join, the filter condition
|
||||
" is transformed into an extended condition for the join.
|
||||
"- In the example, a specific 'carrid' value is filtered for. LEFT OUTER
|
||||
" is specified as join type explicitly. Not specifying the join type here
|
||||
" has the same effect. The 'fldate' value is only retrieved for 'DL'
|
||||
" entries. The other ones have initial values.
|
||||
|
||||
SELECT carrid,
|
||||
connid,
|
||||
cityfrom,
|
||||
cityto,
|
||||
\_fli[ (*) LEFT OUTER WHERE carrid = 'DL' ]-fldate AS flightdate
|
||||
"The following has the same effect in this example
|
||||
"\_fli[ (*) WHERE carrid = 'DL' ]-fldate as flightdate
|
||||
FROM zdemo_abap_cds_ve_assoc_e
|
||||
ORDER BY carrid, connid, flightdate
|
||||
INTO TABLE @DATA(assoc_attr_where).
|
||||
|
||||
output->display( input = assoc_attr_where name = `assoc_attr_where` ).
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `9) ... WHERE clause` ).
|
||||
|
||||
"The following ABAP SQL SELECT statement uses a CDS view entity as
|
||||
"the data source. The statement uses an exposed association.
|
||||
"The SELECT list and the WHERE clause contain a path expression.
|
||||
|
||||
SELECT carrid,
|
||||
connid,
|
||||
countryfr,
|
||||
countryto,
|
||||
\_carr_exp-carrname
|
||||
FROM zdemo_abap_cds_ve_assoc_e
|
||||
WHERE \_carr_exp-carrid LIKE 'A_'
|
||||
ORDER BY carrid, connid
|
||||
INTO TABLE @DATA(assoc_exp_where).
|
||||
|
||||
output->display( input = assoc_exp_where name = `assoc_exp_where` ).
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
16
src/zcl_demo_abap_cds_ve.clas.xml
Normal file
16
src/zcl_demo_abap_cds_ve.clas.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
|
||||
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
|
||||
<asx:values>
|
||||
<VSEOCLASS>
|
||||
<CLSNAME>ZCL_DEMO_ABAP_CDS_VE</CLSNAME>
|
||||
<LANGU>E</LANGU>
|
||||
<DESCRIPT>ABAP cheat sheet: CDS view entities</DESCRIPT>
|
||||
<STATE>1</STATE>
|
||||
<CLSCCINCL>X</CLSCCINCL>
|
||||
<FIXPT>X</FIXPT>
|
||||
<UNICODE>5</UNICODE>
|
||||
</VSEOCLASS>
|
||||
</asx:values>
|
||||
</asx:abap>
|
||||
</abapGit>
|
||||
61
src/zdemo_abap_cds_ve_agg_exp.ddls.asddls
Normal file
61
src/zdemo_abap_cds_ve_agg_exp.ddls.asddls
Normal file
@@ -0,0 +1,61 @@
|
||||
// ABAP CDS cheat sheet example:
|
||||
// Aggregate expressions in the element list of CDS view entities
|
||||
//
|
||||
//////////////////////////------ NOTES ------//////////////////////////////////
|
||||
// - CDS view entity selects from a demo database table
|
||||
// - Demonstrates various aggregate expressions in the element list
|
||||
// - As a prerequisite, run the class zcl_abap_demo_cds_ve to populate the
|
||||
// database tables of the example. Otherwise, no data is displayed.
|
||||
//
|
||||
//////////////////////------ DATA PREVIEW ------///////////////////////////////
|
||||
// - Choose F8 in ADT to open the data preview and check out the data displayed
|
||||
// - For comparing and checking the output, you can also open the data preview
|
||||
// for the database table. In ADT, press and hold CTRL and click the database
|
||||
// table name. In the opened table artifact, choose F8 to open the data preview.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@AccessControl.authorizationCheck: #NOT_REQUIRED
|
||||
@EndUserText.label: 'CDS view entity: Aggregate expressions'
|
||||
define view entity zdemo_abap_cds_ve_agg_exp
|
||||
as select from zdemo_abap_fli
|
||||
|
||||
{
|
||||
|
||||
// The SELECT list intentionally includes few fields only to focus on the effect of aggregate expressions.
|
||||
key carrid,
|
||||
currency,
|
||||
|
||||
// -------- Aggregate expressions --------
|
||||
// - Aggregate expressions can be used as elements of a SELECT list. Other positions are possible.
|
||||
// - An alias name must be specified.
|
||||
// - A GROUP BY clause is required. It must list all non-aggregated fields from the SELECT list.
|
||||
// - Additions: If ALL is used, all rows in the result set are respected. This the default setting.
|
||||
// If DISTINCT is used, only distinct values of an argument are respected.
|
||||
|
||||
// AVG (Returns the average value of an argument)
|
||||
avg( seatsocc as abap.dec(15,2)) as avg_seats_occ,
|
||||
avg( cast(paymentsum as abap.dec(15, 2)) as abap.dec(15,2)) as avg_paysum,
|
||||
|
||||
// SUM (Returns the sum of an argument)
|
||||
// Since a currency field is used in the example, an annotatin is required.
|
||||
@Semantics.amount.currencyCode: 'currency'
|
||||
sum(paymentsum) as total_paysum,
|
||||
|
||||
// MIN (Returns the least value of an argument)
|
||||
min( seatsocc ) as min_occ_seats,
|
||||
|
||||
// MAX (Returns the greatest value of an argument)
|
||||
max( seatsocc ) as max_occ_seats,
|
||||
max( all seatsocc ) as max_occ_seats_all, //Same result as above, ALL is optional
|
||||
|
||||
// COUNT (Returns counted lines)
|
||||
count(*) as cnt, // * means that all lines are respected
|
||||
count(distinct planetype) as cnt_planetype //DISTINCT means that the number of dinstinct values if an argument is counted;
|
||||
//e.g. if 3 is returned, it means there are 3 different plane types among the result set
|
||||
|
||||
}
|
||||
//GROUP BY clause that lists all non-aggregated fields from the SELECT list
|
||||
group by
|
||||
carrid,
|
||||
currency
|
||||
19
src/zdemo_abap_cds_ve_agg_exp.ddls.baseinfo
Normal file
19
src/zdemo_abap_cds_ve_agg_exp.ddls.baseinfo
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"BASEINFO":
|
||||
{
|
||||
"FROM":
|
||||
[
|
||||
"ZDEMO_ABAP_FLI"
|
||||
],
|
||||
"ASSOCIATED":
|
||||
[],
|
||||
"BASE":
|
||||
[],
|
||||
"ANNO_REF":
|
||||
[],
|
||||
"SCALAR_FUNCTION":
|
||||
[],
|
||||
"VERSION":0,
|
||||
"ANNOREF_EVALUATION_ERROR":""
|
||||
}
|
||||
}
|
||||
13
src/zdemo_abap_cds_ve_agg_exp.ddls.xml
Normal file
13
src/zdemo_abap_cds_ve_agg_exp.ddls.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<abapGit version="v1.0.0" serializer="LCL_OBJECT_DDLS" serializer_version="v1.0.0">
|
||||
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
|
||||
<asx:values>
|
||||
<DDLS>
|
||||
<DDLNAME>ZDEMO_ABAP_CDS_VE_AGG_EXP</DDLNAME>
|
||||
<DDLANGUAGE>E</DDLANGUAGE>
|
||||
<DDTEXT>CDS view entity: Aggregate expressions</DDTEXT>
|
||||
<SOURCE_TYPE>W</SOURCE_TYPE>
|
||||
</DDLS>
|
||||
</asx:values>
|
||||
</asx:abap>
|
||||
</abapGit>
|
||||
144
src/zdemo_abap_cds_ve_assoc.ddls.asddls
Normal file
144
src/zdemo_abap_cds_ve_assoc.ddls.asddls
Normal file
@@ -0,0 +1,144 @@
|
||||
// ABAP CDS cheat sheet example: Associations
|
||||
//
|
||||
//////////////////////////------ NOTES ------//////////////////////////////////
|
||||
// - CDS view entity that selects from a demo CDS view entity and demonstrates
|
||||
// associations. A selection of use cases of associations is covered. For
|
||||
// more information and examples, see the ABAP Keyword Documentation.
|
||||
// - As a prerequisite, run the class zcl_abap_demo_cds_ve to populate the
|
||||
// database tables of the example. Otherwise, no data is displayed.
|
||||
// - For further notes on associations, see the commented out information further
|
||||
// down.
|
||||
// - To see how the joins are realized on the database, you can right-click anywhere
|
||||
// in the source code and choose 'Show SQL Create Statement'. The example
|
||||
// includes associations where only left outer joins are performed.
|
||||
//
|
||||
//////////////////////------ DATA PREVIEW ------///////////////////////////////
|
||||
// - Choose F8 in ADT to open the data preview and check out the data displayed
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@AccessControl.authorizationCheck: #NOT_REQUIRED
|
||||
@EndUserText.label: 'CDS view entity: Associations 1'
|
||||
define view entity zdemo_abap_cds_ve_assoc
|
||||
// In this example, the data source, from which data is read, is another CDS view entity.
|
||||
// This source CDS view entity selects all fields from the demo database table zdemo_abap_flsch.
|
||||
// It also contains exposed associations that are used here.
|
||||
as select from zdemo_abap_cds_ve_assoc_e as _flsch
|
||||
|
||||
// 1) One use case of assocations, which is not covered here, can be the use in the FROM clause.
|
||||
// - That is, you can use an association as data source by specifying a path expression.
|
||||
// - For example, the CDS view entity zdemo_abap_cds_ve_assoc_e exposes an association
|
||||
// _carr_exp1. Therefore, a FROM clause as follows is possible:
|
||||
// ... as select from zdemo_abap_cds_ve_assoc_e._carr_exp1 as _flsch ...
|
||||
|
||||
// 2) Adding fields of an association target to the element list (of the same CDS view entity)
|
||||
// - In this case, since data is requested, a join is carried out.
|
||||
// - The data preview (choose F8) shows the values for the added fields.
|
||||
// - In this example, the association is used, but not exposed. Therefore, it cannot be used
|
||||
// in path expressions in other CDS entities as shown in 3).
|
||||
association [1..1] to zdemo_abap_carr as _carr1 on _flsch.Carrid = _carr1.carrid
|
||||
association [1..1] to zdemo_abap_carr as _carr2 on _flsch.Carrid = _carr2.carrid
|
||||
association [1..*] to zdemo_abap_fli as _fli on _flsch.Carrid = _fli.carrid
|
||||
|
||||
// 3) Exposing associations
|
||||
// - In this example, no fields of the association target are added to the element list.
|
||||
// But the assocation is exposed in the element list. Therefore, it can be used
|
||||
// in path expressions in other CDS entities.
|
||||
// - In this case, since data is not requested, a join is not carried out. It is up to
|
||||
// to the consumer (e.g. an ABAP SQL statement or other CDS view entities) to request
|
||||
// fields (one field, multiple fields, all fields ...). Only then, a join is performed.
|
||||
// - The data preview (choose F8) does not show any data of the association target.
|
||||
// - Compare the ON condition to the ones above. For demonstration purposes, the carrid
|
||||
// field is specified with an alias name in the element list. To refer to this name,
|
||||
// the prefix $projection. is required. As above, you can also use the original name.
|
||||
association [1..1] to zdemo_abap_carr as _carr3 on $projection.carrier = _carr3.carrid
|
||||
|
||||
{
|
||||
// Including fields from the data source
|
||||
// The prefix _flsch. is actually not necessary here in the example. It's intentionally included to emphasize the data source.
|
||||
key _flsch.Carrid as carrier,
|
||||
key _flsch.Connid as connection_id,
|
||||
_flsch.Cityfrom,
|
||||
_flsch.Cityto,
|
||||
|
||||
// Regarding 2) Adding fields of an association target to the element list
|
||||
// - Two fields are added in the element list.
|
||||
// - You can make a right-click and choose 'Show SQL Create Statement' to check how the join is performed.
|
||||
// - Because a left outer join is performed, the coalesce function is included for a field.
|
||||
// If there are null values, the specified character literal is inserted.
|
||||
// - Only these two fields are respected from the association target.
|
||||
_carr1.carrname as carrier_name,
|
||||
coalesce(_carr1.url, 'NULL') as carr_url,
|
||||
|
||||
// Regarding 3) Exposing associations
|
||||
// - The association is exposed in the element list (but no fields of the association target).
|
||||
// - As demonstrated in the executable class of this cheat sheet, an ABAP SQL statement
|
||||
// can request data of the association target.
|
||||
// - The data preview does not include any fields from the association target. Likewise,
|
||||
// the 'SQL Create Statement' does not show any join with this assocation.
|
||||
_carr3,
|
||||
|
||||
// 4) Using associations that are exposed in external views
|
||||
// - The CDS view entity that is used as data source in the SELECT list exposes
|
||||
// associations.
|
||||
// - Such an association is used in a path expression here to include a field from the
|
||||
// association target in the element list. In this case, this CDS view entity is
|
||||
// a consumer of the association. A join is performed, as can be seen in the
|
||||
// 'SQL Create Statement' view.
|
||||
_flsch._carr_exp.currcode as curr_exposed,
|
||||
|
||||
|
||||
// 5) Attributes of path expression
|
||||
// - Attributes are specified in angle brackets after each CDS association to define
|
||||
// further properties. These can be the cardinality, join type that is implemented when used,
|
||||
// and filter conditions
|
||||
|
||||
// Speciying filter conditions and the join type
|
||||
// - Only if a join type is specified explicitly, the addition WHERE must be specified
|
||||
// - The example specifies the join type. Therefore, WHERE is required. Here in the example,
|
||||
// the specification can also be the one commented out below because it implicitly has
|
||||
// the same effect. Here, a left outer join is performed by default.
|
||||
// - The operand specified on the left side of the condition must be a field of the association target.
|
||||
_carr2[left outer where $projection.carrier = 'LH'].url as fcond_url,
|
||||
//_carr2[$projection.carrier = 'LH'].url as fcond_url
|
||||
|
||||
// Specifying the cardinality of the current CDS association
|
||||
// - The values 1: (current CDS association is declared as unique) and *: (non-unique) are possible.
|
||||
// - You can use 1: to prevent a syntax warning in case of a path specified with filter conditions, for example.
|
||||
// - In the example, an association is specified with to many (there can be multiple flights for a flight route).
|
||||
// Without the cardinality specification 1:, a warning would be displayed ("The association _fli can modify
|
||||
// the cardinality of the results set").
|
||||
// - Similar to the example above, the first line commented out below is also possible in this example.
|
||||
// The second line commented out results in a syntax warning because it does not specify the
|
||||
// cardinality 1:.
|
||||
|
||||
_fli[1:$projection.connection_id = connid].fldate as fcond_fldate
|
||||
//_fli[1:left outer where $projection.connection_id = connid].fldate as fcond_fldate
|
||||
//_fli[$projection.connection_id = connid].fldate as fcond_fldate
|
||||
}
|
||||
|
||||
////////////////////////// --------- Notes on associations --------- /////////////////////////////////////////
|
||||
// - They offer an advanced modelling capability for CDS data models. They define relationships between
|
||||
// CDS entities (association source and target). On the database, associations are internally transformed into joins.
|
||||
// - Compared to regular joins, associations differ in the following respects:
|
||||
// - Joins are only performed on demand, i.e. when data is requested from the assocation by a consumer, e.g.
|
||||
// using another CDS view entity or an ABAP SQL statement. In the regular joins (e.g. inner, outer joins), the
|
||||
// join is always performed. When a CDS association is instantiated as join on the database, the association
|
||||
// source represents the left side and the association target represents the right side of the join.
|
||||
// - In the SELECT list, specifying the data source as a prefix is mandatory for all fields of the association.
|
||||
// For joins, it is mandatory only for for non-unique names.
|
||||
// - Unlike regular joins, associations can be reused in different positions and basically replace very complex
|
||||
// join expressions.
|
||||
// - Use of associations:
|
||||
// - Including fields from the association target in the current view
|
||||
// - Exposing associations so that they can be used in other CDS entities or in ABAP SQL
|
||||
// - Associations can be used in path expressions (a sequence of associations) at different operand positions.
|
||||
// - Associations can be specified with additional semantic information, such as cardinality.
|
||||
// - Specifying the (optional) cardinality is a means of documenting the semantics of the data model.
|
||||
// - Cardinality is specified in square brackets [ ]. Minimum and maximum can be specified, for example
|
||||
// one to one [1..1], one to many [1..*]. The default is [0..1]. Specifying the minimum value is optional (the
|
||||
// default value is 0), i.e. [0..1] is [1], [0..*] is [*]. The minimum cannot be *, the maximum not 0.
|
||||
// - Maximum values greater than 1 can lead to syntax errors or warnings. Generally, a non-matching cardinality
|
||||
// usually produces a warning.
|
||||
// - If the cardinality is not specified, it is to one by default [x..1].
|
||||
// - Compositions and to-parent associations are special kinds of CDS associations. They are not covered in the example.
|
||||
23
src/zdemo_abap_cds_ve_assoc.ddls.baseinfo
Normal file
23
src/zdemo_abap_cds_ve_assoc.ddls.baseinfo
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"BASEINFO":
|
||||
{
|
||||
"FROM":
|
||||
[
|
||||
"ZDEMO_ABAP_CDS_VE_ASSOC_E",
|
||||
"ZDEMO_ABAP_CARR",
|
||||
"ZDEMO_ABAP_FLI"
|
||||
],
|
||||
"ASSOCIATED":
|
||||
[
|
||||
"ZDEMO_ABAP_CARR"
|
||||
],
|
||||
"BASE":
|
||||
[],
|
||||
"ANNO_REF":
|
||||
[],
|
||||
"SCALAR_FUNCTION":
|
||||
[],
|
||||
"VERSION":0,
|
||||
"ANNOREF_EVALUATION_ERROR":""
|
||||
}
|
||||
}
|
||||
13
src/zdemo_abap_cds_ve_assoc.ddls.xml
Normal file
13
src/zdemo_abap_cds_ve_assoc.ddls.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<abapGit version="v1.0.0" serializer="LCL_OBJECT_DDLS" serializer_version="v1.0.0">
|
||||
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
|
||||
<asx:values>
|
||||
<DDLS>
|
||||
<DDLNAME>ZDEMO_ABAP_CDS_VE_ASSOC</DDLNAME>
|
||||
<DDLANGUAGE>E</DDLANGUAGE>
|
||||
<DDTEXT>CDS view entity: Associations</DDTEXT>
|
||||
<SOURCE_TYPE>W</SOURCE_TYPE>
|
||||
</DDLS>
|
||||
</asx:values>
|
||||
</asx:abap>
|
||||
</abapGit>
|
||||
40
src/zdemo_abap_cds_ve_assoc_e.ddls.asddls
Normal file
40
src/zdemo_abap_cds_ve_assoc_e.ddls.asddls
Normal file
@@ -0,0 +1,40 @@
|
||||
// ABAP CDS cheat sheet example: Associations
|
||||
//
|
||||
//////////////////////////------ NOTES ------//////////////////////////////////
|
||||
// - CDS view entity that selects from a demo database table.
|
||||
// - The purpose of this CDS view entity is to demonstrate associations. The view
|
||||
// is used as a data source in another CDS view entity.
|
||||
// - As a prerequisite, run the class zcl_abap_demo_cds_ve to populate the
|
||||
// database tables of the example. Otherwise, no data is displayed.
|
||||
//
|
||||
//////////////////////------ DATA PREVIEW ------///////////////////////////////
|
||||
// - Choose F8 in ADT to open the data preview and check out the data displayed
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@AccessControl.authorizationCheck: #NOT_REQUIRED
|
||||
@EndUserText.label: 'CDS view entity: Associations'
|
||||
define view entity zdemo_abap_cds_ve_assoc_e
|
||||
as select from zdemo_abap_flsch as _flsch
|
||||
association [1..1] to zdemo_abap_carr as _carr_exp on _flsch.carrid = _carr_exp.carrid
|
||||
association [1..*] to zdemo_abap_fli as _fli on _flsch.carrid = _fli.carrid and _flsch.connid = _fli.connid
|
||||
|
||||
{
|
||||
key _flsch.carrid as Carrid,
|
||||
key _flsch.connid as Connid,
|
||||
_flsch.countryfr as Countryfr,
|
||||
_flsch.cityfrom as Cityfrom,
|
||||
_flsch.airpfrom as Airpfrom,
|
||||
_flsch.countryto as Countryto,
|
||||
_flsch.cityto as Cityto,
|
||||
_flsch.airpto as Airpto,
|
||||
_flsch.fltime as Fltime,
|
||||
_flsch.deptime as Deptime,
|
||||
_flsch.arrtime as Arrtime,
|
||||
_flsch.distance as Distance,
|
||||
_flsch.distid as Distid,
|
||||
_flsch.fltype as Fltype,
|
||||
_flsch.period as Period,
|
||||
_carr_exp,
|
||||
_fli
|
||||
}
|
||||
22
src/zdemo_abap_cds_ve_assoc_e.ddls.baseinfo
Normal file
22
src/zdemo_abap_cds_ve_assoc_e.ddls.baseinfo
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"BASEINFO":
|
||||
{
|
||||
"FROM":
|
||||
[
|
||||
"ZDEMO_ABAP_FLSCH"
|
||||
],
|
||||
"ASSOCIATED":
|
||||
[
|
||||
"ZDEMO_ABAP_CARR",
|
||||
"ZDEMO_ABAP_FLI"
|
||||
],
|
||||
"BASE":
|
||||
[],
|
||||
"ANNO_REF":
|
||||
[],
|
||||
"SCALAR_FUNCTION":
|
||||
[],
|
||||
"VERSION":0,
|
||||
"ANNOREF_EVALUATION_ERROR":""
|
||||
}
|
||||
}
|
||||
13
src/zdemo_abap_cds_ve_assoc_e.ddls.xml
Normal file
13
src/zdemo_abap_cds_ve_assoc_e.ddls.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<abapGit version="v1.0.0" serializer="LCL_OBJECT_DDLS" serializer_version="v1.0.0">
|
||||
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
|
||||
<asx:values>
|
||||
<DDLS>
|
||||
<DDLNAME>ZDEMO_ABAP_CDS_VE_ASSOC_E</DDLNAME>
|
||||
<DDLANGUAGE>E</DDLANGUAGE>
|
||||
<DDTEXT>CDS view entity: Associations</DDTEXT>
|
||||
<SOURCE_TYPE>W</SOURCE_TYPE>
|
||||
</DDLS>
|
||||
</asx:values>
|
||||
</asx:abap>
|
||||
</abapGit>
|
||||
132
src/zdemo_abap_cds_ve_joins.ddls.asddls
Normal file
132
src/zdemo_abap_cds_ve_joins.ddls.asddls
Normal file
@@ -0,0 +1,132 @@
|
||||
// ABAP CDS cheat sheet example: Joins
|
||||
//
|
||||
//////////////////////////------ NOTES ------//////////////////////////////////
|
||||
// - CDS view entity selects from a demo database table
|
||||
// - Demonstrates various joins
|
||||
// - As a prerequisite, run the class zcl_abap_demo_cds_ve to populate the
|
||||
// database tables of the example. Otherwise, no data is displayed.
|
||||
// It is particularly needed in this case because it contains entries to
|
||||
// visualize the effect of outer joins.
|
||||
// - HOW TO:
|
||||
// - To reduce the number of separate artifacts, this example CDS view entity
|
||||
// contains the code for multiple joins, which is commented out.
|
||||
// - To test out various joins, you can comment out and comment in the
|
||||
// respective code sections (select the lines and choose CTRL + 7).
|
||||
// - The example for inner joins is commented in by default. The relevant
|
||||
// code sections are marked with "COMMENT IN/OUT ... START" and
|
||||
// "COMMENT IN/OUT ... START" in both SELECT and element list
|
||||
// (inner joins -> 1a / 1b). To test the left outer joins, for example,
|
||||
// comment out the respective sections of the inner join and comment in
|
||||
// the sections for left outer joins (2a, 2b), and so on.
|
||||
// - Once done, activate the view and choose F8 to open the data preview.
|
||||
// You must activate the view and choose F8 again for every change here
|
||||
// because the alias names are different.
|
||||
//
|
||||
//////////////////////------ DATA PREVIEW ------///////////////////////////////
|
||||
// - Choose F8 in ADT to open the data preview and check out the data displayed
|
||||
// - Note the hints above regarding commenting in/out of code.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@AccessControl.authorizationCheck: #NOT_REQUIRED
|
||||
@EndUserText.label: 'CDS view entity: Joins'
|
||||
define view entity zdemo_abap_cds_ve_joins
|
||||
as select from zdemo_abap_carr as _carr
|
||||
|
||||
// Notes on joins:
|
||||
// - Joins combine two data sources into one result set, consisting of columns of both data sources
|
||||
// - Rows of the result set are determined by the join type and by join conditions between columns of the data sources
|
||||
// - Joins can be nested
|
||||
// - Inner or outer join must contain a join condition after ON
|
||||
// - Each element of the SELECT list must have the name of its data source as prefix
|
||||
// - When joins are used, a WHERE condition affects the result set
|
||||
// - The cardinality can be specified for left outer joins. For more details, see the ABAP Keyword Documentation.
|
||||
|
||||
/////////////////// ----- Example for INNER JOIN ----- /////////////////////////////////
|
||||
// Notes:
|
||||
// - Joins all entries of the data sources whose fields match the ON condition
|
||||
// - In the table zdemo_abap_carr, there are entries in carrid that are not present in zdemo_abap_flsch.
|
||||
// The same is true the other way round. The result only contains those entries that exist in both data sources
|
||||
// based on the ON condition.
|
||||
|
||||
//// ----> COMMENT IN/OUT 1a START <-----
|
||||
inner join zdemo_abap_flsch as _flsch_in on _carr.carrid = _flsch_in.carrid
|
||||
//// ----> COMMENT IN/OUT 1a END <-----
|
||||
|
||||
//Alternative syntax: Specifying 'inner' is optional
|
||||
// join zdemo_abap_flsch
|
||||
// as _flsch_in on _carr.carrid = _flsch_in.carrid
|
||||
|
||||
/////////////////// ----- Example for LEFT OUTER JOIN ----- /////////////////////////////////
|
||||
// Notes:
|
||||
// - The join selects all entries on the left side. Entries that match the ON condition have the same content as in the inner join.
|
||||
// In entries that do not match the ON condition, the elements on the right side have the null value.
|
||||
// - To demonstrate the effect, the table zdemo_abap_carr contains entries for carrid that are not present in zdemo_abap_flsch.
|
||||
// - Example in the SELECT list:
|
||||
// - The coalesce function can be used to prevent null values in the result set.
|
||||
// - In the example, the function is used for an element to prevent null values.
|
||||
|
||||
//// ----> COMMENT IN/OUT 2A START <-----
|
||||
// left outer join zdemo_abap_flsch as _flsch_lo on _carr.carrid = _flsch_lo.carrid
|
||||
//// ----> COMMENT IN/OUT 2A END <-----
|
||||
|
||||
/////////////////// ----- Example for RIGHT OUTER JOIN ----- /////////////////////////////////
|
||||
// Notes:
|
||||
// - The join selects all entries on the right side. Entries that match the ON condition have the same content as in the inner join.
|
||||
// In entries that do not match the ON condition, the elements on the left side have the null value.
|
||||
// - To demonstrate the effect, the table zdemo_abap_carr contains entries for carrid that are not present in zdemo_abap_flsch.
|
||||
// - Example in the SELECT list:
|
||||
// - Instead of the coalesce function, you can also use a CASE expression using a logical expression with IS [NOT] NULL
|
||||
// to prevent null values in the result set.
|
||||
// - In the example, a CASE expression is used for an element to prevent null values.
|
||||
|
||||
//// ----> COMMENT IN/OUT 3A START <-----
|
||||
// right outer join zdemo_abap_flsch as _flsch_ro on _carr.carrid = _flsch_ro.carrid
|
||||
//// ----> COMMENT IN/OUT 3A END <-----
|
||||
|
||||
/////////////////// ----- Example for CROSS JOIN ----- /////////////////////////////////
|
||||
// Notes:
|
||||
// - All entries on the left side are combined with all entries on the right side.
|
||||
// - It is not possible to specify an ON condition.
|
||||
//- The number of lines of the result is the number of left lines multiplied by the number of lines of the right table.
|
||||
|
||||
//// ----> COMMENT IN/OUT 4A START <-----
|
||||
// cross join zdemo_abap_flsch as _flsch_cr
|
||||
//// ----> COMMENT IN/OUT 4A END <-----
|
||||
|
||||
{
|
||||
key _carr.carrid,
|
||||
_carr.carrname,
|
||||
|
||||
/////////////////// ----- Example for INNER JOIN ----- /////////////////////////////////
|
||||
|
||||
// ----> COMMENT IN/OUT 1b START <-----
|
||||
_flsch_in.cityfrom as cityfr_in,
|
||||
_flsch_in.cityto as cityto_in
|
||||
// ----> COMMENT IN/OUT 1b END <-----
|
||||
|
||||
/////////////////// ----- Example for LEFT OUTER JOIN ----- /////////////////////////////////
|
||||
|
||||
//// ----> COMMENT IN/OUT 2b START <-----
|
||||
// _flsch_lo.cityfrom as cityfr_lo,
|
||||
// coalesce(_flsch_lo.cityto, '???') as cityto_lo
|
||||
//// ----> COMMENT IN/OUT 2b END <-----
|
||||
|
||||
/////////////////// ----- Example for RIGHT OUTER JOIN ----- /////////////////////////////////
|
||||
|
||||
//// ----> COMMENT IN/OUT 3b START <-----
|
||||
// case when _carr.url is not null then _carr.url
|
||||
// else '!!!'
|
||||
// end as url_ro,
|
||||
// _flsch_ro.cityfrom as cityfr_ro,
|
||||
// _flsch_ro.cityto as cityto_ro
|
||||
//// ----> COMMENT IN/OUT 3b END <-----
|
||||
|
||||
/////////////////// ----- Example for CROSS JOIN ----- /////////////////////////////////
|
||||
|
||||
//// ----> COMMENT IN/OUT 4b START <-----
|
||||
// _flsch_cr.cityfrom as cityfr_cr,
|
||||
// _flsch_cr.cityto as cityto_cr
|
||||
//// ----> COMMENT IN/OUT 4b END <-----
|
||||
|
||||
}
|
||||
20
src/zdemo_abap_cds_ve_joins.ddls.baseinfo
Normal file
20
src/zdemo_abap_cds_ve_joins.ddls.baseinfo
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"BASEINFO":
|
||||
{
|
||||
"FROM":
|
||||
[
|
||||
"ZDEMO_ABAP_CARR",
|
||||
"ZDEMO_ABAP_FLSCH"
|
||||
],
|
||||
"ASSOCIATED":
|
||||
[],
|
||||
"BASE":
|
||||
[],
|
||||
"ANNO_REF":
|
||||
[],
|
||||
"SCALAR_FUNCTION":
|
||||
[],
|
||||
"VERSION":0,
|
||||
"ANNOREF_EVALUATION_ERROR":""
|
||||
}
|
||||
}
|
||||
13
src/zdemo_abap_cds_ve_joins.ddls.xml
Normal file
13
src/zdemo_abap_cds_ve_joins.ddls.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<abapGit version="v1.0.0" serializer="LCL_OBJECT_DDLS" serializer_version="v1.0.0">
|
||||
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
|
||||
<asx:values>
|
||||
<DDLS>
|
||||
<DDLNAME>ZDEMO_ABAP_CDS_VE_JOINS</DDLNAME>
|
||||
<DDLANGUAGE>E</DDLANGUAGE>
|
||||
<DDTEXT>CDS view entity: Joins</DDTEXT>
|
||||
<SOURCE_TYPE>W</SOURCE_TYPE>
|
||||
</DDLS>
|
||||
</asx:values>
|
||||
</asx:abap>
|
||||
</abapGit>
|
||||
239
src/zdemo_abap_cds_ve_sel.ddls.asddls
Normal file
239
src/zdemo_abap_cds_ve_sel.ddls.asddls
Normal file
@@ -0,0 +1,239 @@
|
||||
// ABAP CDS cheat sheet example:
|
||||
// Operands and expressions in CDS view entities
|
||||
//
|
||||
//////////////////////////------ NOTES ------//////////////////////////////////
|
||||
// - CDS view entity selects from a demo database table
|
||||
// - Demonstrates various syntax options regarding operands and expressions
|
||||
// - As a prerequisite, run the class zcl_abap_demo_cds_ve to populate the
|
||||
// database tables of the example. Otherwise, no data is displayed.
|
||||
//
|
||||
//////////////////////------ DATA PREVIEW ------///////////////////////////////
|
||||
// - Choose F8 in ADT to open the data preview and check out the data displayed
|
||||
// - The example includes parameters. Therefore, you are prompted to insert a
|
||||
// value. In this example, the parameter (maximum seats in a plane) is used for
|
||||
// the WHERE clause (the lower the number entered, the more entries in the result
|
||||
// set).
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
//////////////////////------ Annotations ------///////////////////////////////
|
||||
// Annotations add metadata to a CDS object that expands the syntax options of SQL.
|
||||
// There's a predefined set of SAP annotations. Their specification is optional.
|
||||
|
||||
// Example for a view entity annotation (only possible in CDS view entities)
|
||||
// The following annotation defines implicit access control when ABAP SQL is used
|
||||
// to access the CDS view entity. Here, it is determined that no access control
|
||||
// is required.
|
||||
@AccessControl.authorizationCheck: #NOT_REQUIRED
|
||||
|
||||
// Example for an entity annotation (annotations that can be used in all CDS entities)
|
||||
// The following annotation provdes a translatable short text of the CDS entity.
|
||||
@EndUserText.label: 'CDS view entity: Operands/Expressions'
|
||||
define view entity zdemo_abap_cds_ve_sel
|
||||
with parameters
|
||||
p_smax : abap.int4 //Input parameter typed with an elementary data type;
|
||||
//can also be a DDIC data element
|
||||
|
||||
as select from zdemo_abap_fli //Selection from a demo database table;
|
||||
//an alias name can be also specified ... as _fli, for example
|
||||
|
||||
// The following SELECT list demonstrates possible elements, operands and expressions.
|
||||
// Note: Many of the operands and expressions demonstrated below can occur in multiple positions.
|
||||
// Refer to the ABAP Keyword Documentation for the details.
|
||||
{
|
||||
// -------- Specifying fields of the data source --------
|
||||
//- Multiple fields of the data source from which to be selected are specified.
|
||||
//- Field names can be prefixed with the name of the data source (or, if specified, with the alias name).
|
||||
//- Alias names can be specified for the elements. Note: In case of joins (selection from multiple sources)
|
||||
// all elements must be prefixed with the name of the data source. See the view entity demonstrating joins.
|
||||
//- KEY defines the current element as a key element; must be placed at the beginning; in CDS entities the key
|
||||
// elements are mainly used to document the semantics of the data model (Note: They do not define unique lines
|
||||
// in its result with regard to the key.)
|
||||
|
||||
key carrid as CarrierId, //Alias name specified
|
||||
key zdemo_abap_fli.connid as Connid, //Element prefixed with data source; alias name specified
|
||||
key fldate,
|
||||
price,
|
||||
currency,
|
||||
paymentsum,
|
||||
planetype,
|
||||
seatsmax,
|
||||
seatsocc,
|
||||
|
||||
// -------- Literals --------
|
||||
// Typed literals (cover most built-in types of the ABAP Dictionary)
|
||||
// As is true for all of the following elements, an alias name must be specified.
|
||||
abap.int4'12345' as int4, //Typed numeric literal
|
||||
abap.char'hallo' as c5, //Typed character literal
|
||||
abap.dats'20240101' as date_lit, //Date
|
||||
|
||||
// Note: In case of a currency or quantity field specified with a typed literal, a reference
|
||||
// to a currency key or unit is mandatory, which requires an annotation. The currency example
|
||||
// here uses the following annotation. The element 'currency' from above is referenced.
|
||||
@Semantics.amount.currencyCode: 'currency'
|
||||
abap.curr'12.34' as curr,
|
||||
abap.dec'0.9' as discnt,
|
||||
abap.unit'KM' as kilometers,
|
||||
|
||||
// Untyped literals
|
||||
'Minutes' as fltime_ut, //Untyped character literal
|
||||
1 as num_lit, //Untyped numeric literal
|
||||
|
||||
// -------- Parameters --------
|
||||
// Parameters can be specified in an operand position.
|
||||
// The name of the parameter must be prefixed by $parameters.
|
||||
$parameters.p_smax as param,
|
||||
|
||||
// -------- Session variables --------
|
||||
// Among other sesion variables, there are built-in session variables available.
|
||||
$session.user as usr,
|
||||
$session.client as clnt,
|
||||
$session.system_language as langu,
|
||||
$session.system_date as sys_date,
|
||||
$session.user_timezone as usr_time,
|
||||
$session.user_date as usr_date,
|
||||
|
||||
// -------- Expressions --------
|
||||
//Note: Aggregate and path expressions are covered in a separate CDS view entity.
|
||||
|
||||
// -------- Cast expressions --------
|
||||
// Convert the value of operands to a specified type after as
|
||||
// The examples use built-in data types only.
|
||||
// Note: Regarding which conversion combinations of types are possible, see the ABAP Keyword Documentation.
|
||||
// There are special conversion rules for every combination.
|
||||
cast( price as abap.dec(15,2) ) as cast_curr2dec,
|
||||
|
||||
// The following example uses the prefix $projection. which defines reuse expressions.
|
||||
// That is, you can refer to an element defined previously in the SELECT list of the same CDS view entity.
|
||||
// Note that this is only possible in dedicated positions. One of them is a cast expression.
|
||||
//cast( $projection.date_lit as abap.int4 ) as cast_dats,
|
||||
//cast( $projection.curr as abap.int4 ) as cast_curr,
|
||||
cast( $projection.date_lit as abap.char(8) ) as cast_dats2c,
|
||||
|
||||
// -------- Arithmetic expressions --------
|
||||
seatsocc_b + seatsocc_f as occ_seats_classes,
|
||||
seatsmax - seatsocc as free_seats,
|
||||
2 * 2 as mult,
|
||||
9 / 3 as div,
|
||||
//Arithmetic expressions using cast and reuse expressions
|
||||
cast( paymentsum as abap.dec(17,2) ) * abap.dec'0.75' as discount_1,
|
||||
$projection.cast_curr2dec * $projection.discnt as discount_2,
|
||||
cast( seatsocc / seatsmax * 100 as abap.dec(10, 2) ) as occupancy_rate,
|
||||
|
||||
// -------- Case expressions --------
|
||||
// Simple case distinction for comparing values of operands with other operands.
|
||||
// The first operand specified after THEN for which the comparison is true is
|
||||
// returned as a result. No match: Result is determined by the ELSE branch.
|
||||
|
||||
case currency
|
||||
when 'EUR' then 'X'
|
||||
when 'USD' then 'Y'
|
||||
else 'Z'
|
||||
end as case1,
|
||||
|
||||
// In newer ABAP releases, you can use the ELSE NULL addition that returns the null value.
|
||||
// Note: If ELSE is not specified, the null value is returned as a result.
|
||||
// case $projection.case1
|
||||
// when 'X' then 'A'
|
||||
// else null
|
||||
// end as case2,
|
||||
|
||||
// Complex case distinction (searched case) for evaluating conditions
|
||||
case
|
||||
when seatsmax <= 150 then 'small'
|
||||
when seatsmax > 150 and seatsmax < 300 then 'middle'
|
||||
when seatsmax >= 300 then 'large'
|
||||
else '?'
|
||||
end as case3,
|
||||
|
||||
// -------- Excursion: Logical expressions --------
|
||||
// The following nonsense example using a CASE expression just visualizes the rich variety of options.
|
||||
|
||||
//Comparison operators
|
||||
// Boolean operators AND, OR, NOT as well as parenthesized expressions are possible.
|
||||
case
|
||||
when seatsmax = 385 and not ( seatsocc > 380 and seatsocc <> 379 or seatsocc <= 120 or paymentsum >= 200000 ) then 'A'
|
||||
//Interval comparisons
|
||||
when seatsmax between 250 and 350 and seatsocc not between 1 and 100 then 'B'
|
||||
//Pattern comparisons ('%' -> wildcard character, represents any character string, '_' -> stands for any character)
|
||||
when carrid like '_L' then 'C'
|
||||
//Checking for null and initial value
|
||||
when currency is not null or carrid is not initial then 'D'
|
||||
else '?'
|
||||
end as case4,
|
||||
|
||||
// -------- Built-in functions --------
|
||||
// SQL functions (only a selection is covered here)
|
||||
|
||||
// Numeric functions
|
||||
// The example uses typed and untyped literals only as arguments.
|
||||
abs( abap.int4'-1' ) as nf_abs,
|
||||
ceil( abap.decfloat34'3.333' ) as nf_ceil,
|
||||
floor( abap.decfloat34'3.333' ) as nf_floor,
|
||||
div( 25, 5 ) as nf_div,
|
||||
mod( 11, 3 ) as nf_mod,
|
||||
division( 1, 3, 2 ) as nf_division,
|
||||
round( abap.decfloat34'1.337', 2 ) as nf_round,
|
||||
|
||||
// String functions
|
||||
concat(planetype, '-#') as sf_concat,
|
||||
concat_with_space(carrid, '#', 1) as sf_conc_ws, //3rd argument: number of spaces
|
||||
instr(currency, 'U') as sf_instr, //Position of the first occurrence of the string from the substring in the argument (case-sensitive)
|
||||
left(currency, 2) as sf_left, //String of length n starting from the left of an expression
|
||||
length(planetype) as sf_len, //Number of characters in an argument ignoring trailing blanks
|
||||
lower(carrid) as sf_lower, //String with a length of an argument in which all uppercase letters are transformed to lowercase letters
|
||||
lpad(carrid, 5, '#') as sf_lpad, //String of a length with the right-aligned content of an argument without trailing blanks and in which leading blanks produced by the expanded string are replaced by the characters from an argument (respecting all blanks)
|
||||
ltrim(planetype, 'A') as sf_ltrim, //String with the content of an argument in which all trailing blanks and leading characters are removed that match the specified character.
|
||||
replace(currency, 'U', '#') as sf_repl, //String in which all instances of the second argument are replaced by the content from the third argument
|
||||
replace_regexpr(PCRE => '\\d', //More optional parameters are possible; the example replaces all digits
|
||||
VALUE => planetype,
|
||||
WITH => '#',
|
||||
RESULT_LENGTH => 10) as sf_repl_regex,
|
||||
right(currency, 2) as sf_right, //String of length n starting from the right of an expression
|
||||
rpad(carrid, 5, '#') as sf_rpad, //See lpad; here, right-algined content
|
||||
rtrim(planetype, '0') as sf_rtrim, //See ltrim; here, from the right
|
||||
substring(planetype, 4, 3) as sf_sub, //Returns a substring; second argument: position from where to start; third argument: length of the extracted substring
|
||||
upper( 'abap' ) as sf_upper, //Transforms to upper case
|
||||
|
||||
// Coalesce function
|
||||
// Checks whether the argument contains a null value. If it contains it, it returns the value of the second argument
|
||||
// Otherwise, it returns the value of the first argument.
|
||||
// This example has no null values in carrid, therefore the carrid value is output. See the example view about joins.
|
||||
coalesce(carrid, 'N') as coalesced,
|
||||
|
||||
// Special functions
|
||||
// Type conversion functions
|
||||
fltp_to_dec( abap.fltp'12.34' as abap.dec(10,1) ) as fltp2dec,
|
||||
|
||||
// Unit conversion
|
||||
// In the following example, the number that is input as parameter is used as the value for a distance in miles. It is converted to kilometers.
|
||||
@Semantics.quantity.unitOfMeasure: 'kilometers'
|
||||
unit_conversion( quantity => $parameters.p_smax,
|
||||
source_unit => abap.unit'MI',
|
||||
target_unit => $projection.kilometers ) as converted_value,
|
||||
|
||||
// Date/Time functions
|
||||
// The function in the example calculates the days between to dates. The actual parameters must have the built-in data type DATS.
|
||||
dats_days_between(fldate,$projection.date_lit) as days_bw1,
|
||||
|
||||
// The following example is similar to the example above. Here, the function expects the type DATN.
|
||||
// It also shows the use of another function (dats_to_datn) that converts the types.
|
||||
// Note: Only literals can be passed to the final two parameters on_error, on_initial.
|
||||
datn_days_between(dats_to_datn(fldate,'INITIAL','INITIAL') , dats_to_datn($session.user_date,'INITIAL','INITIAL')) as days_bw2,
|
||||
|
||||
// The following function adds days to a date. Here, the date a week from today is calculated.
|
||||
dats_add_days($session.user_date,7,'INITIAL') as in1week,
|
||||
|
||||
// Time stamp functions
|
||||
// Getting the current time stamp
|
||||
utcl_current() as ts,
|
||||
// Adding seconds to a time stamp
|
||||
utcl_add_seconds($projection.ts,60) as in1minute
|
||||
}
|
||||
// -------- Clauses for the SELECT statement --------
|
||||
// SELECT statements of a CDS view entitiy can be specified with optional clauses
|
||||
// Among them, there are WHERE (to restrict the rows of the result set), GROUP BY (grouping the result set(
|
||||
// HAVING (further restriction after the GROUP BY clause) clauses and other set operators like EXCEPT, INTERSECT and UNION.
|
||||
// This example uses a simple WHERE clause. It uses a condition that includes the input parameter to restrict the result set.
|
||||
where seatsmax > $parameters.p_smax
|
||||
19
src/zdemo_abap_cds_ve_sel.ddls.baseinfo
Normal file
19
src/zdemo_abap_cds_ve_sel.ddls.baseinfo
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"BASEINFO":
|
||||
{
|
||||
"FROM":
|
||||
[
|
||||
"ZDEMO_ABAP_FLI"
|
||||
],
|
||||
"ASSOCIATED":
|
||||
[],
|
||||
"BASE":
|
||||
[],
|
||||
"ANNO_REF":
|
||||
[],
|
||||
"SCALAR_FUNCTION":
|
||||
[],
|
||||
"VERSION":0,
|
||||
"ANNOREF_EVALUATION_ERROR":""
|
||||
}
|
||||
}
|
||||
13
src/zdemo_abap_cds_ve_sel.ddls.xml
Normal file
13
src/zdemo_abap_cds_ve_sel.ddls.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<abapGit version="v1.0.0" serializer="LCL_OBJECT_DDLS" serializer_version="v1.0.0">
|
||||
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
|
||||
<asx:values>
|
||||
<DDLS>
|
||||
<DDLNAME>ZDEMO_ABAP_CDS_VE_SEL</DDLNAME>
|
||||
<DDLANGUAGE>E</DDLANGUAGE>
|
||||
<DDTEXT>CDS view entity: Operands and expressions</DDTEXT>
|
||||
<SOURCE_TYPE>W</SOURCE_TYPE>
|
||||
</DDLS>
|
||||
</asx:values>
|
||||
</asx:abap>
|
||||
</abapGit>
|
||||
Reference in New Issue
Block a user