Вы находитесь на странице: 1из 24

CODEJAM: ABAP FOR SAP HANA

4 – OPENSQL EXTENSIONS IN
SAP NETWEAVER AS ABAP

June 2015 – SAP SE

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

The incorporation of new features has a couple of implications:


· Usage of new features requires the usage of the new OpenSQL syntax throughout the statements
· Usage of the new OpenSQL syntax implies a so-called “strict mode”, wherein nearly everything that
formerly resulted in a syntax warning now results in a syntax error in OpenSQL statement (of course,
only if you develop something new!).

ABAP for SAP HANA


3

B. What’s inside this exercise?

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:

1. Using the new OpenSQL syntax:


a. Escaping of host variables with “@”
b. Comma separated field list
2. Using CASE and COALESCE constructs
3. Generic Existence Check
4. Circumvent offset-/length access via a CDS view
5. Calling CDS view with parameter

C.1 Simple OpenSQL with the new syntax


In this exercise we will implement a simple report using the new OpenSQL syntax.

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)

2. Alternatively use the Open


ABAP Development
Object Dialog

ABAP for SAP HANA


4

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.

Add coding to display the


content of the internal table
(based on methods of the
demo class
cl_demo_output).

4. Save, activate, and execute


the report.

ABAP for SAP HANA


5

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

from CDS entity:


· sepm_sddl_salesorde
r_head

For simplicity restrict the result


set to 10 rows.

Add coding to display the


content of the internal table
(based on methods of the
demo class
cl_demo_output).

6. If you are curios how the CDS


view look like please feel free
to navigate to the source code.
Simply press F3 on the CDS
entity
sepm_sddl_salesorder_h
ead.

event

ABAP for SAP HANA


6

Explanation Screenshot

7. Save, activate, and execute


the report.

Code snippet: ZR_OSQL_1_SIMPLE


REPORT zr_osql_1_simple_sol.

* Usage of new Open SQL syntax


SELECT so_id,
currency_code,
gross_amount
FROM snwd_so
INTO TABLE @DATA(lt_so)
UP TO 10 ROWS.

* *** Write and display the data ***


cl_demo_output=>write_data(
name = 'Exercise 1.1 - Simple OpenSQL using New Syntax'
value = lt_so ).

* Consumption of a CDS view


SELECT sales_order_id,
currency_code,
gross_amount
FROM sepm_sddl_salesorder_head
INTO TABLE @DATA(lt_so_via_cds)
UP TO 10 ROWS.

* *** Write and display the data ***


cl_demo_output=>write_data(
name = 'Exercise 1.2 - Simple OpenSQL consuming a CDS view'
value = lt_so_via_cds ).

cl_demo_output=>display( ).

ABAP for SAP HANA


7

C.2 Using various new features


In this exercise you will use various new OpenSQL features like, the CASE construct in the field list,
using LEFT OUTER JOIN in combination with the built-in function COALESCE.

Explanation Screenshot

1. In the ABAP perspective of


your Eclipse IDE, navigate to
your exercise package
TEST_A4H_EX_##
and open report
ZR_OSQL_2_CONDITIONALS_
## (where ## represents your
group number).

2. Alternatively use the Open


ABAP Development Object
Dialog

3. Enhance the given OpenSQL


statement with a simple case:

CASE
· delivery_status

ABAP for SAP HANA


8

Explanation Screenshot

4. Enhance the given OpenSQL


statement with a Searched
Case:

CASE
· gross_amount as
order_volume_category

5. Enhance the given OpenSQL


statement with a LEFT OUTER
JOIN

LEFT OUTER JOIN


· sepm_sddl_so_invoice
_header

ON
· sales_order_key

ABAP for SAP HANA


9

Explanation Screenshot

6. Enhance the given OpenSQL


statement with the COALESCE
built-in function

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!

7. Save, activate, and execute the


ABAP report

ABAP for SAP HANA


10

Explanation Screenshot

Code snippet: ZR_OSQL_2_CONDITIONALS

