Apologies for creating a new thread. I was not able to frame the question correctly in the previous thread.
I have a table in SQL Server 2008 that looks like this –
Id Status
--------------
1 A
2 I
3 NULL
I have a stored procedure which takes @Status as the parameter.
If the @Status is "-1", I need to retrieve records 1, 2.
If the @Status is "A", I need to retrieve only record 1.
If the @Status is "I", I need to retrieve only record 2.
I want to be able to retrieve the records in a single SELECT statement without using IF ELSE. There are a lot of joins and stuff in the SELECT statement and I don’t want to repeat the same thing in the ELSE condition.
I have tried the following but get incorrect data –
SELECT * FROM Person WHERE Status = @Status OR @Status IS NOT NULL
select * from Person
where (@Status is not null and Status = @Status) or
(@Status is null and Status in ('A', 'P'))
You can do it using OR:
Of course you shouldn’t use
SELECT *in production code.