• I have to agree that this is a very elegant solution, but can be very bad for performance.

    Imagine you have a table with 1 million names in it, and you have indexes on first name and on last name.

    If you query that table by first name, SQL will use the first name index.

    If you query that table by last name, SQL will use the last name index.

    If you use the boolean logic, SQL will likely read the whole table because it won't know which one to use. Even if SQL is smart enough to choose one index, it will save that query plan for every execution, and if the conditions change (i.e. first time read by first name and now is selecting by last name) it will use the wrong index.

    For cases like this dynamic SQL is usually better, since SQL will have a differnt plan for each type of search.

    Please note that this is not an issue in example case because the name in this case is always between '%' , and SQL would not use an index anyway.