Skip to content

Latest commit

 

History

History
56 lines (28 loc) · 3.6 KB

File metadata and controls

56 lines (28 loc) · 3.6 KB

##Scalar User Defined Functions in SAP HANA

The demonstration application SAP HANA Interactive Education (SHINE) includes tutorial to help you learn how to build native SAP HANA applications.

The tutorial provided with the SHINE content show you how to create your own native HANA applications that make use of the sample data and design-time developer objects like: database tables, HANA views, Stored Procedures, Functions, Server-side JavaScript (XSJS), OData services as well as the end user interface using SAPUI5 Technology and Fiori design principles.

In this blog I would introduce you to Scalar User Defined Functions in SAP HANA.

This blog would help you to understand how to create and activate design-time scalar user-defined functions (UDF) and how it is used in the context of SHINE demo content.

Before we move on with creating a Scalar UDF, let us try to first understand what Scalar UDFs in SAP HANA is:

Scalar UDFs are user-defined functions which accept multiple input parameters and returns exactly one scalar value.
These functions allow the developer to encapsulate complex algorithms into manageable, reusable code which can then be nested within the field list of a SELECT statement.

#####How can you get started with creating a Scalar UDF?

Below is an example showing how to create a scalar UDFs and then leveraging it within the field list of a SELECT statement.
This is a very simple example and focus is purely to help you understand the syntax removing any complex logic.

This function is available with the SHINE Delivery Unit and it gives you an insight of how you can create Scalar User Defined functions and use them in your application code.

You can create the Scalar UDF in the HANA repository via an XS Project.

Start the Create New UDF wizard as shown below:

In the Project Explorer view, choose New -> Other ->SAP HANA - >Database Development -> Scalar Function.

Scalar Function

In the SQL Editor, type the code that defines the new user-defined function. This example demonstrates a discount function which takes the price and discount unit as the input parameters and returns the new discounted price as the output parameter. The output is a scalar value and this function can be nested within the field list of a SELECT statement.

Scalar Function

Once you activate the function, the new objects will show up on the catalog in the appropriate package.

Scalar Function

You can now use the functions in the field list of your SELECT statements. You can call the function from the SQL Console as shown:

Scalar Function

Again, this is a simple example, but you can see how powerful a tool scalar UDFs could be to a developer.