Skip to content
Advertisement

Print a custom message when no rows are returned by a SELECT statement

I am trying to create a report of INVALID objects in Oracle. The requirement is to print a custom message when no rows are returned (This is because the output will be redirected to an HTML report). I tried the following query:

Error am seeing:

While the individual queries work without an issue, it fails to execute when a UNION is introduced. How can I fix this?

Advertisement

Answer

It can’t work that way. If you’re using UNION, SELECT statements have to return the same number of columns which match in datatype.

This is an invalid procedure (created in my SYS schema for simplicity; don’t do that yourself):

Query you’d run; notice NOT EXISTS in the second query – it’ll prevent the message to be displayed if there are some invalid objects. Also, I modified date condition to “today” (subtracted 0) as I can’t wait until tomorrow to post it.

If I drop the invalid procedure:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement