I recently encountered a case where I needed a sql result set to account for the value of an "Include inactive" checkbox in the UI. The business requirements were: 1) If the user doesn't check the box, display only active records, 2) If the user checks the box, display both active and inactive records. The active field was a bit in the database table, so all records were either active or inactive. My first thought was that for case 2, no WHERE criteria would be necessary for the sql statement, whereas case 1 would require "Active = 1" criteria. This seemed to point to the need to dynamically construct a sql statement based on the inclusion/exclusion of that where clause.
I was working with an existing parameterized sql statement, so really didn't want to switch over to a stored procedure and start dynamically building the sql within the sproc (not to mention building the sql dynamically isn't the cleanest approach to begin with). After a little thought, I realized that I could solve the problem within the same parameterized sql statement simply using IN criteria.
Here's a stripped down version of the sql. The @IncludeInactive parameter comes from the checkbox value
SELECT UserName FROM Users WHERE Active IN (0, @IncludeInactive)
Case 1: Checkbox not checked, don't include inactive users
SELECT UserName FROM Users WHERE Active IN (0, 0)
Case 2: Checkbox checked, include inactive users
SELECT UserName FROM Users WHERE Active IN (0, 1)
Nothing ground-breaking here, but much less code to maintain and a lot cleaner than dynamic sql.