Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
219 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
                     );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...