Madhu
Madhu

Reputation: 5766

Multiple Oracle Query

I have 2 sql's (each sql is about 10 lines joining about 5 tables). These 2 sqls returns same column. I want to execute sql2 only if sql1 returns no result.

How to do this in one sql?

Upvotes: 1

Views: 52

Answers (1)

Benoit
Benoit

Reputation: 79233

query1
UNION ALL
( query2
   WHERE NOT EXIST (query1)
)

In Oracle you can also factor:

WITH conditional_query AS (query1)
SELECT * FROM conditional_query
UNION ALL
( query2 WHERE NOT EXIST (SELECT NULL FROM conditional_query) )

Upvotes: 1

Related Questions