Learner
Learner

Reputation: 786

IF ELSE Statement in WHERE Clause SQL

I want to achieve the following from sql, Suppose I have @accountId int as a parameter in stored procedure, now if I pass parameter as 99999 then in all the select statements it Select WHERE AccountId <> 124 otherwise if I Pass any other parameter it Selects WHERE AccountId=@accountId. This statement is a part of a very long stored procedure, so I haven't Included whole procedure.

WHERE (IF(AccountId = 99999) 
       BEGIN
          AccountId <> 124
       END
       ELSE
       BEGIN
          AccountId = @accountId
       END)

OR Can I achieve similar from CASE Statement. Something Like,

WHERE (AccountId(CASE(AccountId) WHEN 99999 THEN <>124 ELSE =@accountId))

I have edited my question, I apologize for asking it in unrelated manner. Please let me know if it makes sense for you or you need any further information.

Upvotes: 0

Views: 204

Answers (1)

paparazzo
paparazzo

Reputation: 45096

WHERE (AccountId =  1 and  SenderId <> 10) 
   or (AccountId <> 1 and  SenderId =  10) 

WHERE (@AccountId =  9999 and  SenderId <> 10) 
   or (@AccountId <> 9999 and  SenderId =  @AccountId)

Upvotes: 2

Related Questions