Friday 17 November 2017

SAP HANA: Table User Defined Functions


Table User Define Function always returns a table and its parameter may also be off table type.
Table UDFs are read-only user-defined functions which accept multiple input parameters and return exactly one results table.  SQLScript is the only language which is supported by table UDF's. Since these functions are read-only, only read-only statements can be used within the function.  So, you may not use statements like INSERT, UPDATE or DELETE. Also, any procedure calls within the function must also be read-only.  Currently, you can only create these functions in the catalog via the SQL Editor.

Syntax:

CREATE FUNCTION FUNCTION_NAME
(Parameter1 DATA_TYPE,Parameter2 DATA_TYPE)
RETURNS TABLE
( RetruenValue1 DATA_TYPE, ReturnValue2 DATA_TYPE )
BEGIN
RETURN SELECT 1* Parameter1 AS RetruenValue1 ,1*Parameter2 AS ReturnValue2 FROM mytab;
END;
Table User-defined Functions Support the Following:
v  They can have any number of input parameters
v  They return exactly one table
v  Table operations are allowed within the body
v  They are used in the FROM clause of SELECT statements
v  They must be free of side-effects, i.e. DDL statements or the DML statements insert, update and delete cannot be used in function bodies
The SQL statement to define table user-defined functions also is create function. The difference to the definition of a scalar user-defined function is keyword table for the return parameter and how the value of the sole return parameter is assigned using keyword return:

Basic Syntax to Define a Table User-Defined Function

CREATE FUNCTION <function name> (<list of input parameters with type>) RETURNS TABLE [table type| (<list of table column definitions>)] AS BEGIN
<function body>
RETURN <expression to set return table>
END;
              
             A feature that can be helpful in the context of defining table user-defined functions or stored procedures is dynamic filtering using built-in table function apply_filter. This function allows you to apply a dynamic WHERE clause to a database table or table variable and assign the result to a table variable.

Table:

E_ID
E_NAME
OVERTIME(in_Hrs)
1
Kabil
20
2
Nazeer
15
3
Kalai
10
4
Keerthi
10

CODE :

To Create a  Function:

CREATE FUNCTION "KABIL_PRACTICE"."Convert_OfficialsHours"
(im_filter VARCHAR(1000), im_to VARCHAR(1) )
RETURNS TABLE ( "E_ID" NVARCHAR(3),
"E_NAME" NVARCHAR(20),
Overtime DEC (5,2))
AS BEGIN
lt_official = APPLY_FILTER("KABIL_PRACTICE"."OVERTIME"
,:im_filter);
RETURN SELECT "E_ID", "E_NAME",
"KABIL_PRACTICE"."Convert_Hours_IF_ELSE"("OVERTIME(in_Hrs)",:im_to) AS Overtime
FROM :lt_official;
END;
Note:
Apply_filter: You can use APPLY_FILTER with persistent tables and table variables.
The APPLY_FILTER function applies a dynamic filter on a table or table variable. Logically it can be considered a partial dynamic SQL statement. The advantage of the function is that you can assign it to a table variable and will not block SQL – inlining. Despite this, all other disadvantages of a full dynamic SQL yield also for the APPLY_FILTER.
"KABIL_PRACTICE"."Convert_Hours_IF_ELSE": It is a Scalar User Defined function which is already created.

To call a Function:

SELECT * FROM "KABIL_PRACTICE"."Convert_OfficialsHours"('E_NAME LIKE ''%K%''','d');

Result:

Thursday 16 November 2017

SAP HANA : Scalar User Defined Functions


User Defined Function in SAP HANA:

User Defined Functions in SAP HANA are the read only function that means we cannot perform any DDL and DML (insert update and delete) operation inside the body of the function. There are two types of user-defined functions: Scalar User Defined Functions and Table User Defined Functions.
There are two types of user define the function in SAP HANA:
1. Table User Define Function (Table UDF)
2. Scalar User Define Function (Scalar UDF)
These categories are defined on the basis of input/output parameter, supported functionality in the body of the function and how they are going to consume in the SQL statement.

Scalar Used-Defined Functions

