diff --git a/doc/sql_migration/sql_migration04.md b/doc/sql_migration/sql_migration04.md index ee43d5f20b53..6e96df9102fb 100644 --- a/doc/sql_migration/sql_migration04.md +++ b/doc/sql_migration/sql_migration04.md @@ -870,6 +870,93 @@ The example below shows migration when the root data is displayed. +##### 4.2.3.7 Hierarchical Query identifys the leaves + +**Functional differences** + + - **Oracle database** + - Specifying CONNECT_BY_ISLEAF in the select list of a hierarchical query can identify the leaf rows. This returns 1 if the current row is a leaf. Otherwise it returns 0. + - **PostgreSQL** + - CONNECT_BY_ISLEAF cannot be specified. + +**Migration procedure** + +In a recursive query that uses a WITH clause, the leaf can be checked using a sub-query so that the same result is returned. Use the following procedure to perform migration: + +1. Replace the hierarchical query with syntax that uses a recursive query (WITH clause). +2. Add a sub-query to the column list of the query result of the WITH clause. + +The following shows the conversion format containing rootName. + +~~~ + WITH RECURSIVE queryName( + columnUsed1, columnUsed2 +) AS +( SELECT columnUsed, columnUsed2 + FROM targetTableOfHierarchicalQuery + UNION ALL + SELECT columnUsed(qualified by n), columnUsed2(qualified by n) + FROM targetTableOfHierarchicalQuery n, + queryName w + WHERE conditionalExprOfConnectByClause +) +SELECT *, + CASE WHEN EXISTS(select * from queryName p where p.columnUsed1 = e.columnUsed2) + THEN 0 ELSE 1 END + as is_leaf + FROM queryName e; +~~~ + +For conditionalExprOfConnectByClause, use w to qualify the part qualified by PRIOR. + +**Migration example** + +The example below shows migration when the leaf data is displayed. +
Oracle database | +PostgreSQL | +
---|---|
+
+ |
+
+
+
+ |
+