我在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
它工作正常(我得到一些行)。但是,当我修改此查询的最后几行(最后一个SELECT)时:
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表,因此结果应与第一个查询中的结果相同,但它们不是。
答案 0 :(得分: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';