Академический Документы
Профессиональный Документы
Культура Документы
4 – OPENSQL EXTENSIONS IN
SAP NETWEAVER AS ABAP
This document outlines our general product direction and should not be relied on in making a purchase decision. This
document is not subject to your license agreement or any other agreement with SAP. SAP has no obligation to pursue
any course of business outlined in this document or to develop or release any functionality mentioned in this
document. This document and SAP's strategy and possible future developments are subject to change and may be
changed by SAP at any time for any reason without notice. This document is provided without a warranty of any kind,
either express or implied, including but not limited to, the implied warranties of merchantability, fitness for a particular
purpose, or non-infringement. SAP assumes no responsibility for errors or omissions in this document, except if such
damages were caused by SAP intentionally or grossly negligent.
2
Table of Contents
A. What is OpenSQL? ................................................................................................................................... 2
B. What’s inside this exercise? ...................................................................................................................... 3
C1. Simple OpenSQL with the new syntax .................................................................................................... 3
C2. Using various new features..................................................................................................................... 7
C5. Generic existence check ....................................................................................................................... 11
C3. Circumvent Select ... Check... Endselect pattern ................................................................................. 13
C4. Consuming CDS views with parameters ............................................................................................... 17
D. Summary ................................................................................................................................................ 23
A. What is OpenSQL?
OpenSQL in the SAP NetWeaver AS ABAP, respectively in the ABAP language, is a database abstraction
layer with an SQL-like syntax. This is not a unique feature; other languages provide a DB abstraction layer
with an SQL-like syntax as well. The most important fact is that OpenSQL defines a common SQL semantic
for all SAP supported databases and is not just the subset of SQL commands common to all databases. The
common semantic for all supported DBs ensures that the result of an OpenSQL query is independent of the
underlying database. In the development of business application logic, this in principle allows to exchange
the underlying database management system while maintaining the same business logic.
The latest SQL standard defines a long list of syntax for features that are not yet available in OpenSQL. For
example, OpenSQL currently does not support expressions, has only limited support for JOINs, does not
support UNION or UNION ALL, and so on. However, as we just learnt, OpenSQL defines a common
semantic for the incorporated SQL features, so new feature do not only require syntax enhancements but to
also agree upon common semantics. Despite these obstacles, a vast set of new features have been
provided in OpenSQL. A glimpse into this new feature set is given in this exercise.
In particular, you’ll have a closer look today at the new OpenSQL syntax comprising:
· Escaping of host variables with the @ sign
· Comma separated element list
· Conditional Expressions (CASE)
· Using various new features
SAP NetWeaver AS ABAP not only provides code pushdown functionality via the new features like CDS
Views or ABAP Managed Database Procedures (see the corresponding exercises for more details), but also
provides new, enhanced OpenSQL features, which also provide the possibility for code pushdown – at least
up to a certain level. You have already seen a list of new features, among which the following are going to
be shown in this exercise:
Explanation Screenshot
1. In the ABAP perspective of
your Eclipse IDE, navigate to
your exercise package
TEST_A4H_EX_##
and open report
ZR_OSQL_1_SIMPLE_##
(where ## represents your
group number)
Explanation Screenshot
3. Implement a simple OpenSQL
SELECT statement using the
host variable (escaped with @)
and a comma-separated field
list.
SELECT
· so_id
· currency_code
· gross_amount
FROM (table)
· snwd_so
INTO
· Using the inline DATA
declaration statement.
UP TO
· For simplicity restrict the
result set to 10 rows.
Explanation Screenshot
5. On the second part implement
an OpenSQL statement to
consume data from a CDS
view.
select:
· sales_order_id
· currency_code
· gross_amount
event
Explanation Screenshot
cl_demo_output=>display( ).
Explanation Screenshot
CASE
· delivery_status
Explanation Screenshot
CASE
· gross_amount as
order_volume_category
ON
· sales_order_key
Explanation Screenshot
COALESCE
· as payment_status
WHERE
· inv~payment_status
Comment:
Due the left outer join it might
happen that you are getting
NULL values from the right
hand dataset. Therefore we are
using the COALESCE function
to explicitly set a value for the
NULL values!
Explanation Screenshot
REPORT zr_osql_2_conditionals_##.
* Simple case
CASE delivery_status
WHEN ' ' THEN 'OPEN'
WHEN 'D' THEN 'DELIVERED'
ELSE delivery_status
END AS delivery_status,
* Searched case
CASE
WHEN so~gross_amount < 100
THEN 'S'
WHEN so~gross_amount >= 100 AND
so~gross_amount < 1000
THEN 'M'
WHEN so~gross_amount >= 1000
THEN 'L'
END AS order_volume_category,
FROM sepm_sddl_salesorder_head AS so
LEFT OUTER JOIN sepm_sddl_so_invoice_header AS inv
ON so~sales_order_key = inv~sales_order_key
cl_demo_output=>display( ).
Show capability of generic existence check using literal values in Open SQL.
Explanation Screenshot
Explanation Screenshot
SELECT
· SINGLE @abap_true
FROM
· sepm_sddl_salesorde
r_head
ON
· buyer_key =
business_partner_ke
y
INTO
· @lv_exists
WHERE
· customer~business_p
artner_id =
@p_custid
· created_at >=
@lv_this_years_time
stampl
Explanation Screenshot
REPORT zr_osql_3_existence_check_##.
cl_demo_output=>display( ).
Patterns like SELECT ... ENDSELECT including a CHECK are "bad practice" but might have been
necessary if offset/length access has been needed. This exercise shows how to circumvent that pattern
while using a CDS view and the the substring function in CDS (available as of NetWeaver 7.40 SP8).
Explanation Screenshot
Explanation Screenshot
Explanation Screenshot
Explanation Screenshot
cl_demo_output=>display( ).
Explanation Screenshot
zcdsv_prod_count_##
of a specific customer.
sepm_sddl_sales_order
_item
Explanation Screenshot
product_name~text
Explanation Screenshot
zcdsv_prod_info_##
with parameters.
coalesce
$parameters.langu
SELECT *
FROM zcdsv_prod_info_00( langu = @sy-langu )
INTO TABLE @DATA(lt_product_via_cds)
WHERE customer_id = @p_custid
ORDER BY product_cnt DESCENDING.
Explanation Screenshot
DB_FEATURE_MODE[VIEWS_
WITH_PARAMETERS]
9. So implement an check if the
feature is supported by the
underlying database using
class:
cl_abap_dbfeatures
static method:
use_features
component:
views_with_parameter
10. If the feature is supported by
the underlying database
implement the call of the CDS
view with parameters
ELSE
ENDIF
statement.
11. And don’t forget to change
the value parameter of the
cl_demo_output method call
to the internal table of the
implemented SQL statement
Explanation Screenshot
Explanation Screenshot
##DB_FEATURE_MODE[VIEWS_WITH_PARAMETERS]
SELECT *
FROM zcdsv_prod_info_sol( langu = @sy-langu )
INTO TABLE @DATA(lt_product_via_cds)
WHERE customer_id = @p_custid
ORDER BY product_cnt DESCENDING.
ELSE.
SELECT cnt~customer_id AS customer_id,
cnt~prod_cnt AS product_cnt,
COALESCE( product_name_sylangu~text,
product_name_default~text ) AS product_name
.........
.........
.........
.........
ENDIF.
cl_demo_output=>display( ).
D. Summary
You have successfully finished the OpenSQL exercise. You used the new OpenSQL syntax, e.g. the
escaping of host variables and the comma separation in the field list. You implemented some of the new
OpenSQL features like CASE statements, JOIN statements and consuming CDS views with parameters and
so on.
Last but not least you saw how easy it is to simplify OpenSQL statements while consuming CDS
views-