As of now I am using IF ELSE to handle this condition
IF INPUT_PARAM IS NOT NULL
SELECT ... FROM SOMETABLE WHERE COLUMN = INPUT_PARAM
ELSE
SELECT ... FROM SOMETABLE
Is there any better way to do this in a single query without IF ELSE loops. As the query gets complex there will be more input parameters like this and the amount of IF ELSE required would be too much.
One method would be to use a variant of
There are two pitfalls here however:
if the column is nullable, this clause will filter null values whereas in your question you would not filter the null values in the second case. You could modify this clause to take nulls into account but it turns ugly:
Of course if somehow the
impossible_valueis ever inserted you will run into some other kind of (fun) problems.nvl, you will get full scan even if perfectly valid indexes are present.This is why when there are lots of parameters (several search fields in a big form for example), I like to use dynamic SQL:
You can also use
EXECUTE IMMEDIATE l_query INTO l_result USING param1;