STRATEGY & CREATIVITY

SAP HANA In Memory Database.

PORTFOLIO

We pride ourselves on bringing a good technology and effective knowledge to every perfect one.

  • SAP HANA - SQL Triggers

    Triggers are stored programs, which are automatically executed or fired when some events occur. Triggers are, in fact, written to be executed in response to any of the following events −
    • A database manipulation (DML) statement (DELETE, INSERT, or UPDATE).
    • A database definition (DDL) statement (CREATE, ALTER, or DROP).
    • A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).
    Triggers could be defined on the table, view, schema, or database with which the event is associated.

    Benefits of Triggers

    Triggers can be written for the following purposes −
    • Generating some derived column values automatically
    • Enforcing referential integrity
    • Event logging and storing information on table access
    • Auditing
    • Synchronous replication of tables
    • Imposing security authorizations
    • Preventing invalid transactions
  • SAP HANA - SQL Sequences

    A sequence is a set of integers 1, 2, 3, that are generated in order on demand. Sequences are frequently used in databases because many applications require each row in a table to contain a unique value, and sequences provide an easy way to generate them.

    Using AUTO_INCREMENT column

    The simplest way in MySQL to use sequences is to define a column as AUTO_INCREMENT and leave rest of the things to MySQL to take care.

    Example

    Try out the following example. This will create table and after that it will insert few rows in this table where it is not required to give record ID because it is auto-incremented by MySQL.
    mysql> CREATE TABLE INSECT
       -> (
       -> id INT UNSIGNED NOT NULL AUTO_INCREMENT,
       -> PRIMARY KEY (id),
       -> name VARCHAR(30) NOT NULL, # type of insect
       -> date DATE NOT NULL, # date collected
       -> origin VARCHAR(30) NOT NULL # where collected
    );
    
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> INSERT INTO INSECT (id,name,date,origin) VALUES
       -> (NULL,'housefly','2001-09-10','kitchen'),
       -> (NULL,'millipede','2001-09-10','driveway'),
       -> (NULL,'grasshopper','2001-09-10','front yard');
     
    Query OK, 3 rows affected (0.02 sec)
    Records: 3 Duplicates: 0 Warnings: 0
    mysql> SELECT * FROM INSECT ORDER BY id;
    +----+-------------+------------+------------+
    | id | name        | date       | origin     |
    +----+-------------+------------+------------+
    | 1  | housefly    | 2001-09-10 | kitchen    |
    | 2  | millipede   | 2001-09-10 | driveway   |
    | 3  | grasshopper | 2001-09-10 | front yard |
    +----+-------------+------------+------------+
    3 rows in set (0.00 sec)
    

    Obtain AUTO_INCREMENT Values

    LAST_INSERT_ID( ) is a SQL function, so you can use it from within any client that understands how to issue SQL statements. Otherwise, PERL and PHP scripts provide exclusive functions to retrieve auto-incremented value of last record.

    PERL Example

    Use the mysql_insertid attribute to obtain the AUTO_INCREMENT value generated by a query. This attribute is accessed through either a database handle or a statement handle, depending on how you issue the query. The following example references it through the database handle −
    $dbh->do ("INSERT INTO INSECT (name,date,origin)
    VALUES('moth','2001-09-14','windowsill')");
    my $seq = $dbh->{mysql_insertid};

    PHP Example

    After issuing a query that generates an AUTO_INCREMENT value, retrieve the value by calling mysql_insert_id( ) −
    mysql_query ("INSERT INTO INSECT (name,date,origin)
    VALUES('moth','2001-09-14','windowsill')", $conn_id);
    $seq = mysql_insert_id ($conn_id);

    Renumbering an Existing Sequence

    There may be a case when you have deleted many records from a table and you want to re-sequence all the records. This can be done by using a simple trick but you should be very careful to do so if your table is having join, with other table.
    If you determine that resequencing an AUTO_INCREMENT column is unavoidable, the way to do it is to drop the column from the table, then add it again. The following example shows how to renumber the id values in the insect table using this technique −
    mysql> ALTER TABLE INSECT DROP id;
    mysql> ALTER TABLE insect
       -> ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
       -> ADD PRIMARY KEY (id);

    Starting a Sequence at a Particular Value

    By default, MySQL will start sequence from 1 but you can specify any other number as well at the time of table creation. Following is the example where MySQL will start sequence from 100.
    mysql> CREATE TABLE INSECT
       -> (
       -> id INT UNSIGNED NOT NULL AUTO_INCREMENT = 100,
       -> PRIMARY KEY (id),
       -> name VARCHAR(30) NOT NULL, # type of insect
       -> date DATE NOT NULL, # date collected
       -> origin VARCHAR(30) NOT NULL # where collected
    );
    Alternatively, you can create the table and then set the initial sequence value with ALTER TABLE.
  • SAP HANA - SQL Stored Procedures

    A procedure allows you to group the SQL statement into a single block. Stored Procedures are used to achieve certain result across applications. The set of SQL statements and the logic that is used to perform some specific task are stored in SQL Stored Procedures. These stored procedures are executed by applications to perform that task.
    Stored Procedures can return data in the form of output parameters (integer or character) or a cursor variable. It can also result in set of Select statements, which are used by other Stored Procedures.
    Stored Procedures are also used for performance optimization as it contains series of SQL statements and results from one set of statement determines next set of statements to be executed. Stored procedures prevent users to see the complexity and details of tables in a database. As Stored procedures contain certain business logic, so users need to execute or call the procedure name.
    No need to keep reissuing the individual statements but can refer to the database procedure.

    Sample Statement to Create Procedures

    Create procedure prc_name (in inp integer, out opt "EFASION"."ARTICLE_LOOKUP")
    as
    begin
    opt = select * from "EFASION"."ARTICLE_LOOKUP" where article_id = :inp ;
    end;
  • SAP HANA - SQL Expressions

    An Expression is used to evaluate a clause to return values. There are different SQL expressions that can be used in HANA −
    • Case Expressions
    • Function Expressions
    • Aggregate Expressions
    • Subqueries in Expressions

    Case Expression

    This is used to pass multiple conditions in a SQL expression. It allows the use of IF-ELSE-THEN logic without using procedures in SQL statements.

    Example

    SELECT COUNT( CASE WHEN sal < 2000 THEN 1 ELSE NULL END ) count1,
    COUNT( CASE WHEN sal BETWEEN 2001 AND 4000 THEN 1 ELSE NULL END ) count2,
    COUNT( CASE WHEN sal > 4000 THEN 1 ELSE NULL END ) count3 FROM emp;
    This statement will return count1, count2, count3 with integer value as per passed condition.

    Function Expressions

    Function expressions involve SQL inbuilt functions to be used in Expressions.

    Aggregate Expressions

    Aggregate functions are used to perform complex calculations like Sum, Percentage, Min, Max, Count, Mode, Median, etc. Aggregate Expression uses Aggregate functions to calculate single value from multiple values.
    Aggregate Functions − Sum, Count, Minimum, Maximum. These are applied on measure values (facts) and It is always associated with a dimension.
    Common aggregate functions include −
    • Average ()
    • Count ()
    • Maximum ()
    • Median ()
    • Minimum ()
    • Mode ()
    • Sum ()

    Subqueries in Expressions

    A subquery as an expression is a Select statement. When it is used in an expression, it returns a zero or a single value.
    A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.
    Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN etc.
    There are a few rules that subqueries must follow −
    • Subqueries must be enclosed within parentheses.
    • A subquery can have only one column in the SELECT clause, unless multiple columns are in the main query for the subquery to compare its selected columns.
    • An ORDER BY cannot be used in a subquery, although the main query can use an ORDER BY. The GROUP BY can be used to perform the same function as the ORDER BY in a subquery.
    • Subqueries that return more than one row can only be used with multiple value operators, such as the IN operator.
    • The SELECT list cannot include any references to values that evaluate to a BLOB, ARRAY, CLOB, or NCLOB.
    • A subquery cannot be immediately enclosed in a set function.
    • The BETWEEN operator cannot be used with a subquery; however, the BETWEEN operator can be used within the subquery.

    Subqueries with the SELECT Statement

    Subqueries are most frequently used with the SELECT statement. The basic syntax is as follows −

    Example

    SELECT * FROM CUSTOMERS
    WHERE ID IN (SELECT ID
    FROM CUSTOMERS
    WHERE SALARY > 4500) ;
    +----+----------+-----+---------+----------+
    | ID | NAME     | AGE | ADDRESS | SALARY   |
    +----+----------+-----+---------+----------+
    | 4  | Chaitali | 25  | Mumbai  | 6500.00  |
    | 5  | Hardik   | 27  | Bhopal  | 8500.00  |
    | 7  | Muffy    | 24  | Indore  | 10000.00 |
    +----+----------+-----+---------+----------+
  • SAP HANA - SQL Functions

    There are various SQL functions provided by SAP HANA database −
    • Numeric Functions
    • String Functions
    • Fulltext Functions
    • Datetime Functions
    • Aggregate Functions
    • Data Type Conversion Functions
    • Window Functions
    • Series Data Functions
    • Miscellaneous Functions

    Numeric Functions

    These are inbuilt numeric functions in SQL and use in scripting. It takes numeric values or strings with numeric characters and return numeric values.
    • ABS − It returns the absolute value of a numeric argument.
    Example  SELECT ABS (-1) "abs" FROM TEST;
    abs
    1
    ACOS, ASIN, ATAN, ATAN2 (These functions return trigonometric value of the argument)
    • BINTOHEX − It converts a Binary value to a hexadecimal value.
    • BITAND − It performs an AND operation on bits of passed argument.
    • BITCOUNT − It performs the count of number of set bits in an argument.
    • BITNOT − It performs a bitwise NOT operation on the bits of argument.
    • BITOR − It perform an OR operation on bits of passed argument.
    • BITSET − It is used to set bits to 1 in <target_num> from the <start_bit> position.
    • BITUNSET − It is used to set bits to 0 in <target_num> from the <start_bit> position.
    • BITXOR − It performs XOR operation on bits of passed argument.
    • CEIL − It returns the first integer that is greater or equal to the passed value.
    • COS, COSH, COT ((These functions return trigonometric value of the argument)
    • EXP − It returns the result of the base of natural logarithms e raised to the power of passed value.
    • FLOOR − It returns the largest integer not greater than the numeric argument.
    • HEXTOBIN − It converts a hexadecimal value to a binary value.
    • LN − It returns the natural logarithm of the argument.
    • LOG − It returns the algorithm value of a passed positive value. Both base and log value should be positive.
    Various other numeric functions can also be used − MOD, POWER, RAND, ROUND, SIGN, SIN, SINH, SQRT, TAN, TANH, UMINUS

    String Functions

    Various SQL string functions can be used in HANA with SQL scripting. Most common string functions are −
    • ASCII − It returns integer ASCII value of passed string.
    • CHAR − It returns the character associated with passed ASCII value.
    • CONCAT − It is Concatenation operator and returns the combined passed strings.
    • LCASE − It converts all character of a string to Lower case.
    • LEFT − It returns the first characters of a passed string as per mentioned value.
    • LENGTH − It returns the number of characters in passed string.
    • LOCATE − It returns the position of substring within passed string.
    • LOWER − It converts all characters in string to lowercase.
    • NCHAR − It returns the Unicode character with passed integer value.
    • REPLACE − It searches in passed original string for all occurrences of search string and replaces them with replace string.
    • RIGHT − It returns the rightmost passed value characters of mentioned string.
    • UPPER − It converts all characters in passed string to uppercase.
    • UCASE − It is identical to UPPER function. It converts all characters in passed string to uppercase.
    Other string functions that can be used are − LPAD, LTRIM, RTRIM, STRTOBIN, SUBSTR_AFTER, SUBSTR_BEFORE, SUBSTRING, TRIM, UNICODE, RPAD, BINTOSTR

    Date Time functions

    There are various Date Time functions that can be used in HANA in SQL scripts. Most common Date Time functions are −
    • CURRENT_DATE − It returns the current local system date.
    • CURRENT_TIME − It returns the current local system time.
    • CURRENT_TIMESTAMP − It returns the current local system timestamp details (YYYY-MM-DD HH:MM:SS:FF).
    • CURRENT_UTCDATE − It returns current UTC (Greenwich Mean date) date.
    • CURRENT_UTCTIME − It returns current UTC (Greenwich Mean Time) time.
    • CURRENT_UTCTIMESTAMP
    • DAYOFMONTH − It returns the integer value of day in passed date in argument.
    • HOUR − It returns integer value of hour in passed time in argument.
    • YEAR − It returns the year value of passed date.
    Other Date Time functions are − DAYOFYEAR, DAYNAME, DAYS_BETWEEN, EXTRACT, NANO100_BETWEEN, NEXT_DAY, NOW, QUARTER, SECOND, SECONDS_BETWEEN, UTCTOLOCAL, WEEK, WEEKDAY, WORKDAYS_BETWEEN, ISOWEEK, LAST_DAY, LOCALTOUTC, MINUTE, MONTH, MONTHNAME, ADD_DAYS, ADD_MONTHS, ADD_SECONDS, ADD_WORKDAYS

    Data Type Conversion Functions

    These functions are used to convert one data type to other or to perform a check if conversion is possible or not.
    Most common data type conversion functions used in HANA in SQL scripts −
    • CAST − It returns the value of an expression converted to a supplied data type.
    • TO_ALPHANUM − It converts a passed value to an ALPHANUM data type
    • TO_REAL − It converts a value to a REAL data type.
    • TO_TIME − It converts a passed time string to the TIME data type.
    • TO_CLOB − It converts a value to a CLOB data type.
    Other similar Data Type conversion functions are − TO_BIGINT, TO_BINARY, TO_BLOB, TO_DATE, TO_DATS, TO_DECIMAL, TO_DOUBLE, TO_FIXEDCHAR, TO_INT, TO_INTEGER, TO_NCLOB, TO_NVARCHAR, TO_TIMESTAMP, TO_TINYINT, TO_VARCHAR, TO_SECONDDATE, TO_SMALLDECIMAL, TO_SMALLINT
    There are also various Windows and other miscellaneous functions that can be used in HANA SQL scripts.
    • Current_Schema − It returns a string containing the current schema name.
    • Session_User − It returns the user name of current session
  • CONTENT

    We are providing SAP HANA Administration Materials.

    CONTACT US

    For enquiries you can contact us in several different ways. Contact details are below.

    VENKAT SAP HANA

    • Street :Hyderabad
    • Person :Venkat
    • Country :INDIA
    • Email :venkatsaptraining@gmail.com

    For any Query,Please leave message

    Learn SAP HANA Be with latest update.