I've read loads of q&a's on here but none fit the bill for what I'm trying to achieve. I don't even know if this is possible! Any help/suggestions would be greatly appreciated.
I'm trying to build a conditional WHERE
clause based on
CustID (int) = @CustomerID
If @CustomerID = 0
then I want the result to be WHERE CustID > 0
(i.e. return all customers) otherwise I only want certain customers CustID = @CustomerID
Is this possible?
Just do WHERE (@CustomerID = 0 OR CustID = @CustomerID)
something like this?
SELECT *
FROM YOURTABLE
WHERE
( CASE
WHEN @CustomerID = 0 THEN CustID
ELSE 1
END
) > 0
AND
( CASE
WHEN @CustomerID <> 0 THEN @CustomerID
ELSE CUSTID
END
) = CUSTID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With