Conditional Operator in SQL Where Clause

Posted by Marc on Stack Overflow See other posts from Stack Overflow or by Marc
Published on 2010-04-02T19:37:04Z Indexed on 2010/04/02 19:43 UTC
Read the original article Hit count: 529

Filed under:
|
|
|

I'm wishing I could do something like the following in SQl Server 2005 (which I know isnt valid) for my where clause. Sometimes @teamID (passed into a stored procedure) will be the value of an existing teamID, otherwise it will always be zero and I want all rows from the Team table.

I researched using Case and the operator needs to come before or after the entire statement which prevents me from having a different operator based on the value of @teamid. Any suggestions other than duplicating my select statements.

    declare @teamid int
    set @teamid = 0

    Select Team.teamID From Team
      case @teamid
         when 0 then 
            WHERE Team.teamID > 0
         else
            WHERE Team.teamID = @teamid
      end 

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server