-2

I have the below SQL and giving me expression errors.

I want to check if the record exists, If Exists, then I want to execute one sql and get column values, If not I want to execute another sql and get columns values.

How can I do this oracle ?

DECLARE 
  VALIDEXISTS NUMBER;
BEGIN
  SELECT * 
    FROM table1 
   WHERE column1 IN ('Yes')
     AND columns2 IN (
                       SELECT COUNT(column1) AS VALIDEXISTS 
                         FROM table1 
                        WHERE column1 IN ('Yes') 
                          AND column2 NOT LIKE '%Yes%'
                           IF VALIDEXISTS = 0 THEN
                             SELECT column2 
                               FROM table1 
                              WHERE column1 IN ('Yes') 
                                AND column2 NOT LIKE '%Yes%'


                           ELSE 
                             SELECT column2 
                               FROM table1 
                               WHERE column1 IN ('Yes') 
                                 AND column2 NOT LIKE '%No%'    END IF; )
END
Chatra
  • 2,989
  • 7
  • 40
  • 73

1 Answers1

2

Calculate the condition first, then apply it to the final SELECT using CASE. Something like this:

with temp (validexists) as
  (select count(column1)
   from table1
   where column1 in ('Yes') 
      and column2 not like '%Yes%'
  )
  select *
    into l_row
    from table1
    where column1 in ('Yes')
      and column2 in (select column2
                      from table1 cross join temp
                      where column1 in ('Yes')
                        and column2 not like case when validexists = 0 then '%Yes%'
                                                  else                      '%No%'
                                             end
                     );
Littlefoot
  • 131,892
  • 15
  • 35
  • 57