• I would be interested to see the relative performance of the article's method versus the following approach, which is equally powerful but (I think) can be optimised much more easily by the query engine:

    -- declare condition variables as in the article
    if @companynameOp='' and @countryOp=''
      select customerid, companyname, country from customers
    else if @companynameOp='' and @countryOp='eq'
      select customerid, companyname, country from customers where country like @country
    else if @companynameOp='' and @countryOp='bg'
      select customerid, companyname, country from customers where country like @country+'%'
    -- etc etc
    else if @companynameOp='ct' and @countryOp='ne'
      select customerid, companyname, country from customers where companyname like '%' + @companyName +'%' and country not like @country
    -- etc etc

     

    OK it's much more typing but that shouldn't really be considered an issue. As you can see, we are handling each possible pair of comparison operators separately, and each select statement can use indexes, be optimised, etc.