Poorly written SQL is the root of all evil!

SQL as well as database design is what gives the likes of ColdFusion and all middle tier app. server solutions a bad name. I have seen lines and lines of SQL code kluge and clog up applications. This leaves dba and developers pointing fingers and makes for a frustrating experience. What happens in this case is a developer will use ColdFusion as the scapegoat or opt to move for a hardware upgrade... UGGG! Many of these issues can be cleared up with a very simple change and some simple concepts that the programmer didn't realize was available.

Some of these concepts are proper database design and alternative query methods such as sub queries at the field level and in the where clause, forcing nulls when data is not found and handling appropriately with ISNULL or NVL. (Just a brief example of something one might not have thought of).

Here's a good one, how about nesting ISNULL or NVL to handle a null from 2 subqueries that return null in the inner null. It's fast and can be done with one line of code rather than creating multiple cursors to load data to a temp table to ultimately pick out or append records that apply to a specified filter.

Many techniques such as these go undiscovered due to lack of documentation (book or otherwise) and pressing deadlines, which can put a coders head in a bubble. As a coder, when you get that, "there's gotta be an easier, more concise way" feeling... know that there always is and it may be time to take a Google break or thumb through that Rafe Colburn "Using SQL" book you paid 40 bucks for on Amazon.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
now all we wannabe dba/cfdeveloper needs is a common sense guide to proper SQL to avoid these pitfalls
# Posted By Michael White | 4/4/06 10:27 AM
HA, no doubt Michael. For what it's worth, the book I mentioned in my blog is a great start, especially if you are writing stored procs in SQL Server and Oracle (I believe I've blogged about it in the past). Ben Forta has some excellent sql books as well.
# Posted By Jeff Bouley | 4/4/06 11:09 AM
this a book available, it's called usenet. Use it!!!

In all seriousness, there is a wealth of information about SQL and best practices out there. Books are great, but they tend to get outdated. Personally I think that all developers who use SQL Server should visit http://www.sqlservercentral.com/.

It's probably one of the best sites out there for SQL optimiztion information. Also there are TONS of usenet newsgroups out there.
# Posted By tony petruzzi | 4/4/06 11:49 AM
Yeah, I know...

I just recently has to wade through a large 8 page StoredProc to figure out what someone was doing.

Turns out, I just needed a SQL with two inner joins.

I am with you Michael!
# Posted By jdanylko | 4/4/06 12:59 PM
Here are the couple of the classics links i use for my sql studies...

http://doc.ddart.net/mssql/sql2000/html/
MS SQL Books Online

That's usually a good starting point..
# Posted By Craig M. Rosenblum | 4/4/06 1:26 PM
Tony, I totally agree on your usenet comment. But in regard to SQL (procedure development or in-line) and database design, it is not a technology that has been moving as fast as middle tier or any other IT technology for that matter, making an older book still relevant today. Solid concepts was where I was going with my book comment. Thanks for your response sir.
# Posted By Jeff Bouley | 4/4/06 6:40 PM
I just had a highly paid gentleman ask if I could help him debug his SQL. His result set was not returning any records.

His where clause had AND POLICY = 'NULL'.

sigh...shaking head.
# Posted By Ryan Kelley | 5/31/06 5:07 PM

Copyright Strikefish, Inc., 2005. All rights reserved.