The simplest database object for which you can make use of SQL Script are Scalar User- Defined Functions (Scalar UDFs)
Scalar UDFs allow you to the define functions which take a number of input parameters and return scalar values. Only expressions are allowed in the body of the UDF, so no table operations, CE functions or array operations.
Scalar User-Defined Functions Support the Following:
·         They can have any number of scalar input parameters (primitive SQL types). Input parameters of table type are not supported.
·         They can return multiple scalar values.
·         They can contain expressions within their body. Table and array operations are not supported.
·         They can be used in the field list or the WHERE clause of SELECT statements — like built-in functions.
·         They are callable via direct assignment in other user-defined functions or stored procedures
·         (x := my_scalar_func () ).
·         They must be free of side-effects and do not support any type of SQL statement in their body.
The SQL statement to define user-defined functions is create function. The basic syntax to define a scalar user-defined function looks as follows:

Basic Syntax to Define a Scalar User-Defined Function

CREATE FUNCTION <function name> (<list of input parameters with
type>)
RETURNS <scalar result parameter name and type>
AS BEGIN
<function body>
END;
v  You can create scalar user-defined functions for use like built-in functions.
v  Prefix parameter names with “:”to access their values.

Code:

To Create a table:
create column table "KABIL_PRACTICE"."OVERTIME"
 (
 "E_ID" integer,
 "E_NAME" Nvarchar(35),
 "OVERTIME(in_Hrs)" Integer
);
To Drop a table:
Drop table "KABIL_PRACTICE"."OVERTIME";
To Insert the values into a table:
Insert into "KABIL_PRACTICE"."OVERTIME" values (1,'Kabil',20);
Insert into "KABIL_PRACTICE"."OVERTIME" values (2,'Nazeer',15);
Insert into "KABIL_PRACTICE"."OVERTIME" values (3,'Kalai',10);
To Create function:
Create Function "KABIL_PRACTICE"."CONVERT_HRS"
(i_Hours integer)
returns  result Decimal(34,2)
as begin
result := :i_Hours * 60;
End;
NOTE:
            We should not mention parameter name inside the double quotes. If we did it throws an error such as inappropriate variable name: do not allow "" or '_SYS_' prefix for the name of variable or parameter
To Call the function using Dummy:
select "KABIL_PRACTICE"."CONVERT_HRS"(1) from dummy;
Result:




To Use the function with the Table:
select *,"KABIL_PRACTICE"."CONVERT_HRS"("OVERTIME(in_Hrs)") as "OVERTIME_in_Minutues" from "KABIL_PRACTICE"."OVERTIME";

Result:

To Drop a function:
Drop function "KABIL_PRACTICE"."CONVERT_HRS";
NOTE:
The only way to change the body of an existing user-defined function using SQL statements is to delete the function and re-create it.

Tuesday 14 November 2017

SAP HANA: How to DELETE duplicate records from the Table but keep original record?


How to delete duplicate records from the Table but keep the original record?

Scenario:

DEPTID
DEPTNAME
PRICE
1
Blog
2000
2
Article
5000
3
Resource
7000
4
Book
500
4
Book
500
1
Blog
1000

From the above table, DEPTID 4 have some duplicate records. i.e., DEPTID, DEPTNAME, PRICE columns having the same value.

Solution:

Code:

delete from "ADZSUPRI_PRACTICE"."cpy_tab_Department"
where "$rowid$" in
(
SELECT   LEAD("$rowid$") over (partition by DEPTID,DEPTNAME,"price" order by DEPTID,DEPTNAME)
from "ADZSUPRI_PRACTICE"."cpy_tab_Department" ;
)

Result:

DEPTID
DEPTNAME
PRICE
1
Blog
2000
2
Article
5000
3
Resource
7000
4
Book
500
1
Blog
1000

 Note:
When we execute the inner query i.e., Sub query we get the result like as shown below:

Code:

SELECT   LEAD("$rowid$") over (partition by DEPTID,DEPTNAME,"price" order by DEPTID,DEPTNAME) from "ADZSUPRI_PRACTICE"."cpy_tab_Department" ;


ROWID
·         For each row in the database, the rowid pseudo column returns the address of the row.
·         Usually, a rowid value uniquely identifies a row in the database.
·         Rowid values have several important uses:
v  They are the fastest way to access a single row.
v  They can show you how the rows in a table are stored.
v  They are unique identifiers for rows in a table.