Overview
Converting SQL queries from ORACLEto ALTIBASE HDB might be easy because ALTIBASE HDB has almost same SQL syntax with ORACLE.
Because of compatibility with ORACLE, ALTIBASE HDB try to accept Oracle syntax and features such as rownum psedocolumn, hierachy query, ORACLE-like bulit-in functions and so on. However,
because ALTIBASE HDB is not identical to ORACLE, you need to convert some SQL queries from ORACLE to ALTIBASE HDB if there're ORACLE proprietary features not provided by ALTIBASE HDB.
From ALTIBASE HDB V6, ALTIBASE HDB supports ORACLE-Style outer join statement.
Besides, Pivot Query, Ranking Functions have been applied at ALTIBASE HDB V6.
In this chapter, we will discuss
- ORACLE features not provided by ALTIBASE HDB
- Workarounds when you use ORACLE-supported features not supported by ALTIBASE HDB
OUTER JOIN expression
ALTIBASE HDB supports three types of outer join: right, left, and full.
The syntax of outer join is slightly different between ORACLE and ALTIBASE HDB.
Converting example from ORACLE outer join to ALTIBASE HDB outer join is as follows.
Besides workarounds described above, ALTIBASE HDB supports officially ORACLE-Style Outer Join from ALTIBASE HDB V6.
ALTIBASE HDB does support Pivot query from ALTIBASE HDB V6 as well.
An ORACLE statement shown below is completely compatible with an ALTIBASE HDB statement.
ORACLE
ALTIBASE HDB
or
Table order specified after FROM clause is important to use ANSI-SQL outer join.
Because in case of the outer join statement, it is bound to execute full table scan by accessing all rows of a Table order specified after FROM clause.
It frequently causes SQL Tuning issue.
Hierarchy Query
ALTIBASE HDB provides the limited functionality about hierarchy query.
ALTIBASE HDB does not support following features among of those of Oracle provides
- CONNECT_BY_ISCYCLE pseudocolumn
- CONNECT_BY_ISLEAF pseudocolumn
- CONNECT_BY_ROOT operator
- SYS_CONNECT_BY_PATH function
- SIBLINGS in order by clause
Aside from CONNECT_BY_ISCYCLE and SIBLINGS, there're workarounds to replace above features.
Assume that we have a table named PC and the table stores data as below.
ITEM_ID |
PARENT_ID |
ITEM_NAME |
ITEM_QTY |
---|---|---|---|
1001 |
NULL |
Computer |
1 |
1002 |
1001 |
BODY |
1 |
1003 |
1001 |
monitor |
1 |
1004 |
1001 |
printer |
1 |
1005 |
1002 |
Mother board |
1 |
1006 |
1002 |
Lan card |
1 |
1007 |
1002 |
Power Supply |
1 |
1008 |
1005 |
RAM |
1 |
1009 |
1005 |
CPU |
1 |
1010 |
1005 |
Graphic device |
1 |
1011 |
1005 |
ETC device |
1 |
- CONNECT_BY_ROOT
ORACLE
ALTIBASE HDB
- CONNECT_BY_IS_LEAF
ORACLE
ALTIBASE HDB
SYS_CONNECT_BY_PATH
To convert this pseudocolumn to ALTIBASE, you can use a user-defined function as shown below.
ORACLE
ALTIBASE HDB
- NOCYCLE in ORACLE
ALTIBASE has IGNORE LOOP keyword corresponding to NOCYCLE in Oracle. Here's an example.Oracle
ALTIBASE HDB
About analytic features regarding ranking functions, Oracle provides ranking functions as follows at ALTIBASE HDB V6.
- ROW_NUMBER()
- RANK()
- DENSE_RANK()
But ALTIBASE HDB does not support above functions before ALTIBASE HDB V6.
So, if you wish to use above functions, you need to convert SQL.
Let's assume that we have a table named EMP which stores data as below.
ENAME |
DEPTNO |
SAL |
---|---|---|
SMITH |
20 |
800 |
ALLEN |
30 |
1600 |
WARD |
30 |
1250 |
JONES |
20 |
2975 |
MARTIN |
30 |
1250 |
BLAKE |
30 |
2850 |
CLARK |
10 |
2450 |
KING |
10 |
5000 |
TURNER |
30 |
1500 |
JAMES |
30 |
950 |
FORD |
20 |
3000 |
MILLER |
10 |
1300 |
SCOTT |
20 |
3000 |
ADAMS |
20 |
1100 |
Here're workaround samples when you want to convert ORACLE ranking functions to ALTIBASE HDB.
Besides described below, ALTIBASE HDB ranking functions are almost identical with ORACLE.
Those were applied at ALTIBASE HDB Vesion 6
- ROW_NUMBER()
ORACLE
ALTIBASE HDB
- ROW_NUMBER PARTITION BY
ORACLE
ALTIBASE HDB
- RANK()
ORACLE
ALTIBASE HDB
- RANK() PARTITION BY
ORACLE
ALTIBASE HDB
- DENSE_RANK()
ORACLE
ALTIBASE HDB
- DENSE_RANK PARTITION BY
ORACLE
ALTIBASE HDB
ROLLUP / CUBE
Let's assume that we have a table named SALES which stores data as shown below.
GROUP_ID |
SALES_EMP |
SALES_QTY |
---|---|---|
A |
test1 |
5 |
A |
test2 |
10 |
A |
test3 |
1 |
B |
test4 |
10 |
B |
test5 |
5 |
C |
test6 |
50 |
A |
test1 |
5 |
A |
test2 |
10 |
- ROLLUP
ORACLE
ALTIBASE HDB
- CUBE
ORACLE
ALTIBASE HDB
PIVOT / UNPIVOT
Pivot queries involve transposing rows into columns (pivot) or columns into rows (unpivot) to generate results in crosstab format.
Use sum(decode) function to get appropriate result as follows.
For pivotting, table named pivot_test is described as shown below.
ID |
CUSTOMER_ID |
PRODUCT_CODE |
QUANTITY |
---|---|---|---|
1 |
1 |
A |
10 |
2 |
1 |
B |
20 |
3 |
1 |
C |
30 |
4 |
2 |
A |
40 |
5 |
2 |
C |
50 |
6 |
3 |
A |
60 |
7 |
3 |
B |
70 |
8 |
3 |
C |
80 |
9 |
3 |
D |
90 |
10 |
4 |
A |
100 |
PIVOT
ORACLE
ALTIBASE HDB
UNPIVOT
ORACLE
ALTIBASE HDB
MERGE statement
MERGE statement is convenient way to combine multiple operations.
ALTIBASE HDB does not support MERGE statement yet.
If your application uses MERGE statement, please seperate MERGE statement into INSERT, UPDATE, DELETE statement according to the kinds of transaction or procedure as below.
Merge statement has a workaround using procedure as follows.
Using this statement to insert row, if it already exists, update or delete operation will be available instead of insert operation.
On the contrary, when no duplication detected, insert operation might be executed instead.
RETURNING clause
This function is used to return value of the column to the bind variable after the execution of SQL.
Analytic Features
Here are explanations about aggregate functions except those described at the ranking function above.
This function computes an aggregate value based on a group of rows.
They differ from aggregate functions in that they return multiple rows for each group.
FIRST_VALUE
It returns the first value in an ordered set of values.
If the first value in the set is null, then the function returns NULL unless you
specify IGNORE NULLS.
ORACLE
ALTIBASE HDB
LAST_VALUE
It returns the last value in an ordered set of values.
If the last value in the set is null, then the function returns NULL unless you
specify IGNORE NULLS.
ORACLE
ALTIBASE HDB