Reputation: 341
I am trying to execute Select query with ID parameter, now if ID is empty then I want All Rows, otherwise only row which contains this ID, So far, I have created this Store Procedure,
IF(@CustId = null)
Begin
Select * from tblCustomer
End
Else
Begin
Select * from tblCustomer where custID=@custID
End
When I am executing this query with ID, I am getting result but when I am passing, I am not getting any result. What's correct way to do this?
Upvotes: 1
Views: 1761
Reputation: 17540
Null is handled differently than other values, you need to use
IF (@CustId IS NULL)
Your current code of IF(@CustId = null)
will always evaluate to False, so the else case will be executed regardless of what you pass for @CustId
. This article goes into more detail about how to handle NULL values:
Upvotes: 3