My talk at the 1. HANA Tech Night Mannheim (https://www.meetup.com/de-DE/HANA-Developers-Mannheim/events/258187456/) about the different perspective of SQL developers.
2. Brandeis Consulting
Me
Jörg Brandeis
Freelancer
BW Consulting
SQLScript Trainings – every 2 months in Mannheim plus Inhouse Trainings for customers
Author of the Book „SQLScript für SAP HANA“ – english edition is available end of March!
Focus on technic – I am a developer!
SQL Developers think different
Contact:
www.brandeis.de
joerg@brandeis.de
@joerg_brandeis
Xing, LinkedIn
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
3. Brandeis Consulting
@hpseitz
SAP HANA Database Server
3
HANA DB
SQLScript
Graph
SQL View
Schema
“Container
”
Index
Table
Procedur
e
….. &
more
Code to Data
CDS
Calculatio
n
View
Spatial
Multi-model
Database
Nerd
4. Brandeis Consulting
Survey
Which programming language do you really know well?
JavaScript
ABAP
Java
SQL
PL/SQL or SQLScript
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
5. Brandeis Consulting
Declarative SQLScript
This talk is about declarative SQLScript only!
SQLScript has also imperative elements. But we don’t look at them today
The pure declarative logic is very powerful and can be optimized by the HANA
database very well
Often you don't have a clue how to solve a problem in SQL
This talk should give you some inspirations, what you can do with SQL
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
6. Brandeis Consulting
Different cultures
My first culture shock: From Java to ABAP
The second culture shock: From ABAP to SQLScript
How we solve problems depends on our idea of the possibilities of the respective
programming environment. The art is to use the strengths of the respective
programming language.
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
ABAP OO Java
Many build-in functions Everything is a method of an
object
Internal tables Collections
SAP GUI / WebDynpro Swing etc.
Many obsolete thins
Data centric Object oriented
7. Brandeis Consulting
The difference between SQL and ABAP is also a question of perspective
18.07.2018
SQL developers think different
7
What is the perspective of SQL? And what is the
perspective of an imperative programming language, like
Java or ABAP?
8. Brandeis Consulting
ABAP vs. SQLScript (declarativ) – the data types
ABAP
Primitive/scalar variables
(deep) structures
Internal tables
References to objects
SQL
Everything is a Table
Table variables
Table parameters
Database tables
Temorary tables
Table expressions
Table functions
Tables are sets
18.07.2018
SQL developers think different
10. Brandeis Consulting
Every problem becomes a nail
If you have only a hammer,
every problem becoms a nail
18.07.2018
SQL developers think different
11. Brandeis Consulting
The strenghts
Strengths of SQL:
• Very fast
• SELECT
• JOINS
• Set-Operations: UNION, INTERSECT,
EXCEPT
• Powerful SQL-Functions
• Expressions
• Table variables
18.07.2018
SQL developers think different
Strengths of ABAP:
• Internal tables
• Tight integration between DDic and
ABAP
• Cool features for business
applications
• Client handling
• Currency and unit handling
• Multi-language concept
• Generic programming
• OpenSQL
12. Brandeis Consulting
Example: lookup in a database table
A typical usecase in the BW is a lookup on a database table.
How do you do this in ABAP?
18.07.2018
SQLScript für BW-Berater
13. Brandeis Consulting
Example: lookup in a database table – The ABAP way (worst case)
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
FIELD-SYMBOLS: <ls_data> TYPE dtfitx_4.
LOOP AT c_c_t_data ASSIGNING <ls_data>.
SELECT SINGLE bupla gsber FROM bseg
INTO (<ls_data>-bupla,<ls_data>-gsber)
WHERE bukrs = <ls_data>-bukrs
AND belnr = <ls_data>-belnr
AND gjahr = <ls_data>-gjahr
AND buzei = <ls_data>-buzei.
ENDLOOP.
Bad idea!
SELECT SINGLE in a
LOOP is very slow.
In this case, millions of
SELECTS were executed
14. Brandeis Consulting
Example: lookup in a database table – The ABAP way (best case)
TYPES: BEGIN OF ty_bseg,
bukrs TYPE bukrs,
belnr TYPE belnr_d,
gjahr TYPE gjahr,
buzei TYPE buzei,
gsber TYPE gsber,
bupla TYPE bupla,
END OF ty_bseg.
DATA: lt_bseg_sel TYPE STANDARD TABLE OF ty_bseg
WITH DEFAULT KEY,
ls_bseg TYPE ty_bseg,
lth_bseg TYPE HASHED TABLE OF ty_bseg
WITH UNIQUE KEY bukrs belnr
gjahr buzei.
FIELD-SYMBOLS: <ls_data> TYPE dtfitx_4,
<s_bseg> TYPE ty_bseg.
LOOP AT c_c_t_data ASSIGNING <ls_data>.
ls_bseg-bukrs = <ls_data>-bukrs.
ls_bseg-belnr = <ls_data>-belnr.
ls_bseg-gjahr = <ls_data>-gjahr.
ls_bseg-buzei = <ls_data>-buzei.
APPEND ls_bseg TO lt_bseg_sel.
ENDLOOP.
SORT lt_bseg_sel.
DELETE ADJACENT DUPLICATES FROM lt_bseg_sel.
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
SELECT bukrs belnr gjahr buzei gsber bupla
FROM bseg
INTO TABLE lth_bseg
FOR ALL ENTRIES IN lt_bseg_sel
WHERE bukrs = lt_bseg_sel-bukrs
AND belnr = lt_bseg_sel-belnr
AND gjahr = lt_bseg_sel-gjahr
AND buzei = lt_bseg_sel-buzei.
LOOP AT c_c_t_data ASSIGNING <ls_data>.
read TABLE lth_bseg ASSIGNING <s_bseg>
with TABLE KEY bukrs = <ls_data>-bukrs
belnr = <ls_data>-belnr
gjahr = <ls_data>-gjahr
buzei = <ls_data>-buzei.
if sy-subrc = 0.
<ls_data>-bupla = <s_bseg>-bupla.
<ls_data>-gsber = <s_bseg>-gsber.
endif.
ENDLOOP.
Much faster, but this
is a lot of effort for a
standard case and
also very error-prone.
15. Brandeis Consulting
Example: lookup in a database table – The SQL way
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
outtab = SELECT it.*,
bseg.bupla,
bseg.gsber
FROM :intab as it
LEFT OUTER JOIN bseg
ON it.bukrs = bseg.bukrs
AND it.belnr = bseg.belnr
AND it.gjahr = bseg.gjahr
AND it.buzei = bseg.buzei.
Just define the
result, don’t think
about the
algorithm!
16. Brandeis Consulting
outtab = SELECT it.*,
bseg.bupla,
bseg.gsber
FROM :intab as it
LEFT OUTER JOIN bseg
ON it.bukrs = bseg.bukrs
AND it.belnr = bseg.belnr
AND it.gjahr = bseg.gjahr
AND it.buzei = bseg.buzei.
Example: lookup in a database table – The SQL way
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
Just define the
result, don’t think
about the
algorithm!
SQLScript is obviously better suited for this task: Just tell the system, what you want.
17. Brandeis Consulting
The other side
This was an example, where declarative SQLScript is much more elegant than
ABAP or any other programming language.
But there are many language elements, that doesn’t exist in declarative
SQLScript:
• LOOP over a table variable
• DO n TIMES
• WHILE LOOPs
• IF…ELSE
• Recursion
On the following slides, I will show you some workarounds and examples.
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
18. Brandeis Consulting
Drawbacks of declarative SQLScript – no LOOP over a table
But you can do many calculations an transformations within the SELECT-clause:
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
LOOP AT lt_tmp ASSIGNING <ls_tmp>.
<ls_tmp>-name = <ls_tmp>-first_name && <ls_tmp>-last_name.
<ls_tmp>-amount_gross = <ls_tmp>-amount_net *
(1+<ls_tmp>-vat / 100).
ENDLOOP.
Lt_tmp2 = SELECT *,
firstname || lastname AS name,
amount_net * (1+vat/100) AS amount_gross
FROM :lt_tmp;
19. Brandeis Consulting
Drawbacks of declarative SQLScript – no DO x TIMES
It’s a join with a table of x numbers
Example: Count the characters of a text
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
DO x TIMES…/ FOR (int a = 0; a < x; a++)
lt_tmp = SELECT words.word,
pos.element_number AS pos
SELECT :it_word as words
CROSS JOIN SERIES_GENERATE_INTEGER(1,0,10)
AS pos;
20. Brandeis Consulting
Example: Count the characters of a text
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift
cipher, Caesar's code or Caesar shift, is one of the simplest and most widely
known encryption techniques. It is a type of substitution cipher in which each
letter in the plaintext is replaced by a letter some fixed number of positions down
the alphabet. For example, with a left shift of 3, D would be replaced by A, E
would become B, and so on. The method is named after Julius Caesar, who used
it in his private correspondence.[1] (https://en.wikipedia.org/wiki/Caesar_cipher)
Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
Start system
Catalog/SQL-Console
HANAdevsMAN/G…123
Solution inspired by http://www.kodyaz.com/sap-
abap/sap-hana-sqlscript-split-string-function.aspx
21. Brandeis Consulting
Example: Count the characters of a text
In cryptanalysis, frequency analysis (also known as counting letters) is the
study of the frequency of letters or groups of letters in a ciphertext. The method
is used as an aid to breaking classical ciphers. (https://en.wikipedia.org/wiki/Frequency_analysis)
Count the occurences of the characters of a text.
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
Start system
Catalog/SQL-Console
HANADEVSMAN/G…123
Solution inspired by http://www.kodyaz.com/sap-
abap/sap-hana-sqlscript-split-string-function.aspx
22. Brandeis Consulting
Example: Count the characters of a text – an ABAP solution
REPORT zjb_test.
DATA: BEGIN OF ls_cnt,
char TYPE c,
cnt TYPE int4,
END OF ls_cnt.
DATA lt_cnt LIKE TABLE OF ls_cnt.
DATA(text) = |Lore Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos
et accusam et justo duo dolores et ea rebum. Stet clita |.
DO strlen( text ) TIMES.
DATA(lv_index) = sy-index - 1.
ls_cnt = VALUE #( cnt = 1
char = text+lv_index(1) ).
COLLECT ls_cnt INTO lt_cnt.
ENDDO.
DELETE lt_cnt WHERE char = space.
SORT lt_cnt BY cnt DESCENDING.
cl_demo_output=>display( lt_cnt ).
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
23. Brandeis Consulting
Example: Count the characters of a text – a SQL solution
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
create function characters( it_word tt_word )
returns table (val nvarchar(1))
as begin
lt_tmp = select words.word,
pos.element_number as pos
from :it_word as words
cross join SERIES_GENERATE_INTEGER(1, 0, 10 ) as pos;
lt_tmp2 = select substr(word, pos, 1) as val
from :lt_tmp;
return select * from :lt_tmp2
where val <> '';
end;
24. Brandeis Consulting
Example: Count the characters of a text – a SQL solution
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
Peter
Paul
Dieter
Klaus Peter 1
Peter 2
Peter 3
Peter 4
Peter 5
Peter 6
… …
Peter 10
Paul 1
Paul 2
Paul 3
Paul 4
… …
P
e
t
e
r
…
P
a
u
l
…
P
e
t
e
r
P
a
u
L
…
create function characters( it_word tt_word )
returns table (val nvarchar(1))
as begin
lt_tmp = select words.word,
pos.element_number as pos
from :it_word as words
cross join SERIES_GENERATE_INTEGER(1, 0, 10 ) as pos;
lt_tmp2 = select substr(word, pos, 1) as val
from :lt_tmp;
return select * from :lt_tmp2
where val <> '';
end;
25. Brandeis Consulting
Example: Count the characters of a text – test of the SQL solution
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
do begin
lt_words = select firstname as word from users;
select val,
count(*)
from characters( :lt_words )
group by val
order by val asc;
end;
26. Brandeis Consulting
Drawbacks of declarative SQLScript – no recursion
Recursion is great, because you can traverse a graph, without knowing the size
and the number of steps you will have to go.
Sometimes, if you assume a maximum number of steps, you can find an elegant
solution for this problem in SQL.
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
No recursion
27. Brandeis Consulting
Flatten a hierarchy
A requirement, that is typicaly solved with recursion, is the hierarchy analyis.
An example from my last BW project:
A hierarchy is given in a NODE / PARENT relation. (H-Table of an InfoObject)
For each leaf-node, the node on level 1 should be found
The hierarchy has a maximum of 8 levels
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
28. Brandeis Consulting
Flatten a hierarchy
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
select lvl1.hieid,
coalesce(lvl8.nodename,
lvl7.nodename,
lvl6.nodename,
lvl5.nodename,
lvl4.nodename,
lvl3.nodename,
lvl2.nodename,
lvl1.nodename) as leaf,
lvl1.nodename as level_1
from "/BIC/HBI_PROFCT" as lvl1
left outer join "/BIC/HBI_PROFCT" as lvl2
on lvl1.nodeid = lvl2.parentid
and lvl1.hieid = lvl2.hieid
left outer join "/BIC/HBI_PROFCT" as lvl3
on lvl2.nodeid = lvl3.parentid
and lvl1.hieid = lvl3.hieid
left outer join "/BIC/HBI_PROFCT" as lvl4
on lvl3.nodeid = lvl4.parentid
and lvl1.hieid = lvl4.hieid
left outer join "/BIC/HBI_PROFCT" as lvl5
on lvl4.nodeid = lvl5.parentid
and lvl1.hieid = lvl5.hieid
left outer join "/BIC/HBI_PROFCT" as lvl6
on lvl5.nodeid = lvl6.parentid
and lvl1.hieid = lvl6.hieid
left outer join "/BIC/HBI_PROFCT" as lvl7
on lvl6.nodeid = lvl7.parentid
and lvl1.hieid = lvl7.hieid
left outer join "/BIC/HBI_PROFCT" as lvl8
on lvl7.nodeid = lvl8.parentid
and lvl1.hieid = lvl8.hieid
where lvl1.parent = '00000000';
and lvl1.hieid = 'TNBHWU3JK6CIDHPC10VUDJYY3';
29. Brandeis Consulting
Drawbacks of declarative SQLScript – no flow control
For each branch:
Create a table with the appropriate WHERE condition
Process the branch logic for the tables
UNION the result
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
IF … ELSE / CASE
30. Brandeis Consulting
Drawbacks of declarative SQLScript – no flow control
LOOP AT fruits.
IF apple .
Peel the apple.
Cut it into 6 pieces.
Remove the core.
IF pear.
Cut the pear into 4 pieces.
ENDLOP
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
IF … ELSE / CASE
31. Brandeis Consulting
Drawbacks of declarative SQLScript – no flow control
In SQL, we create two tables for the different fruits and process them:
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
Apples
Pears
32. Brandeis Consulting
Drawbacks of declarative SQLScript
We found some workarounds, that are more or less elegant. And they can be
optimized by the database.
If you have a lack of ideas, how to formulate a solution in SQL:
http://www.giyf.com/
There are thousands of ideas in the internet, how to solve problems with SQL.
Don’t focus on SQLScript, HANA SQL or SAP pages. Many helpful blog posts are
for different databases.
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different
33. Brandeis Consulting
If you want to start with SQLScript
You can install a HANA Express database, as HP has told you
You can use the SAP Cloud Platform, to get your own free instance of SAP
HANA 1.0 SP12 .
And you can use the data model, that I created for my book
https://github.com/captainabap/SQLScript_for_SAP_HANA
There are also all listings from the book.
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis
SQL developers think different