oarcle with recursive
时间: 2023-07-01 08:06:55 浏览: 109
`WITH RECURSIVE` is a SQL clause that allows for recursive queries to be made. The `WITH RECURSIVE` clause defines a recursive common table expression (CTE), which is a temporary named result set that can be referenced within the main query.
In the context of Oracle, the `WITH RECURSIVE` clause is not supported natively. However, there are workarounds that can be used to achieve similar functionality. One common method is to use a hierarchical query with the `CONNECT BY` clause.
Here's an example of a recursive query using `CONNECT BY`:
```
WITH
data(id, parent_id, name) AS (
SELECT 1, NULL, 'Root' FROM DUAL UNION ALL
SELECT 2, 1, 'Child 1' FROM DUAL UNION ALL
SELECT 3, 1, 'Child 2' FROM DUAL UNION ALL
SELECT 4, 2, 'Grandchild 1' FROM DUAL UNION ALL
SELECT 5, 3, 'Grandchild 2' FROM DUAL
)
SELECT id, parent_id, name, LEVEL
FROM data
CONNECT BY PRIOR id = parent_id
START WITH parent_id IS NULL;
```
This query returns a hierarchical result set that represents a tree structure. The `CONNECT BY` clause specifies the join conditions between parent and child rows, and the `START WITH` clause specifies the root of the tree. The `LEVEL` pseudo-column is used to display the depth of each row in the tree.
阅读全文