Reputation: 3602
Let's say I have two Oracle SQL queries that I'm using in various places in my code:
select *
from employees
where manager_id IS NULL
and etc etc etc...
and
select *
from employees
where manager_id IS NOT NULL
and etc etc etc...
The only difference between the two queries is that manager_id is NULL in the first one, and NOT NULL in the second one.
Is there any way to write this as one generic statement? Is it possible to pass in NULL or NOT NULL in a parameter? Something like this:
select *
from employees
where manager_id = ?
and etc etc etc...
I know the above example doesn't work (and I know why). My question is more along the lines of is there a more elegant solution than managing two separate SQL strings that are 99% similar?
Upvotes: 1
Views: 200
Reputation: 1269693
You would need to use more complex logic. Perhaps:
where ( (? = 'not null' and manager_id is not null) or
(? = 'null' and manager_id is null)
)
Upvotes: 8