It’s also a common practice when people are building the sql query programmatically, it’s just easier to start with ‘where 1=1 ‘ and then appending ‘ and customer.id=:custId’ depending if a customer id is provided.
So you can always append the next part of the query starting with ‘and …’.
The 1=1 is ignored by always all rdbms. There is no tradeoff executing a query with WHERE 1=1.
Building dynamic WHERE conditions, like ORM frameworks or other do very often, it is easier to append the real where conditions because you avoid checking for prepending an AND to the current condition.
stmt += “WHERE 1=1″;
if (v != null) {
stmt += (” AND col = ” + v.ToString());
}
This is how it looks like without 1=1.
var firstCondition = true;
…
if (v != null) {
if (!firstCondition) {
stmt += ” AND “;
}
else {
stmt += ” WHERE “;
firstCondition = false;
}
stmt += “col = ” + v.ToString());
}