0

我在 Oracle 中有一个查询。此查询的结果应该是父表的“子”表列表:

WITH tempTab AS
 (SELECT owner          ,
         constraint_name,
         table_name
 FROM    sys.all_constraints
 WHERE   owner           = 'PARENT_OWNER'
 AND     table_name      = 'PARENT_TABLE'
 AND     constraint_type = 'P'
 )
 ,
 acTemp AS
 (SELECT DISTINCT ac.owner                       AS child_owner,
                                   ac.table_name AS child_table,
                                   ac.constraint_name
                  FROM             sys.all_constraints ac,
                                   tempTab tt
                  WHERE            ac.constraint_type   = 'R'
                  AND              ac.r_constraint_name = tt.constraint_name
 )
SELECT act.child_owner,
   act.child_table
FROM   acTemp act

它工作正常(我得到了一些行)。但是当我修改这个查询的最后几行(最后一个选择)时:

WITH tempTab AS
 (SELECT owner          ,
         constraint_name,
         table_name
 FROM    sys.all_constraints
 WHERE   owner           = 'PARENT_OWNER'
 AND     table_name      = 'PARENT_TABLE'
 AND     constraint_type = 'P'
 )
 ,
 acTemp AS
 (SELECT DISTINCT ac.owner                       AS child_owner,
                                   ac.table_name AS child_table,
                                   ac.constraint_name
                  FROM             sys.all_constraints ac,
                                   tempTab tt
                  WHERE            ac.constraint_type   = 'R'
                  AND              ac.r_constraint_name = tt.constraint_name
 )
SELECT act.child_owner,
   act.child_table
FROM   acTemp act ,
   acTemp act2,
   tempTab tt
WHERE  tt.owner      = act2.child_owner
AND    tt.table_name = act2.child_table

我没有得到任何行。为什么?在第二个查询中,我不过滤 act 表,因此结果应该与第一个查询中的相同,但事实并非如此。

4

1 回答 1

0

尝试这样的事情:

SELECT
     parent_table.owner
    ,parent_table.table_name AS parent_table
    ,child_table.table_name AS child_table
    ,parent_table.constraint_name AS constraint_name
FROM sys.all_constraints parent_table
INNER JOIN sys.all_constraints child_table ON ( child_table.constraint_name = parent_table.r_constraint_name AND child_table.owner = parent_table.owner)
WHERE parent_table.owner = 'PARENT_OWNER'
AND parent_table.table_name = 'PARENT_TABLE'
AND parent_table.constraint_type = 'R';
于 2013-02-06T12:35:10.097 回答