Vous êtes sur la page 1sur 24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

SAP YARD

YOUR BACKYARD FOR SAP TECHNICAL TIPS AND SOLUTIONS


HOME

SEE ALL POSTS

TUTORIALS

CODE SNIPPETS

KNOW THY YARD MEN

HELP FORUM

BOOKS & JOBS

SAP QUIZ
246
Shares

ABAP on SAP HANA. Part VI. New Age


Open SQL ABAP 740
TOPICS: Future ABAP

Native And Open SQL In ABAP

New Commands In OpenSQL

Enteremail

Subscribe

We Respect Your Privacy !

SEARCH

New Language Features In ABAP 7.4

Open SQL - ABAP Programming

SAP ABAP Open SQL Overview

SAP News
Follow@sapyard

http://www.sapyard.com/abaponsaphanapartvi/

1/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

SAPYard
656likes

Liked

Youand2otherfriendslikethis

246
Shares

POSTED BY: CHINU

AUGUST 17, 2016

New Age Open SQL ABAP 740


In this article, we would take a break from HANA. We would pause and check
what is available in Open SQL. Why is it called Open? You guessed it
correct!! Open means Open to any Database, i.e. database independent.
You do not need to have HANA database to take advantage of the Open SQL
statements which can improve the way we develop our applications.
If you have been following the previous posts on SAP ABAP on HANA, you
would know that CDS View is another technique to achieve Code to Data
paradigm. If the same functionality can be achieved by both CDS Technique
and Open SQL, which one should we adopt?
Answer: SAP wants us to stay Open. Open SQL is the rst choice. Then
comes CDS View and then the stored procedures (ADBC, ADMP which we
will cover in our subsequent articles).
http://www.sapyard.com/abaponsaphanapartvi/

SAP ABAP LINKS

Abapinho

7MostPopular&FeaturedArticles

SAP HANA from Space


Level
Lazy and Smart ABAPers

DELETING rows of the internal


table within the LOOP. Is it a
2/24

10/30/2016

246
Shares

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

The whole idea of the modern ABAP/SQL/HANA is to push down logic to the
database. We outsource these powerful innovations to put and execute the
logic in the database. But do remember, SAP also wants to be as Open as
possible. So given a choice between database speci c and database
independent solution, always chose the latter (database independent).

Taboo? A big NO NO?

Enough of preaching, let us come to the topic of the day. New Age SQL for
ABAP.

ABAPer

Prior to release 740, if we had the requirement to add an additional column


in the output which did not exist in SAP table with some custom logic, then
we usually wrote something like below.

Zero

We de ned the TYPES. We looped through the table and added the custom
logic (High Purchase or Low Purchase) as shown below.

Part I. First Program in

TYPES: BEGIN OF ty_ekpo,


ebeln TYPE ebeln,
ebelp TYPE ebelp,
werks TYPE ewerk,
netpr TYPE bprei,
pur_type TYPE char14,
END OF ty_ekpo.

DATA: it_ekpo TYPE STANDARD TABLE OF ty_ekpo.

FIELD-SYMBOLS <fs_ekpo> TYPE ty_ekpo.

SELECT ebeln ebelp werks netpr


FROM ekpo
INTO TABLE it_ekpo.

LOOP AT it_ekpo ASSIGNING <fs_ekpo>.

IF <fs_ekpo>-netpr GT 299.
<fs_ekpo>-pur_type = 'High Purchase'.
http://www.sapyard.com/abaponsaphanapartvi/

Fiori App - An
Introduction from an

SAP HANA at Ground

ABAP on SAP HANA.


ABAP HANA
Get Latitude and
Longitude of any place
using Google Map API in
SAP

NetworkedBlogs

Blog:
SAPYard
Topics:
Abap,Sap,Hana

Followourblog

3/24

10/30/2016

246
Shares

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

ELSE.
<fs_ekpo>-pur_type = 'Low Purchase'.
ENDIF.

ENDLOOP.

IF it_ekpo IS NOT INITIAL.


cl_demo_output=>display_data(
EXPORTING
value = it_ekpo
name = 'Old AGE SQL : 1' ).
ENDIF.

