Skip to content
NextLytics
Megamenü_2023_Über-uns

Shaping Business Intelligence

Whether clever add-on products for SAP BI, development of meaningful dashboards or implementation of AI-based applications - we shape the future of Business Intelligence together with you. 

Megamenü_2023_Über-uns_1

About us

As a partner with deep process know-how, knowledge of the latest SAP technologies as well as high social competence and many years of project experience, we shape the future of Business Intelligence in your company too.

Megamenü_2023_Methodik

Our Methodology

The mixture of classic waterfall model and agile methodology guarantees our projects a high level of efficiency and satisfaction on both sides. Learn more about our project approach.

Products
Megamenü_2023_NextTables

NextTables

Edit data in SAP BW out of the box: NextTables makes editing tables easier, faster and more intuitive, whether you use SAP BW on HANA, SAP S/4HANA or SAP BW 4/HANA.

Megamenü_2023_Connector

NextLytics Connectors

The increasing automation of processes requires the connectivity of IT systems. NextLytics Connectors allow you to connect your SAP ecosystem with various open-source technologies.

IT-Services
Megamenü_2023_Data-Science

Data Science & Engineering

Ready for the future? As a strong partner, we will support you in the design, implementation and optimization of your AI application.

Megamenü_2023_Planning

SAP Planning

We design new planning applications using SAP BPC Embedded, IP or SAC Planning which create added value for your company.

Megamenü_2023_Dashboarding

Dashboarding

We help you with our expertise to create meaningful dashboards based on Tableau, Power BI, SAP Analytics Cloud or SAP Lumira. 

Megamenü_2023_Data-Warehouse-1

SAP Data Warehouse

Are you planning a migration to SAP HANA? We show you the challenges and which advantages a migration provides.

Business Analytics
Megamenü_2023_Procurement

Procurement Analytics

Transparent and valid figures are important, especially in companies with a decentralized structure. SAP Procurement Analytics allows you to evaluate SAP ERP data in SAP BI.

Megamenü_2023_Reporting

SAP HR Reporting & Analytics

With our standard model for reporting from SAP HCM with SAP BW, you accelerate business activities and make data from various systems available centrally and validly.

Megamenü_2023_Dataquality

Data Quality Management

In times of Big Data and IoT, maintaining high data quality is of the utmost importance. With our Data Quality Management (DQM) solution, you always keep the overview.

Career
Megamenü_2023_Karriere-2b

Working at NextLytics

If you would like to work with pleasure and don't want to miss out on your professional and personal development, we are the right choice for you!

Megamenü_2023_Karriere-1

Senior

Time for a change? Take your next professional step and work with us to shape innovation and growth in an exciting business environment!

Megamenü_2023_Karriere-5

Junior

Enough of grey theory - time to get to know the colourful reality! Start your working life with us and enjoy your work with interesting projects.

Megamenü_2023_Karriere-4-1

Students

You don't just want to study theory, but also want to experience it in practice? Check out theory and practice with us and experience where the differences are made.

Megamenü_2023_Karriere-3

Jobs

You can find all open vacancies here. Look around and submit your application - we look forward to it! If there is no matching position, please send us your unsolicited application.

Blog
NextLytics Newsletter Teaser
Sign up now for our monthly newsletter!
Sign up for newsletter
 

Tips for readable SQLscript code

The most elementary requirement of SQLScript Code is that it works correctly. That already given, we must ensure readability, transparency and maintainability of our code. This is crucial, as existing reporting solutions often have to be subsequently corrected or expanded. The reasons for this can be manifold: new regulatory requirements, new user requests or simply errors in the implementation.

For everything to run smoothly we have to maintain a clean and legible codebase as a basic requirement. Well written SQL code can be read as easily as a book, even without much context. It must always be clear what is happening in the source code and for what reason. If necessary, even an uninvolved programmer must be able to quickly identify the appropriate part of the coding and execute necessary adjustments.

This holds true for the original developer as well. Even with your own developments, it can happen that after a while you no longer find your way around. With comprehensible and well-structured code, troubleshooting is easier and faster.  So how do you keep your code clean? In this article we will provide you with a few ideas.

Formatting

One of the simplest approaches to make SQLScript more readable is using appropriate formatting.

Line breaks

From a technical perspective each SQL query can be written in a single line. However, that would cause many queries to become too long and difficult to read. Therefore you should always use only one line per field and condition. For example:

SELECT
  CustomerName,
  City
FROM
  Customers
WHERE
    Country='Germany';

Blank lines

The correct use of blank lines can also improve readability. Avoid blank lines within a statement and instead use blank lines to separate the individual blocks of your program, e.g. variable declaration.

DO BEGIN

lt_customers_in = SELECT
CustomerID
FROM
Customers
WHERE
  Country = 'Germany';

lt_employees_in = SELECT
EmployeeID,
Amount
FROM
Orders
WHERE
  CustomerID in (SELECT * FROM :lt_customers_in);

SELECT
EmployeeID,
SUM(Amount) AS Amount
FROM
:lt_employees_in
GROUP BY
EmployeeID
ORDER BY
Amount DESC;

END;

001-readable-line-breaks_SQLScript Code

Indentation

To make your code even more readable, indent fields and conditions after commands. Use indentation for subqueries as well.

So instead of using a left-aligned format, as in the example below...

SELECT
CustomerName,
City
FROM
Customers;

... your code looks like this:

SELECT
  CustomerName,
  City
FROM

    Customers;

