在oracle查询

时间:2015-09-02 11:24:13

标签: sql oracle view case-when

我创建了一个OBJECT_STATUS视图,该视图工作正常。但是现在我在KPI_DEFINITION表中有很多行,并且我想在选择查询时应用循环,其中EXIST条件存在,这样我将获得具有select查询的所有KPI_DEF_ID和我将设置为'N'。但不知道该怎么做。是否可以在CASE WHEN语句中循环。

    CREATE OR REPLACE FORCE VIEW "RATOR_MONITORING"."OBJECT_STATUS" ("OBJECT_TYPE", "OBJECT_ID", "OBJECT_STATUS") AS 
      select "OBJECT_TYPE"
       ,"OBJECT_ID"
       ,"OBJECT_STATUS"
  from (select 'EVENT' as object_type
              ,os.event_id as object_id
              ,case
                  when not exists (select kd.kpi_def_id
                          from rator_monitoring_configuration.kpi_definition kd
                         where kd.event_id = os.event_id
                           and kd.kpi_type in (19, 21)) then
                   'N'
                  when exists (select kd.kpi_def_id
                          from rator_monitoring_configuration.kpi_definition kd
                         where kd.event_id = os.event_id
                           and kd.kpi_type in (19, 21)
                           and (kd.kpi_active_initial = 'N' or kd.kpi_active_current = 'N' or
                               kd.kpi_active_manual = 'N' or kd.kpi_active_downtime = 'N')) then
                   'N'
                  else
                   os.status
               end as object_status
          from event_status os)
 order by object_type
         ,decode(object_status, 'N', 1, 'R', 2, 'Y', 3, 'G', 4, 5)
         ,object_id;

1 个答案:

答案 0 :(得分:0)

您必须在'case statement'中添加“假”查询。

select object_name,object_type,
  case when 1 in (select 1 from dual where EXISTS (select 1 from user_tables tab where tab.table_name = obj.object_name)) 
  then 'IS_TABLE'
  when  1 in (select 1 from dual where not EXISTS (select 1 from user_tables tab where tab.table_name = obj.object_name)) 
  then 'NO_TABLE'
  else 'END' end case_with_exist
from user_objects obj
where object_type in ('TABLE','INDEX')
;