WEB DYNPRO ABAP TUTORIALS

DECEMBER 26, 2014

Lets Web Dynpro.


Part V

NOVEMBER 24, 2014

Lets Web Dynpro.


Part IV

NOVEMBER 11, 2014

Lets Web Dynpro.


Part III

NOVEMBER 5, 2014

Lets Web Dynpro.


Part ZZ

NOVEMBER 3, 2014

Lets Web Dynpro.


Part II

Let us see how we can achieve the same thing in a new way. With ABAP 740
and above, we get rid of TYPES, Data Declaration and Loop. Isnt it cool?

http://www.sapyard.com/abaponsaphanapartvi/

4/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

Sample 1 ( Using comma separated elds with inline data declaration


and usage of CASE for reference elds)

SELECT ebeln, ebelp, werks, netpr,


CASE
WHEN netpr > 299
THEN 'High Purchase'
ELSE 'Low Purchase'
END AS pur_type
246 FROM ekpo
Shares INTO TABLE @DATA(lt_sales_order_header).

IF sy-subrc = 0.
cl_demo_output=>display_data(
EXPORTING
value = lt_sales_order_header
name = 'New AGE SQL : 1' ).
ENDIF.

http://www.sapyard.com/abaponsaphanapartvi/

5/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

246
Shares

Outputs from both the above techniques are same. But the path does
matters. Isnt it?
If you have some confusion regarding HANA, check this popular post: SAP
HANA from Space Level.
Next, let us check the powerful inbuilt functions in SELECT.
Sample 2 ( Using JOIN and COUNT / DISTINCT functions in SELECT )

PARAMETERS: p_matnr TYPE matnr,


p_lgort TYPE lgort_d.

SELECT mara~matnr,
http://www.sapyard.com/abaponsaphanapartvi/

6/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

mard~lgort,
COUNT( DISTINCT ( mard~matnr ) ) AS distinct_mat, " Unique Number of Material
COUNT( DISTINCT ( mard~werks ) ) AS distinct_plant, " Unique Number of Plant
SUM( mard~labst ) AS sum_unrest,
AVG( mard~insme ) AS avg_qlt_insp,
SUM( mard~vmspe ) AS sum_blocked
FROM mara AS mara INNER JOIN mard AS mard
ON mara~matnr EQ mard~matnr
INTO TABLE @DATA(lt_storage_loc_mat)
UP TO 1000 ROWS
WHERE mard~matnr = @p_matnr
246 AND mard~lgort = @p_lgort
Shares
GROUP BY mara~matnr,
mard~lgort.

IF sy-subrc = 0.
cl_demo_output=>display_data(
EXPORTING
value = lt_storage_loc_mat
name = 'New AGE SQL : 2' ).
ENDIF.

DISTINCT Material is 1 and DISTINCT Plant is 2. SUM for the Unrestricted


stock is 2, AVG is 2/2 = 1 and SUM of Blocked stock is 2. This is just a sample
to showcase how versatile and powerful the SELECT statement has become.

http://www.sapyard.com/abaponsaphanapartvi/

7/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

246
Shares

Next, in our menu, today is the Mathematical Operators in SELECT. Check


the below snippet where we can directly assign 10 (as rebate percent) which
would be in the internal table. CEIL function, multiplication, subtraction etc
can be handled during the SELECT statement. If we were not in 740, we would
have needed a separate loop and bunch of code to achieve this function. Isnt
ABAP real modern now?
Sample 3 ( Using vivid mathematical operators in SELECT )

DATA: lv_rebate TYPE p DECIMALS 2 VALUE '0.10'.

SELECT ebeln,
10 AS rebate_per,
CEIL( netpr ) AS whole_ord_net,
( @lv_rebate * netpr ) AS rebate,
( netpr - ( @lv_rebate * netpr ) ) AS act_net
FROM ekpo
USING CLIENT '130'
UP TO 10 ROWS
INTO TABLE @DATA(lt_po_data).

http://www.sapyard.com/abaponsaphanapartvi/

8/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

