The curse and blessings of dynamic SQL (2004-2022)
sommarskog.se
sommarskog.se
Edit: the sanest solution, then and now, is probably to select between a number of alternative static queries based on what index values have been supplied, and to use the 'is null or' construction for other filters.
This approach allows us to have a one-liner to enable searching in a grid, and scales well to grids with millions of rows and 50+ columns.
Of course the user might issue a search on a single non-indexed column, but this hasn't been an issue in practice. Either it happens very seldom, or they've already filtered on something indexed.
We also have use dynamic SQL for cases where we needed to select from different tables or views depending on some run-time condition, or similar. No direct user input there though so don't really need the safety that parameters bring.
I mean clearly we could just maintain multiple near-identical static queries and just pick the right one, rather than have a single dynamic query.
However, then you're stuck having to keep them in sync and you will eventually screw that up.
In our case, we'll resort to that if the alternatives are too painful, which so far has just been a few places amongst our several hundred queries.
There's no better tool generally speaking, there's the tool more suited for your use case, RDBMS are now very good, but not for everything.
I spent a good couple months translating it to C#, and making a few improvements while I was at it. Absolutely 100% worth it, those improvements made it possible to add features to make supporting the product much easier for frontline support to help people.
The only benefit is we could cowboy updates directly to prod instantly. If that made your heart skip a beat, congrats, you actually give a damn about change management!
I still don't get it
And dynamic would be like query("SELECT * FROM foo WHERE bar = $1 AND " + var ? "true" : "EXISTS(...)", "baz"). The value of `var` can change the entire query plan. Obviously this is a little more dangerous, but sometimes you need the flexibility.
I read the article and the comments here and have no idea if my definition matches theirs.
Which, fundamentally, is what dynamic SQL is.
When I realized the article was going to be about that kind of dynamic SQL, I closed the tab.
Building SQL queries dynamically is fine.
Doing it within the SQL server is an abomination.
A DBA is likely doing things like looping over metadata tables and running
'grant select on ' || some_table || ' to ' || some_user
While an app developer with free reign will end up doing something much more complex, and much harder to reason about/tune(The characters never actually get escaped with parameterisation - they are not part of the query text when it is parsed so can’t affect it - hence parameterising a value in sql query replaces the need to escape it with something much more robust.)
I am perfectly able to:
SELECT ACCOUNT_STATUS FROM DBA_USERS WHERE USERNAME=:var;
But I cannot: ALTER USER :var ACCOUNT UNLOCK;
I really don't understand why that capability wasn't added a decade ago, as loud as the advice is to avoid hard parsing.Dynamic SQL is used on practice to fix a lot of problems the SQL people have been refusing to touch.
Most of the OLAP / warehousing stuff I used to lean on dynamic sql for can be done more cleanly, with the benefit of easy source control.
The article could have made that much more clear.
It reminds me of the bash-completion project which is the default on Debian-based distros.
It tries to parse bash IN BASH. This is a very bad idea.
Oil Uses Its Parser For History And Completion - https://www.oilshell.org/blog/2020/01/history-and-completion...
I would also argue that parsing bash in C is not a great idea either!
Dynamic SQL exists just because it can. It had absolutely no use case outside of I want to slap together some SQL statements real quick and don't feel like using anything except the DB.
Your best bet when writing SQL in strings is make it "greppable". Naming a table "user" is very convenient until someone asks, "What will be affected by this change to the user table?" and you're stuck with needles in a haystacks. Tossing in a tiny prefix e.g. "tblUser" will annoy some people but then your signal-to-noise is ideal.
But yeah, concatenating bits and pieces of text together into queries can rapidly escalate out of control without a conservative/judicious approach.
This is so ridiculous it hurts
> Tossing in a tiny prefix e.g. "tblUser" will annoy some people but then your signal-to-noise is ideal.
just no.