0

I have two queries. I want to run the first select statement and only if it does not return any record then I want to run the second statement. How can I achieve this. These are my two statements:

select * from drscan.oem_program p 
WHERE upper(OEM_PROGRAMID) LIKE upper('%Subvented%')
  and oem_code = 'NCI'
  and rownum=1
  and p.ISACTIVE='Y'

select *
from drscan.oem_program p 
where oem_code ='NCI'
  and p.PROGRAMCATEGORY='NEW'
  and p.ISACTIVE='Y'
  and trunc(p.STARTDATE)<=trunc(sysdate)
  and trunc(p.ENDDATE)>= trunc(sysdate)
  and rownum=1

1 Answer 1

2

In SQL (not PL/SQL) you can do it like this:

with p as (
  select * from drscan.oem_program p 
  WHERE upper(OEM_PROGRAMID) LIKE upper('%Subvented%')
    and oem_code = 'NCI'
    and rownum=1
    and p.ISACTIVE='Y'
)
, p2 as (
    select *
  from drscan.oem_program p 
  where oem_code ='NCI'
    and p.PROGRAMCATEGORY='NEW'
    and p.ISACTIVE='Y'
    and trunc(p.STARTDATE)<=trunc(sysdate)
    and trunc(p.ENDDATE)>= trunc(sysdate)
    and rownum=1
)
select * from p
union all 
select * from p2 where not exists(select 1 from p)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.