IF sy-subrc = 0.
cl_demo_output=>display_data(
EXPORTING
value = lt_po_data
name = 'New AGE SQL : 3' ).
ENDIF.

246
Shares

Not only Mathematics is fun with ABAP 740, but also logical programming.
Continue below to taste the new avour.
Sample 4 ( Using Complex Case statement on non-referenced elds i.e.
multiple in one Select )

PARAMETERS: p_werks TYPE werks_d.


DATA:
lv_rebate TYPE p DECIMALS 2 VALUE '0.10',
lv_high_rebate TYPE p DECIMALS 2 VALUE '0.30'.

SELECT ebeln,
http://www.sapyard.com/abaponsaphanapartvi/

9/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

werks,
CEIL( netpr ) AS whole_ord_net,
( @lv_rebate * netpr ) AS rebate,
( netpr - ( @lv_rebate * netpr ) ) AS act_net,

CASE WHEN werks = @p_werks " For specific plant


THEN @lv_rebate
ELSE @lv_high_rebate
END AS rebate_type,

CASE WHEN werks = @p_werks " For specific plant


246 THEN 'low rebate'
Shares
ELSE 'high rebate'
END AS low_high

FROM ekpo
USING CLIENT '130'
UP TO 25 ROWS
INTO TABLE @DATA(lt_po_data).

IF sy-subrc = 0.
cl_demo_output=>display_data(
EXPORTING
value = lt_po_data
name = 'New AGE SQL : 4' ).
ENDIF.

http://www.sapyard.com/abaponsaphanapartvi/

10/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

246
Shares

COALESCEs literal meaning from the dictionary is come together and form
one mass or whole or combine (elements) in a mass or whole.
According to SAP documentation, the COALESCE function in Open SQL
returns the value of the argument arg1 (if this is not the null value); otherwise, it
returns the value of the argument arg2. A blank must be placed after the
opening parenthesis and before the closing parenthesis. A comma must be
placed between the arguments
Check the usage below. If data for ekko~lifnr is present (means PO is created
for the lessor) then the LIFNR (Vendor Number) from EKKO is printed else,
http://www.sapyard.com/abaponsaphanapartvi/

11/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

No PO literal is updated. This function is quite handy in many real practical


