Dont Try Coding Abap Core Data Service Without This
Dont Try Coding Abap Core Data Service Without This
ABAP for HANA SAP HANA
March 18, 2017
As we described in the prior blog The ABAP Developer Road Map to SAP
HANA, with the advent of SAP HANA, there has been a paradigm shift in
the way business applications are developed. The rule-of-thumb is simple:
Do as much as you can in the database to get the best performance. This
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 1/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
was coined as “Code Pushdown” by SAP. Well, this is also true for the
underlying data models of the business applications.
HANA CDS: the database language that can be used to create tables,
views, and structures on the HANA database itself. Views created in HANA
can be consumed from the Netweaver AS using Native SQL.
ABAP CDS: made available with SAP Netweaver 7.40 SP5, is a valuable tool
to have when programming for HANA. However, ABAP CDS can be used
even if the underlying database is not a HANA database, as it is an open
DDL that is supported by many traditional databases as well. ABAP CDS is
usually the best choice when designing and creating database views that
will need to access the HANA database, and this will be the prime focus of
this blog.
ABAP CDS uses an SQL-like syntax, enhanced with some useful additional
features. Like any typical ABAP object, ABAP CDS les are also
transportable between Netweaver AS systems, which is an advantage
ABAP CDS has over its HANA CDS counterpart. Once transported, an ABAP
CDS View will create and deploy the corresponding database view on the
target database automatically (requiring no additional steps for the
developer or transport manager).
Unlike classical SE11 views, ABAP CDS views can only be created in Eclipse
(by utilizing the ABAP Development Tools for SAP Netweaver add-on for
Eclipse). The tools can be found at https://tools.hana.ondemand.com/.
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 3/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
Step 3: Search for Core Data Services Folder and select Data De nition
(depending on ABAP development tools version, this may say DDL Source)
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 4/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
Step 4: Select the transport to which you want the DDL source le
attached. Once this has been done you can either select “Finish”, which
will automatically select the basic CDS view or you can select “Next”, which
will bring you to a list of different DDL source le templates to choose
from. We will choose NEXT, so we can expore the differnet Templates.
Step 5: Choose the template you would like to begin with. For the rst
example, we will be starting with the basic view, which creates a CDS
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 5/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
Entity beginning with the “De ne View” keywords. Table functions are an
option as well, but that will be covered in the future AMDP blog.
This rst CDS View example will be a simple SELECT of some elds from
the SNWD_SO table, in addition to a generated eld that will illustrate the
CASE capabilities of CDS. This view will also illustrate the basic structure
used for DDL source les. Take a look at the screen-shot below. We will go
into each of the red boxed subsections in detail.
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 6/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
SQL View Name is ‘zjontestview1’. While this SQL View name should NOT
be used in your ABAP programs, it can be used in SE11 to display details
about the current view (similar to how one would view a classical view).
This section will usually come after the opening Annotations but before
the rst curly bracket. Within this section, the developer speci es:
After the rst curly bracket comes the desired elds from the table, as well
as any elds that are to be computed. This is the section that a developer
will primarily utilize to take advantage of code-pushdown to allow the
database to perform calculations. Our example illustrates the ease of
using aliases for elds, as well as the New ABAP CASE Construct. The CASE
Construct allows a particular value to be returned based on the value of a
table eld. In our example, we are converting the billing status indicator
into its real-world English meaning. If the billing status is ‘P’ for the current
record, our calculated eld payment_status will have the value ‘Paid’. If the
eld is blank, the eld will be ‘Unpaid’. Finally, if the eld is some
unexpected value, the payment_status eld will be a question mark.
The nal section of the DDL source le is where you would add any
selection restrictions (such as a where clause) in addition to any
aggregation instructions (such as GROUP BY). We will be covering both of
these options in later examples.
Ok now that we have created a basic CDS view for the table snwd_so table,
let’s check our work by a SQL preview with Eclipse.
CDS Views can be previewed right within the Eclipse editor. To do this,
right-click on the created view, select “Open With” and then select “Data
Preview” from the submenu. (Note that in other versions of the ABAP
Development Tools, the “Data Preview” option may appear immediately in
the menu when you right click.)
Below is the data preview for the example CDS View above,
ZJON_CDS_VIEW_EXAMPLE:
Notice that, along with the data we have selected from the snwd_so table,
we also have the calculated eld payment_status that was created using
the CASE function.
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 8/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
Next, we will look at an example of how to use the example CDS view
within an ABAP Program. This simply requires using an Open SQL
statement that selects from our CDS Entity (ZJON_CDS_VIEW_EXAMPLE).
As an added bene t of ABAP CDS, a generated CDS Entity name can also
be used in DATA declarations to create structures of a compatible TYPE.
(ZJON_CDS_EXAMPLE_CONSUME – Program)
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 9/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
There you have it! Your rst CDS View and an example ABAP Report that
consumes it. What? Can we extend the view if new z- elds are needed?
Funny you should ask…
Another nice feature of CDS views is the ability to enhance them. This
feature is bene cial when you want to augment an existing view, such as
those that come with the standard SAP solution. We often need to modify
SAP objects with custom Z elds, and view extension present a simple and
transportable way to do this. To extend a view, follow the same as above
when creating a basic view. However, when you arrive at the “New Data
De nition” screen, select “Extend View” instead of “De ne View”.
This template will allow you to select an existing CDS View to extend, and
specify the new name for the view being created:
@AbapCatalog.sqlViewAppendName: 'sql_view_append_name'
@EndUserText.label: 'EXTENDO'
extend view view_name with Zextedn_Exmpl {
data_source_name.element_name
}
@AbapCatalog.sqlViewAppendName: ‘${sql_view_append_name}’
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 10/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
This view extension will append our new eld to the view it is extending
(ZJON_CDS_VIEW_EXAMPLE) and append the new eld SO_STATUS. The
new eld SO_STATUS is calculated based on two qualifying criteria: when
the gross_amount for the sales order is greater than $100000 and the
billing status is not ‘P’. This extended view displays exactly like the original
view example, with the addition of the required “High Impact” indicator.
Below is the output of this view, executed by running the “Data Preview” in
Eclipse.
Once enhanced, our extending eld will be available during all SELECTS
against the ZJON_CDS_VIEW_EXMAPLE table.
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 11/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
The view used to accomplish this will again utilize our initial example
ZJON_CDS_VIEW_EXAMPLE. It performs a SELECT from this view while
utilizing an “Association” to map in the corresponding company from the
snwp_bpa table with the matching buyer ID eld.
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 12/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
Associations are essentially reusable JOINS that relate two CDS Entities
(tables and views) to each other. CDS Associations have the added bene t
of being able to specify cardinality ([1..1], [0..1], [*], etc.). Many of the features
of ASSOCIATIONS are available as JOINS, however, ASSOCIATIONS are the
preferred Best Practice and more elegant option when merging two CDS
entities.
The aggregate functions available in CDS Views are the same ones that are
available in the new Open SQL. Although they are readily available during
a regular Open SQL Select (NO Core Data Services), it is still an extremely
useful tool to have in CDS Views. Using the CDS approach, developers can
make certain summary data is uniform across projects and systems
without having to maintain entirely separate summary level tables.
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 13/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
Now, we will bring this all together with a sample ABAP program that
utilizes our new CDS View. This program will also illustrate how to call a
CDS View with the parameter we have speci ed. take a look at the code
below…
*&------------------------------------------------------------------
*& Report zjon_cds_example_para_assoc
*&------------------------------------------------------------------
*&
*&------------------------------------------------------------------
REPORT zjon_cds_example_para_assoc.
Notice, to perform a SELECT from a CDS View with a parameter, the CDS
Entity name must be immediately followed by an open parenthesis (no
spaces) and a closed parenthesis. Within the parenthesis, the parameter
must be assigned a compatible value. This value can be hard coded or a
variable preceded by the ‘@’ Escape symbol. This is similar to executing a
method in ABAP OO with importing parameters.
OK, let’s run the program. We get the expected SELECTION screen. We will
$10,000.00 as our threshold.
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 15/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
Generally speaking, ABAP developers should make very little use of HANA
CDS when compared to ABAP CDS or Open SQL. ABAP CDS provides
many of the features available in HANA CDS in a much simpler fashion,
with the added bene t of being transportable. There are very few
scenarios where HANA CDS would be a better option. Two scenarios where
HANA CDS may have to be necessary used are:
If you are using HANA as a secondary database, then ABAP CDS will simply
not work. That is because ABAP CDS works with the data dictionary and
assumes that all views are coming from the primary DB and not a
secondary DB. As of yet, there is no modi er to specify a secondary DB
instead
There is some Native SQL function not available in ABAP CDS you
require.
AS ABAP on HANA becomes the standard, this scenario will become the
more likely reason someone may opt to use HANA CDS vs ABAP CDS.
While ABAP CDS and HANA CDS are being closely developed, there may
be some branching in their functionalities. This is because ABAP CDS’ chief
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 16/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
purpose is to serve as an Open Source DDL for all databases, while HANA
CDS is designed solely to model data on the HANA side.
Prerequisites: You have a SAP HANA System up and running. You are
connected to the HANA system through Eclipse (with SAP Development
Perspective). You have permissions to create views and run SQL on the
SYSTEM schema. In addition, your SAP Netweaver AS system is connected
to this HANA DB (as either a primary DB or a secondary DB).
Step 2: Navigate to the Project Explorer tab (should be on the top left of
the screen, unless you have rearranged your layout)
Step 3: Navigate to the SAP HANA Folder and hit the drop down. Select XS
Project and hit next
Step 5: Select the desired workspace that utilizes your SAP HANA system.
If a workspace doesn’t exist that meets this criterion, hit “Add Workspace”
and then select your HANA system on the next screen
Step 6: For the purposes of this example, you can deselect both Access
Objects on the following screen and hit Finish
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 17/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
Step 7: You should see your new project appear in the Project Explorer tab.
Right-click this tab and select New->Other again.
Step 9: Name your le, select your package, and hit Finish
From here you should see a very basic “Context” layout for you to begin
with. You should ensure your @SCHEMA annotation says ‘SYSTEM’ after it,
or take note of the current schema to ensure your ABAP code is
SELECTING from the correct one.
There are instances that would require many more changes to transfer an
ABAP CDS to HANA CDS, however, it should be reassuring to developers
that knowing ABAP CDS will make learning HANA CDS much easier.
The dif culty in using HANA CDS does not lie in the HANA CDS view
creation itself, but within the ABAP Program source that needs to use
some form of Native SQL to access it. As you will see below, Native SQL is
much less elegant than Open SQL, much harder to read, and generally a
headache one would want to avoid if possible.
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 18/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
– This was a very simple SELECT scenario (made simpler by the fact
we’re using CDS Views). Even a slightly more complicated SELECT
would make this Native SQL SELECT much worse!
*&------------------------------------------------------------------
*& Report zjon_native_sql_exec
*&------------------------------------------------------------------
*&
*&------------------------------------------------------------------
REPORT zjon_native_sql_exec.
TRY.
CONCATENATE 'SELECT "Salesorder", "buyer_id","PAYMENT_STATUS",
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 19/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
ENDTRY.
And the output (which is very similar to our rst examples output):
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 20/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
As you can see, HANA CDS requires much more effort to implement, then
ABAP CDS. For this reason, HANA CDS should be avoided unless there is
an absolute need to use it.
SUMMARY
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 21/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
IT Partners
∗ First Name
∗ Last Name
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 22/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
By submitting this form, you are consenting to receive marketing emails from: IT Partners. You can revoke your
consent to receive emails at any time by using the SafeUnsubscribe® link, found at the bottom of every email.
Emails are serviced by Constant Contact.
Sign Up!
RELATED POSTS
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
A simple message in order to say thank you for this blog. It is awesome.
Regards,
△ ▽ • Reply • Share ›
Anthony Cecchini Mod > Marc-Antoine • 9 months ago
Thank you so much for your kind comment! We write these blogs with the hope that it
ill h l di i t t h l
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/
d b bl t t k d t f th 23/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
will help someone dive in to a new technology and be able to take advantage of the
many great features SAP offers. So it means it a great deal that you found it useful.
Please feel to reach out if you have any questions or comments regarding other blog
topics.
△ ▽ • Reply • Share ›
Thank you so much for your kind comment! We write these blogs with the hope that it
will help someone dive in to a new technology and be able to take advantage of the
many great features SAP offers. So it means it a great deal that you found it useful.
Please feel to reach out if you have any questions or comments regarding CDS or
other blog topics.
Thanks again!
-Jon
△ ▽ • Reply • Share ›
IT Partners blog makes the Feedspot best Debugging ABAP for Functional Teams –
of the best SAP Blog List Part 1
3 comments • a year ago 1 comment • 6 years ago
Mithun R — Great post.. Oracle Training in Chennai — Thanks to
Avatar AvatarShare the QTP Material for
Freshers,qtptrainingchennai
Don’t Try Coding ABAP Core Data New Features in ABAP 7 4 Declaring and
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 24/25
9/20/2019 Don't Try Coding ABAP Core Data Services Without Reading This First
IT Partne
Septembe
IT Partne
Septembe
Since 1993, IT Partners has been providing reliable, cost-effective solutions
to meet our customer’s goals and objectives in the Commercial and
Federal ERP Marketplace.
AFCEA
Privacy Policy
Copyright © 2019 ITPSAP. All Rights Reserved. Designed by ArachnidWorks, Inc.
https://itpsap.com/dont-try-coding-abap-core-data-services-without-reading-this-first-2/ 25/25