Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided i...
For further actions, you may consider blocking this person and/or reporting abuse
Hey javinpaul, nice article! I just wanted to add my 2 cents here, since I have also developed a style writing big and complex SQL queries.
In your example, I would write the query like this:
This style allows easily commenting out columns and criteria, identifying the tables and joins, along with the join criteria.
One more advanced example with CTE would be something like this:
Finally, you could write UNIONs easily with this style:
Hello @gpower2 , I must say your style is better than mine :-) thanks for sharing with us.
I'm really glad you liked it @javinpaul !
Always happy to see fellow devs trying to be better and share their thoughts with the community!
@gpower2 I like your style and thats what we enforcing at our work place.
God knows how much I hate those implicit joins. You never know what's going on on this kind of queries.
My queries are usually more or less like this:
Sorry, I didn't get your point, isn't join is explicit on my query example?
It's not a point, I'm just saying I rather do queries the same way you did. I'm not talking about your example, I'm talking about most people I've been working with.
Looks like people are stuck in time sometimes. Huge SQL lines, list of tables instead of JOINs, a lot of business rules in procedures and triggers when they should have been added to the application backend, etc.
Ah, I see from where you are coming. I have seen such queries, and, yes, they are very messy to deal with. I can understand your pain, especially if you have to make a change on such code.
I'm of the opinion that using a formatter can actually familiarize the developer with accepted formatting guidelines. They reinforce uniformity.
While not everyone can afford a Red Gate formatter, I've had good experiences with architectshack.com › Poor Man's T-SQL Formatter which has a free plug-in for SSMS.
VSCode also has it. Good enough. Great for when your console spits out a huge query in one line
Yes, a formatter is a good option and it can also give consistency. Does VSCode has by default or there is any extension/plugins?
Now you've got me. I'm not sure if from a plugin.
I've been using this one for SQL Server:
github.com/microsoft/vscode-mssql
And this one for other (recent project, not so many contributors, so not yet so full of features):
github.com/mtxr/vscode-sqltools
If select query and right click doesn't show a "format code/file", it probably comes from one of these.
Hey javinpaul, thanks for encouraging proper capitalization in order to have readable queries. When I read very long and complex queries, I often get lost as well. Do you also have techniques how to cope with multiple UNIONs and a WITH sections in one query? Is one big query better or worse than multiple small but comprehensive ones?
Sometimes I had struggles reading parts of your article. Could it be that your images are jumbled up? The first query (1.0) is not written all in one line as the description states. The first image of 3.0 is not indented. Also, in the second one of 3.0, shouldn't it be 1=1 instead of 1==1? There you could actually mention this as a cool trick, to have the possibility to comment conditions out (without commenting the WHERE keyword)!
Also, there is a typo:
tecach -> teach
Thank you for the links as well! Haven't heard of T-SQL yet.
Hello @victor , first of all very valuable comment.
Once again thanks for the comment and making the article better.
Hi
I found this article rather obvious, unless someone just started with sql development.
I would recommend using ssmsboost. Google it. It's a free ssms add in if you use the community version. There are so many useful features there that you will soon not be able to work without it. Do yourself a favour, try it out and understand its features. Oh BTW, it has an sql formatter. Not thrilled with it, but you get quick consistency, perfect to help with diffs.
Thanks @sagie, it may sound obvious but not really as everybody has there own style and many developer doesn't give formatting a thought when writing SQL query, mostly because you don't need it when your query is small. But, when you have more than 4 tables joining, and feting 50+ columns with 10+ conditions it make sense to format it. I didn't know about ssmsboost, thanks for the pointer, I'll take a look.
There is also one more big advantage in adding "1=1" to your WHERE-clause: You can add conditions dynamically without checking if there already is a condition. Without 1=1 as the first condition you have to check if you also have to add an "AND" (if condition 2+) or not (first condition).
Indeed, thanks for comment @Jannik
Nice post
Thanks Ben and a belated happy birthday :-)