Capitalization

To make it easy for everyone (and also your future self) to figure out what operations are being performed, you should capitalize all SQL statements.

This includes keywords such as SELECT, FROM, WHERE, GROUP BY, ORDER BY, but also statements used within clauses, such as JOIN, AS, IN, as well as ON. Functions like SUM() or COUNT() should also be capitalized to better distinguish them from column names.

Speaking Names

Use meaningful, i.e. self-explanatory, names for variables. The purpose of the variable should be clear from the name. For example the variable for family names should be called lv_familyname, not var1. The name of a function should also directly communicate what it is used for. Accordingly, a variable name will usually result in a noun, while a function name will be a verb.


Increase the performance of your BW with SQLScript!
Click here for the whitepaper!

Neuer Call-to-Action


Comments

Developer comments help immensely when reading a program. Especially in spots where the coding is unintuitive and hard to understand. However, it is also important not to overdo it. Too many comments can hinder the reading flow, so only write comments when it is necessary. If you follow our advice, use meaningful names and write modular code, you should not have to write too many comments.

For example, the following comments are completely superfluous:

/* Show addresses for German customers */
SELECT
ContactName, --Name
Address, --Address
City, -- City
PostalCode --Postal code
FROM
/* Table with customer data */
Customers
WHERE
/* Selection criteria */
Country = 'Germany' --Select Germany only

On the other hand, comments are necessary when it is not obvious what you are trying to accomplish just from reading the code by itself. A good rule of thumb is that comments should answer the question "why" you are doing something, rather than "what" you are doing.

Apart from comments in the code itself, header comments providing a summary or high level overview of the individual steps are also helpful.

/*
Purpose: Lists salesmen that have registered more than 10 orders
Executed steps:
        1.Format name
        2.Enhance with respective team
Author: Sebastian Uhlig
Change History: Creation 14.12.2022
*/

Split queries into multiple steps

Normally, a block of code in SQL is always executed at once. By dividing your coding into several steps, you can find causes of errors more easily afterwards.  The separation allows you to analyze the individual steps in the debugger and check whether the expected result matches the actual result.

We recommend the following order:

  1. Input selections
  2. Aggregations
  3. Calculations
  4. Joins
  5. Output selection

By the way, ORDER BY is often used in all of these steps, but it is only relevant in the output selection. By eliminating superfluous statements, your code will be less cluttered.

In SQLScript you can use table variables for subdivision. This makes the queries easier to read. It is obvious at first glance which sequence has been defined and which steps are executed before the final result is displayed.

For example:

DO BEGIN

-- Select german customers
lt_customers_in = SELECT
CustomerID
FROM
Customers
WHERE
  Country = 'Germany';

-- Identify respective salesmen
lt_employees_in = SELECT
EmployeeID,
Amount
FROM
Orders
WHERE
  CustomerID in (SELECT * FROM :lt_customers_in);

-- Show total sales per salesman in Germany
SELECT
EmployeeID,
SUM(Amount) AS Amount
FROM
:lt_employees_in
GROUP BY
EmployeeID
ORDER BY
Amount DESC;

END;

 

002-multiple-steps_SQLScript Code

Use UDFs

As mentioned earlier, modularization helps us find bugs in the code faster. Besides separating the query into several steps, you can also use User Defined Functions (UDFs). They can help to reduce complexity and increase readability. The functions can also be tested individually and provide individual, verifiable results, which is helpful with larger queries.

Another advantage of UDFs is their reusability. You can write the function once and use it in multiple queries. For example, you want to perform a concatenation of surnames and first names:

 SELECT
  FirstName || ' ' ||  LastName AS Name
FROM
    Customer;

Instead of using this piece of code in many individual queries and thus overloading the code, you can create a UDF for it and reuse multiple times:

CREATE FUNCTION udf_concatenate_name(
lv_firstname NVARCHAR(30),
lv_lastname NVARCHAR(30)
)
RETURNS lv_name NVARCHAR(61)
AS BEGIN
lv_name = lv_firstname || ' ' || lv_lastname;
END;

SELECT
  CustomerID,
  udf_concatenate_name(FirstName, LastName) AS Name
FROM
    Customer;

SQLScript Code - Our Summary

Now you have learned several approaches to make your SQLScript code more readable and facilitate subsequent maintenance. Always remember: good SQL code is not that which computers understand, but that which humans understand.

Do you have questions about SQLScript? Or do you want to convert your transformation routines to SQLScript and are looking for experienced developers with SQLScript know-how? Please do not hesitate to contact us.

Learn more about  SAP HANA SQLScript

Further information can be found in the book "SQLScript for SAP HANA" (SAP PRESS, ISBN 978-3-8362-7408-1) by Jörg Brandeis.

avatar

Irvin

Irvin has worked with HANA Native and Datasphere since 2019. He can primarily draw on experience as a consultant and in development on the XSA platform using SQLScript. In his spare time, he is passionate about playing basketball and is also a big NBA fan.

Got a question about this blog?
Ask Irvin

Blog - NextLytics AG 

Welcome to our blog. In this section we regularly report on news and background information on topics such as SAP Business Intelligence (BI), SAP Dashboarding with Lumira Designer or SAP Analytics Cloud, Machine Learning with SAP BW, Data Science and Planning with SAP Business Planning and Consolidation (BPC), SAP Integrated Planning (IP) and SAC Planning and much more.

Subscribe to our newsletter

Related Posts

Recent Posts