Eswar Vignesh Eswar Vignesh - 6 months ago 8
SQL Question

select in case or if statement?

Is it possible to add a select statement in case or if function in SQL

select case when :A='do' then (select col1 from table1) else 'n/a' end
;

or

select if(:A='do',(select col1 from table1),'N/A');


If my parameter is 'do' it should display all the value in table or else it should just display 'N/A'.

Please help me. Thanks!

Answer

If this is Oracle, as per your tag, then neither of your queries are valid as they're not actually selecting from a table.

Perhaps what you're after is something like:

select case when :A='do' then col1 else 'n/a' end col
from   table1;

Or maybe you're after something like:

select col1
from   table1
where  :A != 'do'
union all
select 'N/A' col1
from   dual
where  :A = 'do';

You didn't provide any example data, so I'm not sure if what you're trying to do is make all the values of col1 appear as 'N/A' if the bind variable is "do" or whether you only want a single row containing 'N/A'.

Comments