Conditional WHERE Clauses in SQL Server 2008

Posted by user336786 on Stack Overflow See other posts from Stack Overflow or by user336786
Published on 2010-05-09T19:53:22Z Indexed on 2010/05/09 19:58 UTC
Read the original article Hit count: 157

Filed under:

Hello,

I am trying to execute a query on a table in my SQL Server 2008 database. I have a stored procedure that uses five int parameters. Currently, my parameters are defined as follows:

@memberType int,
@color int,
@preference int,
@groupNumber int,
@departmentNumber int

This procedure will be passed -1 or higher for each parameter. A value of -1 means that the WHERE clause should not consider that parameter in the join/clause. If the value of the parameter is greater than -1, I need to consider the value in my WHERE clause. I would prefer to NOT use an IF-ELSE statement because it seems sloppy for this case.

I saw this question here. However, it did not work for me. I think the reason why is because each of the columns in my table can have a NULL value. Someone pointed this scenario out in the fifth answer. That appears to be happening to me.

Is there a slick approach to my question? Or do I just need to brute force it (I hope not :().

Thank you!

© Stack Overflow or respective owner

Related posts about sql-server