I had a user request to be able to select data by employee ID value or Employee Name value. In the event neither is requested pull all employees.
My first impulse was to create two variables: @EmpID and @Name
then
select a,b,c,d,e
where
(
c between date1 and date 2
and
d > 0
)
and
(
a in (@EmpID)
or
b = @Name
)
It works great if I supply values for @Empid or @Name.
What I can’t figure out is how to get it to return everyone if @EmpID is null and @Name is null.
Add an extra
ORstatement to yourWHEREclause:If both
@EmpIDand@NameareNULL, then all employees will be returned. Keep in mind that theWHEREclause:will still be applied to any employees selected.