Oracle 中的递归

发布于 2024-10-11 04:04:55 字数 622 浏览 2 评论 0原文

我在预言中有下表:

Parent(arg1, arg2)

并且我想要关系父级的传递闭包。也就是说,我想要下表,

Ancestor(arg1, arg2)

这在Oracle中怎么可能?

我正在执行以下操作:

WITH Ancestor(arg1, arg2)  AS (

  SELECT p.arg1, p.arg2 from parent p
  UNION
  SELECT p.arg1 , a.arg2 from parent p,  Ancestor a 
  WHERE p.arg2 = a.arg1

)

SELECT DISTINCT * FROM Ancestor;

我收到错误

*Cause:    column aliasing in WITH clause is not supported yet
*Action:   specify aliasing in defintion subquery and retry
Error at Line: 1 Column: 20

如何在没有列别名的情况下解决此问题?

I have the following table in an oracle:

Parent(arg1, arg2)

and I want the transitive closure of the relation parent. That is, I want the following table

Ancestor(arg1, arg2)

How is this possible in Oracle?

I am doing the following:

WITH Ancestor(arg1, arg2)  AS (

  SELECT p.arg1, p.arg2 from parent p
  UNION
  SELECT p.arg1 , a.arg2 from parent p,  Ancestor a 
  WHERE p.arg2 = a.arg1

)

SELECT DISTINCT * FROM Ancestor;

I get the error

*Cause:    column aliasing in WITH clause is not supported yet
*Action:   specify aliasing in defintion subquery and retry
Error at Line: 1 Column: 20

How can I solve this without column aliasing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烦人精 2024-10-18 04:04:55
WITH    Ancestor(arg1, arg2) AS
        (
        SELECT  p.arg1, p.arg2
        FROM    parent p
        WHERE   arg2 NOT IN
        (
            SELECT  arg1
            FROM    parent
        )

        UNION ALL

        SELECT  p.arg1, a.arg2
        FROM    Ancestor a 
        JOIN    parent p
        ON      p.arg2 = a.arg1
        )
SELECT  *
FROM    Ancestor

Oracle 仅从 11g 第 2 版开始支持递归 CTE

在早期版本中,使用 CONNECT BY 子句:

SELECT  arg1, CONNECT_BY_ROOT arg2
FROM    parent
START WITH
        arg2 NOT IN
        (
        SELECT  arg1
        FROM    parent
        )
CONNECT BY
        arg2 = PRIOR arg1
WITH    Ancestor(arg1, arg2) AS
        (
        SELECT  p.arg1, p.arg2
        FROM    parent p
        WHERE   arg2 NOT IN
        (
            SELECT  arg1
            FROM    parent
        )

        UNION ALL

        SELECT  p.arg1, a.arg2
        FROM    Ancestor a 
        JOIN    parent p
        ON      p.arg2 = a.arg1
        )
SELECT  *
FROM    Ancestor

Oracle only supports recursive CTE since 11g Release 2.

In earlier versions, use CONNECT BY clause:

SELECT  arg1, CONNECT_BY_ROOT arg2
FROM    parent
START WITH
        arg2 NOT IN
        (
        SELECT  arg1
        FROM    parent
        )
CONNECT BY
        arg2 = PRIOR arg1
时间海 2024-10-18 04:04:55

Oracle 允许递归查询。
请参阅:http://www.adp-gmbh.ch/ora/sql/connect_by .html

当然,这些通常假设分层数据都在一张表中。将其拆分为单独的表会使事情变得复杂。

Oracle allows recursive queries.
See: http://www.adp-gmbh.ch/ora/sql/connect_by.html

Of course, these usually assume the hierarchical data is all in one table. Splitting it into separate tables makes things complicated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文