Skip to content
Advertisement

Is there a better way to write this gross SQL?

So I’m creating a query for a report that could have several optional filters. I’ve only included client and station here to keep it simple. Each of these options could be an include or an exclude and could contain NULL, 1, or multiple values. So I split the varchar into a table before joining it to the query.

This test takes about 15 minutes to execute, which… just won’t do :p Is there a better way? We have similar queries written with dynamic sql, and I was trying to avoid that, but maybe there’s no way around it for this?

Advertisement

Answer

First of all, I always tend to mention Erland Sommarskog’s Dynamic Search Conditions in such cases.

However, you already seem to be aware of the two options: one is dynamic SQL. The other is usually the old trick and (@var is null or @var=respective_column). This trick, however, works only for one value per variable.

Your solution indeed seems to work for multiple values. But in my opinion, you are trying too hard to avoid dynamic sql. Your requirements are complex enough to guarantee it. And remember, usually, dynamic sql is harder for you to code, but easier for the server in complex cases – and this one certainly is. Making a performance guess is always risky, but I would guess an improvement in this case.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement