Strategies for queries against bit columns


Recently someone posted a question where they couldn’t quite figure out how to construct a predicate based on a bit parameter. They tried to write a procedure like this, which wouldn’t parse, of course:

CREATE PROCEDURE dbo.whatever @flag bit = 0 AS SELECT * FROM dbo.tablename WHERE IF @flag = 1 flag_column = 1 IF @flag <> 1 flag_column = 0;

I explained that you can’t have control-of-flow inside a SQL statement like that, at least not in T-SQL. And that the way you should do it is as follows, if the table is sensible and the bit column doesn’t