0

我有以下查询:

SELECT U.NAME , U.TYPE , U.REFERENCED_NAME , U.REFERENCED_TYPE,
CASE 
WHEN U.REFERENCED_TYPE= 'TABLE' and A.TABLE_NAME= U.REFERENCED_NAME 
  THEN 'TABLE EXISTS'
WHEN U.REFERENCED_TYPE= 'PROCEDURE' and A.TABLE_NAME= U.REFERENCED_NAME 
   THEN 'PROCEDURE EXISTS'
END
FROM  USER_DEPENDENCIES U , user_tables a  
WHERE U.name ='RANDOM_PROCEDURE_NAME' and U.REFERENCED_NAME = A.TABLE_NAME;

我在这个查询中的问题是: U.REFERENCED_NAME = A.TABLE_NAME这样的连接只给了我特定的条件。我想在表 USER_DEPENDENCIES 中知道存在的表,即使它不存在于表 user_tables 中。我想包括一个外部连接,但我不能这样做。任何人都可以帮助我吗?

4

4 回答 4

1

像这样的东西?

SELECT
      U.NAME,
      U.TYPE,
      U.REFERENCED_NAME,
      U.REFERENCED_TYPE,
      CASE
          WHEN U.REFERENCED_TYPE = 'TABLE'
              AND A.TABLE_NAME = U.REFERENCED_NAME
          THEN
              'TABLE EXISTS'
          WHEN U.REFERENCED_TYPE = 'PROCEDURE'
              AND A.TABLE_NAME = U.REFERENCED_NAME
          THEN
              'PROCEDURE EXISTS'
          ELSE
              'NOT DEFINED'
      END
          TYPE_1
FROM
      USER_DEPENDENCIES U,
      USER_TABLES A
WHERE
      U.NAME LIKE '%TEST%'
      AND U.REFERENCED_NAME = A.TABLE_NAME(+)
于 2013-10-23T11:08:17.457 回答
1

使用OUTER JOIN

FROM  
    USER_DEPENDENCIES u 
  LEFT OUTER JOIN
    user_tables a  
      ON u.REFERENCED_NAME = a.TABLE_NAME
WHERE 
    u.name ='RANDOM_PROCEDURE_NAME'  ; 

(左)表中的所有行USER_DEPENDENCIES,无论它们是否有匹配的行user_tables

于 2013-10-23T12:03:09.693 回答
1

这为您提供了对对象的所有依赖项。对于表和过程,它显示它们中的哪些存在于您的数据库模式中:

select dep.name , dep.type , dep.referenced_name , dep.referenced_type,
case 
  when dep.referenced_type = 'TABLE' and tab.table_name is not null then
    'TABLE EXISTS'
  when dep.referenced_type = 'PROCEDURE' and pro.procedure_name is not null then
    'PROCEDURE EXISTS'
end as lookup
from user_dependencies dep
left outer join user_tables tab on (dep.referenced_type = 'TABLE' and tab.table_name = dep.referenced_name) 
left outer join user_procedures pro on (dep.referenced_type = 'PROCEDURE' and pro.procedure_name = dep.referenced_name) 
where dep.name ='RANDOM_PROCEDURE_NAME';
于 2013-10-23T12:05:13.180 回答
1

请使用LEFT OUTER JOIN而不是INNER JOIN. 替换WHEREON您的查询,如下所示:

SELECT U.NAME , U.TYPE , U.REFERENCED_NAME , U.REFERENCED_TYPE,
CASE 
WHEN U.REFERENCED_TYPE= 'TABLE' and A.TABLE_NAME= U.REFERENCED_NAME 
  THEN 'TABLE EXISTS'
WHEN U.REFERENCED_TYPE= 'PROCEDURE' and A.TABLE_NAME= U.REFERENCED_NAME 
   THEN 'PROCEDURE EXISTS'
END
FROM  USER_DEPENDENCIES U 
LEFT JOIN user_tables a  
ON U.name ='RANDOM_PROCEDURE_NAME' and U.REFERENCED_NAME = A.TABLE_NAME;

希望这会帮助你。谢谢。:)

于 2013-10-23T12:40:42.817 回答