case语句中的外连接

时间:2013-10-23 11:00:43

标签: oracle outer-join

我有以下查询:

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 此类加入仅向ME提供特定条件。我希望表USER_DEPENDENCIES知道即使它确实存在的表存在于表user_tables中。我想包括一个外部联接但我无法做到这一点。谁能帮助我?

4 个答案:

答案 0 :(得分: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(+)

答案 1 :(得分: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处都有匹配的行。

答案 2 :(得分: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';

答案 3 :(得分:1)

请使用LEFT OUTER JOIN代替INNER JOIN。将WHERE替换为ON您的QUERY,如下所示:

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;

希望这会对你有所帮助。谢谢。 :)