I suspect that the problem is that you have no rows. When you return a row with a null
column, coalesce
or nvl
will do exactly what you expect. If you need to return a value when when the result set is empty, you need to use a bit more trickery. You can do that like this:
SELECT NVL (t1.c1, 'NO RECORDS'),
t1.c2,
t2.c3,
t1.c4
FROM temp t1, temp_1 t2
WHERE t1.c2 = t2.c2 AND t1 IN ('T001', 'T002')
UNION ALL
SELECT 'NO_RECORDS',
NULL,
NULL,
NULL
FROM DUAL
WHERE NOT EXISTS
(SELECT *
FROM temp t1, temp_1 t2
WHERE t1.c2 = t2.c2 AND t1 IN ('T001', 'T002'));
However, if this is intended to be interpreted by code somewhere, you'd be better off determining that no rows were returned via the available metadata rather than from a magic value.
If c1 is a number or date, then you'll need to use either CAST
or TO_CHAR
to convert it to a varchar2 in order to make this work. Queries merged via UNION
must have the same data type for each column.