tags:

views:

60

answers:

4

If one of the paramater is passed / or both passed how to match that to single column in WHERE clause.

If both are passed should get result set just with the first one else with second one.

Ex:

--either will be passed
declare @processStepID int = null
declare @PROCESS_StepID int = null

if(@processStepID is null)
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES 
where PROCESS_STEP_ID = @PROCESS_StepID
else
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES 
where PROCESS_STEP_ID = @processStepID

How can this be written in where clause not using if statement.

thanks

A: 
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES 
where (PROCESS_STEP_ID = @PROCESS_StepID) OR (PROCESS_STEP_ID = @processStepID)

Seems far too obvious. What am I missing?

Simon Righarts
+1  A: 

Use the ISNULL function

SELECT *
FROM CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES 
WHERE PROCESS_STEP_ID = ISNULL( @processStepID, @PROCESS_StepID )
martin clayton
+1 beat me too it.
Irwin M. Fletcher
A: 
select * 
from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES 
where PROCESS_STEP_ID = ISNULL(@PROCESS_StepID,@processStepID)

This should do what you are looking for

Irwin M. Fletcher
Thanks Irwin. Incase both are passed i should be able to get resultset based on @processStepID.
Nev_Rahd
+3  A: 
WHERE PROCESS_STEP_ID = coalesce( @processStepID, @PROCESS_StepID )
Damir Sudarevic
+1: COALESCE is preferrable over db specific null check syntax
OMG Ponies