REPORT zr_osql_2_conditionals_##.

* Demonstration of CASE (simple and searched) and COALESCE constructs


SELECT
sales_order_id,
so~currency_code,
so~gross_amount,

* 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,

* Coalesce: payment status or "ToBeInv - To be Invoiced"


COALESCE( inv~payment_status, 'ToBeInv' ) AS payment_status

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

INTO TABLE @DATA(lt_data)


UP TO 10 ROWS
WHERE inv~payment_status IS NULL
.

* *** Write and display the data ***


cl_demo_output=>write_data(
name = 'Exercise 2 - Demonstration of CASE (simple & searched) and the COALESCE construct!'
value = lt_data ).

cl_demo_output=>display( ).

ABAP for SAP HANA


11

C.3 Generic existence check

Show capability of generic existence check using literal values in Open SQL.

Explanation Screenshot

1. In the ABAP perspective of


your Eclipse IDE, navigate to
your exercise package
TEST_A4H_EX_##
and open report
ZR_OSQL_3_EXISTENCE_CH
ECK_## (where ##
represents your group
number).

2. Alternatively use the Open


ABAP Development
Object Dialog

ABAP for SAP HANA


12

Explanation Screenshot

3. Implement a generic existence


check using:

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

4. Save, activate, and execute


the ABAP report

ABAP for SAP HANA


13

Explanation Screenshot

Code snippet: ZR_OSQL_3_EXISTENCE_CHECK

REPORT zr_osql_3_existence_check_##.

PARAMETER: p_custid TYPE snwd_partner_id VALUE CHECK DEFAULT '0100000000'.

DATA lv_this_years_timestampl TYPE timestampl VALUE '20150101000000.0000000'.


DATA lv_exists TYPE abap_bool VALUE abap_false.

* Usage of host variable @abap_true (value 'X')


* Allows for a generic existence check without
* Knowing the structure of the tables, views, etc.
SELECT SINGLE @abap_true
FROM sepm_sddl_salesorder_head AS so
JOIN sepm_sddl_businesspartner AS customer
ON so~buyer_key = customer~business_partner_key
INTO @lv_exists
WHERE customer~business_partner_id = @p_custid
AND so~created_at >= @lv_this_years_timestampl.

* *** Write and display the data ***


cl_demo_output=>write_data(
name = 'Exercise 3 - Generic Existence Check!'
value = lv_exists ).

cl_demo_output=>display( ).

C.4 Circumvent Select ... Check... Endselect pattern

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

1. In the ABAP perspective of


your Eclipse IDE, navigate to
your exercise package
TEST_A4H_EX_##
and open report
ZR_OSQL_4_CHECK_PATTER
N_## (where ## represents
your group number).

ABAP for SAP HANA


14

Explanation Screenshot

2. Alternatively use the Open


ABAP Development
Object Dialog

3. Patterns like SELECT …


ENDSELECT including a
CHECK are a “typical pattern”
but actually “bad practise”.

4. To bypass the SELECT …


ENDSELECT … CHECK
pattern we will call CDS View.
Open DDL source
zcdsv_so_info_##
by using Ctrl-Shift-A
(where ## represents
your group number).

ABAP for SAP HANA


15

Explanation Screenshot

5. You will see the use of the


build-in function substring ( …
) in the CDS view.

6. Please feel free to execute


the report.

But be careful: The runtime


might be quite long
dependent on the amount
of data in the system!

Uncomment or delete the


SELECT … ENDSELCT
statement and implement a
simple SELECT from the
given CDS view
zcdsv_so_info_## by
using a where clause to filter
on specific products.

7. Change the importing


parameter value to the
internal table used in the
above inline data declaration
e.g. lt_so_via_cds.

ABAP for SAP HANA


16

Explanation Screenshot

8. Save, activate, and execute


the ABAP report.

Code snippet: ZR_OSQL_4_CHECK_PATTERN

*SELECT so_i~sales_order_key, prod~product_id


