SlideShare une entreprise Scribd logo
1  sur  34
Télécharger pour lire hors ligne
Brandeis Consulting
SQL developers think
different
Jörg Brandeis
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
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
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
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
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
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?
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
Brandeis Consulting
Set-based thinking
18.07.2018
SQL developers think different
Each step is a transformation
from one table to the next
table.
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
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
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
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
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.
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!
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.
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
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;
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;
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
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
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
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;
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;
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;
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
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
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';
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
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
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
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
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
Brandeis Consulting
Vorstellung
Jörg Brandeis
SQL Developers think different
Contact:
www.brandeis.de
joerg@brandeis.de
@joerg_brandeis
Xing, LinkedIn
joerg@brandeis.de / www.brandeis.de / @joerg_brandeis

Contenu connexe

Similaire à SQL developer think different

SQL Pass Architecture SQL Tips & Tricks
SQL Pass Architecture SQL Tips & TricksSQL Pass Architecture SQL Tips & Tricks
SQL Pass Architecture SQL Tips & TricksIke Ellis
 
Performance By Design
Performance By DesignPerformance By Design
Performance By DesignGuy Harrison
 
Beginner's Guide: Programming with ABAP on HANA
Beginner's Guide: Programming with ABAP on HANABeginner's Guide: Programming with ABAP on HANA
Beginner's Guide: Programming with ABAP on HANAAshish Saxena
 
Database highload solutions
Database highload solutionsDatabase highload solutions
Database highload solutionslarest
 
Database highload solutions
Database highload solutionsDatabase highload solutions
Database highload solutionslarest
 
Pass chapter meeting dec 2013 - compression a hidden gem for io heavy databas...
Pass chapter meeting dec 2013 - compression a hidden gem for io heavy databas...Pass chapter meeting dec 2013 - compression a hidden gem for io heavy databas...
Pass chapter meeting dec 2013 - compression a hidden gem for io heavy databas...Charley Hanania
 
Webinar: Scaling MongoDB
Webinar: Scaling MongoDBWebinar: Scaling MongoDB
Webinar: Scaling MongoDBMongoDB
 
Catalyst - refactor large apps with it and have fun!
Catalyst - refactor large apps with it and have fun!Catalyst - refactor large apps with it and have fun!
Catalyst - refactor large apps with it and have fun!mold
 
A Metadata-Driven Approach to Computing Financial Analytics in a Relational D...
A Metadata-Driven Approach to Computing Financial Analytics in a Relational D...A Metadata-Driven Approach to Computing Financial Analytics in a Relational D...
A Metadata-Driven Approach to Computing Financial Analytics in a Relational D...inscit2006
 
White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...Jeff Jacobs
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersSQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersAdam Hutson
 
SQL vs NoSQL deep dive
SQL vs NoSQL deep diveSQL vs NoSQL deep dive
SQL vs NoSQL deep diveAhmed Shaaban
 
SQL Server Tips & Tricks
SQL Server Tips & TricksSQL Server Tips & Tricks
SQL Server Tips & TricksIke Ellis
 
Performance By Design
Performance By DesignPerformance By Design
Performance By DesignGuy Harrison
 
MongoDB performance
MongoDB performanceMongoDB performance
MongoDB performanceMydbops
 
Intoduction to sql 2012 Tabular Modeling
Intoduction to sql 2012 Tabular ModelingIntoduction to sql 2012 Tabular Modeling
Intoduction to sql 2012 Tabular ModelingKaran Gulati
 
Dynamic SQL: How to Build Fast Multi-Parameter Stored Procedures
Dynamic SQL: How to Build Fast Multi-Parameter Stored ProceduresDynamic SQL: How to Build Fast Multi-Parameter Stored Procedures
Dynamic SQL: How to Build Fast Multi-Parameter Stored ProceduresBrent Ozar
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09guest9d79e073
 

Similaire à SQL developer think different (20)

SQL Pass Architecture SQL Tips & Tricks
SQL Pass Architecture SQL Tips & TricksSQL Pass Architecture SQL Tips & Tricks
SQL Pass Architecture SQL Tips & Tricks
 
Performance By Design
Performance By DesignPerformance By Design
Performance By Design
 
GraphDatabase.pptx
GraphDatabase.pptxGraphDatabase.pptx
GraphDatabase.pptx
 
Beginner's Guide: Programming with ABAP on HANA
Beginner's Guide: Programming with ABAP on HANABeginner's Guide: Programming with ABAP on HANA
Beginner's Guide: Programming with ABAP on HANA
 
Database highload solutions
Database highload solutionsDatabase highload solutions
Database highload solutions
 
Database highload solutions
Database highload solutionsDatabase highload solutions
Database highload solutions
 
Pass chapter meeting dec 2013 - compression a hidden gem for io heavy databas...
Pass chapter meeting dec 2013 - compression a hidden gem for io heavy databas...Pass chapter meeting dec 2013 - compression a hidden gem for io heavy databas...
Pass chapter meeting dec 2013 - compression a hidden gem for io heavy databas...
 
Webinar: Scaling MongoDB
Webinar: Scaling MongoDBWebinar: Scaling MongoDB
Webinar: Scaling MongoDB
 
Catalyst - refactor large apps with it and have fun!
Catalyst - refactor large apps with it and have fun!Catalyst - refactor large apps with it and have fun!
Catalyst - refactor large apps with it and have fun!
 
A Metadata-Driven Approach to Computing Financial Analytics in a Relational D...
A Metadata-Driven Approach to Computing Financial Analytics in a Relational D...A Metadata-Driven Approach to Computing Financial Analytics in a Relational D...
A Metadata-Driven Approach to Computing Financial Analytics in a Relational D...
 
White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersSQL Server 2008 Development for Programmers
SQL Server 2008 Development for Programmers
 
NoSQL - Leo's notes
NoSQL - Leo's notesNoSQL - Leo's notes
NoSQL - Leo's notes
 
SQL vs NoSQL deep dive
SQL vs NoSQL deep diveSQL vs NoSQL deep dive
SQL vs NoSQL deep dive
 
SQL Server Tips & Tricks
SQL Server Tips & TricksSQL Server Tips & Tricks
SQL Server Tips & Tricks
 
Performance By Design
Performance By DesignPerformance By Design
Performance By Design
 
MongoDB performance
MongoDB performanceMongoDB performance
MongoDB performance
 
Intoduction to sql 2012 Tabular Modeling
Intoduction to sql 2012 Tabular ModelingIntoduction to sql 2012 Tabular Modeling
Intoduction to sql 2012 Tabular Modeling
 
Dynamic SQL: How to Build Fast Multi-Parameter Stored Procedures
Dynamic SQL: How to Build Fast Multi-Parameter Stored ProceduresDynamic SQL: How to Build Fast Multi-Parameter Stored Procedures
Dynamic SQL: How to Build Fast Multi-Parameter Stored Procedures
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09
 

Dernier

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 

Dernier (20)

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 

SQL developer think different

  • 1. Brandeis Consulting SQL developers think different Jörg Brandeis
  • 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
  • 9. Brandeis Consulting Set-based thinking 18.07.2018 SQL developers think different Each step is a transformation from one table to the next table.
  • 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
  • 34. Brandeis Consulting Vorstellung Jörg Brandeis SQL Developers think different Contact: www.brandeis.de joerg@brandeis.de @joerg_brandeis Xing, LinkedIn joerg@brandeis.de / www.brandeis.de / @joerg_brandeis