scenarios.
Sample 5 ( Using COALESCE and Logical operators like GE / GT/ LE / LT
etc in JOIN which was originally not available

SELECT lfa1~lifnr,
lfa1~name1,
246 ekko~ebeln,
Shares ekko~bukrs,
COALESCE( ekko~lifnr, 'No PO' ) AS vendor
FROM lfa1 AS lfa1 LEFT OUTER JOIN ekko AS ekko
ON lfa1~lifnr EQ ekko~lifnr
AND ekko~bukrs LT '0208'
INTO TABLE @DATA(lt_vend_po)
UP TO 100 ROWS.

IF sy-subrc = 0.
cl_demo_output=>display_data(
EXPORTING
value = lt_vend_po
name = 'New AGE SQL : 5' ).
ENDIF.

Also Read: Are you a Lazy ABAPer?


http://www.sapyard.com/abaponsaphanapartvi/

12/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

How many times and in how many projects did you have the requirement to print
Plant and Plant description together like 0101 (Houston Site) or in forms you
had the requirement to write Payee (Payee Name)? We achieved it by looping
and concatenating. We did not have better option earlier, but now we can do
it while selecting the data. Thanks to the SAP Development Team.
Sample 6 (Concatenation while selecting data )

SELECT lifnr
Shares
&& '(' && name1 && ')' AS Vendor,
ORT01 as city
FROM lfa1
INTO TABLE @DATA(lt_bp_data)
UP TO 100 ROWS.
IF sy-subrc = 0.
cl_demo_output=>display_data(
EXPORTING
value = lt_bp_data
name = 'New AGE SQL : 6' ).
ENDIF.

246

http://www.sapyard.com/abaponsaphanapartvi/

13/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

246
Shares

Every report/conversion/interface asks us to validate the input data and we


do it by checking its existence in the check table. That has become easier
and better now like shown below.
Sample 7 ( Check existence of a record )

SELECT SINGLE @abap_true


FROM mara
INTO @DATA(lv_exists)
WHERE MTART = 'IBAU'.
IF lv_exists = abap_true.
WRITE:/ 'Data Exists!! New AGE SQL : 7'.
ENDIF.

ABAP was always a fth generation programming language and it has


become more so. It has become more readable and real life syntactically too.
. HAVING function is another feather to the crown.
Sample 8 ( Use of HAVING functions in SELECT )
http://www.sapyard.com/abaponsaphanapartvi/

14/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

SELECT lfa1~lifnr,
lfa1~name1,
ekko~ebeln,
ekko~bukrs
FROM lfa1 AS lfa1 INNER JOIN ekko AS ekko
ON lfa1~lifnr EQ ekko~lifnr
AND ekko~bukrs LT '0208'
INTO TABLE @DATA(lt_vend_po)
GROUP BY lfa1~lifnr, lfa1~name1, ekko~ebeln, ekko~bukrs
HAVING lfa1~lifnr > '0000220000'.
246
Shares
IF sy-subrc = 0.
cl_demo_output=>display_data(
EXPORTING
value = lt_vend_po
name = 'New AGE SQL : 8' ).
ENDIF.

You might also enjoy GPS like Tool in SAP.

http://www.sapyard.com/abaponsaphanapartvi/

15/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

Remember, sometimes we need to select all elds of more than one table
and provide custom names in the output. Wasnt it tiresome to create TYPEs
and achieve our requirement?
Sample 9 ( Use of selection of all columns with renaming of elds. This
is handy in case you have to do all eld select )
I thought with ABAP 740, I could do the below.
246
Shares

SELECT jcds~*,
tj02t~*
FROM jcds INNER JOIN tj02t
ON jcds~stat = tj02t~istat
WHERE tj02t~spras = @sy-langu
INTO TABLE @DATA(lt_status)
UP TO 1000 ROWS.
IF sy-subrc = 0.
cl_demo_output=>display_data(
EXPORTING
value = lt_status
name = 'New AGE SQL : 9' ).
ENDIF.

The above code is syntactically correct. Wow!! I was so excited to test it as it


would show all columns from both the tables.

OOPs!! We get the abovemessage. Too early to be so happy.


Let us modify the same code a little bit. We need to de ne the TYPEs and
declare the internal table (Inline did not work above).
http://www.sapyard.com/abaponsaphanapartvi/

16/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

TYPES BEGIN OF ty_data.


INCLUDE TYPE jcds AS status_change RENAMING WITH SUFFIX _change.
INCLUDE TYPE tj02t AS status_text RENAMING WITH SUFFIX _text.
TYPES END OF ty_data.

DATA: lt_status TYPE STANDARD TABLE OF ty_data.


SELECT jcds~*,
tj02t~*
FROM jcds INNER JOIN tj02t
ON jcds~stat = tj02t~istat
246 WHERE tj02t~spras = @sy-langu
Shares
INTO TABLE @lt_status
UP TO 100 ROWS.

IF sy-subrc = 0.
cl_demo_output=>display_data(
EXPORTING
value = lt_status
name = 'New AGE SQL : 9' ).
ENDIF.

Check _CHANGE is added to the eld name. _TEXT is also added in the
column name from second table (not captured in the screen print below)

These were just the tip of the icebergs. We would stumble upon more
features and surprises as we work on projects in real system. Just to let you
know, all the above code snippets are from atraditional database (not HANA)
http://www.sapyard.com/abaponsaphanapartvi/

17/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

which has EhP 7.4. So do not confuse that we need HANA database to take
advantage of modern SQL techniques. We just need near and above EhP 7.4.
Update 08/25/2016:
We askedIf CDS Views and SQL can achieve the same functionality. Which
one should we choose?
246
Shares

Expert Simon Bain (CEO SearchYourCloud Inc.) said:


I suppose the answer would be another questionor set of questions. In your
application do you currently use CDS? Are your developers knowledgeable
on CDS? If yes to both then probably CDS Views.
If there is a learning curve then go for the more widely known SQL and train
the development team for the next update, rather than putting in code that
they are either unhappy with or have little knowledge on.
At the end of the day, I would say use whichever one works best for your
project, team and application. The user should not see any difference in
usability. It is all about maintenance and knowledge at the end of the day.
If you want to get such useful articles directly to your inbox, please
SUBSCRIBE. We respect your privacy and take protecting it seriously.
If you liked this post, please hit the share buttons and like us on facebook.
Thank you very much for your time!!
Also, check our complete list of ABAP for SAP HANA Tutorials.

http://www.sapyard.com/abaponsaphanapartvi/

18/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

105

246
Shares

Isn't it Fair to Share??


Related Posts:

Real Time Analytics

http://www.sapyard.com/abaponsaphanapartvi/

19/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

Previous post

9 COMMENTS

Next post

ON "ABAP ON SAP HANA. PART VI. NEW AGE OPEN SQL ABAP 740"

246
Shares

Stefanos | August 21, 2016 at 8:25 pm | Reply


Very interesting and useful. Many many Thanks.

Giri | August 23, 2016 at 1:27 pm | Reply


thanks for the blog. I am new to ABAP. We have a
requirement of adding 2 columns, when sending data from ECC to
HANA. For this we have written a ABAP include to add these columns
(which is con gured on SAP SLT server), which is not working. Any
suggestions, if we can use openSQL to con gure the same on ECC/SLT
Server? thanks for your guidance.
Regards
Giri

vinay singh | August 30, 2016 at 4:48 pm | Reply


http://www.sapyard.com/abaponsaphanapartvi/

20/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

hello Giri,
this should be work . Please drop the table and
reload it HANA studio, if it still does not work
please share the error log with us.(I hope you the restriction on
lines of code you could have in it).
You can also use Smart Data Integrator for it.
please be reminded Open SQL is preferred approach for Code
246
Shares

Push Down to HANA.


Please revert if still have the issue .
regards,
vinay singh

MD Saif | August 30, 2016 at 2:15 am | Reply


Very helpful blog. Thanks for sharing knowledge.
Appreciate for your work. Keep doing and helping.

SAP Yard | August 31, 2016 at 2:24 am | Reply


Dear Saif Thank you so much for your
encouraging words. Please keep visiting and sharing your
thoughts.
http://www.sapyard.com/abaponsaphanapartvi/

21/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

Regards,
Team SAPYard.

Feleciano Buenavista | September 15, 2016 at 8:00 am | Reply


246

Very useful information for Abapers willing to explore

Shares

SAP HANA and beyond. Hoping one day I could taste its power.

SAP Yard | September 15, 2016 at 1:39 pm | Reply


Dear Feleciano Thank you for sparing some time
going through our posts and leaving your feedback. You can try
Hana Cloud Platform for free. You can try some basic stuffs
there for free. We need to be prepared for any opportunity which
might knock our door in future.
Regards,
Team SAPYard.

Naveen | September 21, 2016 at 6:24 am | Reply


Your blog is very interesting and really helpful to every
abaper for quick reference.
http://www.sapyard.com/abaponsaphanapartvi/

22/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

I would like to add one more point, which i faced many times during
my work. some of the above explained statements wont work with
forall entries. So, use new open sql accordingly.
keep sharing
Thanks
Naveen
246
Shares

SAP Yard | September 21, 2016 at 2:25 pm | Reply


Dear Naveen,
Thank you very much for your message and thank you for
pointing out the exceptions.
Would it be possible for you to provide some documents about
these exception in For All Entries? We would append them along
with this article.
Regards,
Team SAPYard.

Comment, Criticism, Opinion, Feedback. Please do not


hold back. Share your Thoughts!!
http://www.sapyard.com/abaponsaphanapartvi/

23/24

10/30/2016

ABAPonSAPHANA.PartVI.NewAgeOpenSQLABAP740SAPYard

Enteryourcommenthere...

COPYRIGHT 2016 | MH NEWSDESK LITE BY MH THEMES

246
Shares

http://www.sapyard.com/abaponsaphanapartvi/

24/24

Vous aimerez peut-être aussi