* FROM sepm_sddl_salesorder_item AS so_i
* INNER JOIN sepm_sddl_product AS prod
* ON so_i~product_key = prod~product_key
* INTO @DATA(ls_prod).
*
* CHECK ls_prod-product_id+3(4) = '1107'. "software with bugs
*
** For those sales orders, get sales order ID
* SELECT DISTINCT so~sales_order_id
* FROM sepm_sddl_salesorder_head AS so
* APPENDING TABLE @lt_so
* WHERE so~sales_order_key = @ls_prod-sales_order_key.
*ENDSELECT.

* Circumvent offset-/length by simply access via a CDS view


SELECT sales_order_id
FROM zcdsv_so_info_sol
INTO TABLE @DATA(lt_so_via_cds)
UP TO 10 ROWS
WHERE product_id_abbrev = '1107'.

* *** Write and display the data ***


cl_demo_output=>write_data(
name = 'Exercise 4 - Circumvent Check Pattern!'
value = lt_so_via_cds ).

cl_demo_output=>display( ).

ABAP for SAP HANA


17

C.5 Consuming CDS views with parameters


Explanation Screenshot

1. In the ABAP perspective of


your Eclipse IDE, navigate to
your exercise package
TEST_A4H_EX_##
and open report
ZR_OSQL_5_CDSV_PARAMET
ERS_## (where ##
represents your group
number).

2. Alternatively use the Open


ABAP Development
Object Dialog

ABAP for SAP HANA


18

Explanation Screenshot

3. Execute the report see the


result of the SQL statement

4. Close the result window and


navigate back to you source
code.

What is the SQL statement


doing?

a) First, the SQL is selecting


data from the CDS view

zcdsv_prod_count_##

of a specific customer.

b) Navigate to the CDS view:

The CDS view is selecting


data from the

sepm_sddl_sales_order
_item

view counting all products

ABAP for SAP HANA


19

Explanation Screenshot

a) Navigate back to the


report:

Second, SQL statement is


using

LEFT OUTER JOINs

to select the description

product_name~text

of a product for a specific


language:

(1) logon language


sy-langu
(2) default language
'E'
5. Let’s simply the SQL
statement while using
another CDS view with
parameters!

ABAP for SAP HANA


20

Explanation Screenshot

6. Open CDS view

zcdsv_prod_info_##

by using e.g. Ctrl-Shift-A.

You will see an enhanced


view definition

with parameters.

The field list is using the

coalesce

function together with an


association to select the
product description for the
logon language using the
parameter

$parameters.langu

and as a fallback the default


language 'English '

7. Navigate back to the report.

Implement a simple SELECT


from CDS view
zcdsv_prod_info_##
(where ## represents your
group number).

while specifying parameter


langu = @sy-langu

Code snippet: ZR_OSQL_5_CDSV_PARAMETERS.

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.

ABAP for SAP HANA


21

Explanation Screenshot

8. You will see a syntax warning


indicating that you are using a
Database features which is
not implemented on all
databases.

This is why this warning is


produced.

This message can be handled


using a pragma:

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

Don’t forget to add the

ELSE

clause and of course the

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

ABAP for SAP HANA


22

Explanation Screenshot

12. Save, activate, and execute


the ABAP report.

ABAP for SAP HANA


23

Explanation Screenshot

Code snippet: ZR_OSQL_5_CDSV_PARAMETERS.

* 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_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.

* *** Write and display the data ***


cl_demo_output=>write_data(
name = 'Exercise 5 - Consumption of CDS view with parameters!'
value = lt_product_via_cds ).

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-

ABAP for SAP HANA


© 2013 by SAP AG. All rights reserved.
SAP and the SAP logo are registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo are trademarks or
registered trademarks of Business Objects Software Ltd. Business Objects is an SAP company.Sybase and the Sybase logo are registered trademarks of Sybase Inc.
Sybase is an SAP company such products and services, if any. Nothing herein should be construed as constituting an additional warranty .

Вам также может понравиться