Jeremy D. Miller -- The Shade Tree Developer

Sponsors

The Lounge

Syndication

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Good and Evil in the Garden of Stored Procedures

Periodically I see or engage in an argument over the usage of stored procedures. I think a lot of these types of arguments are somewhat unproductive because the combatants are thinking and talking about radically different things. Besides, I think the argument over the proper role of stored procedures in data access is often obfuscated by the misuse of stored procedures for things other than data access. My opinion has changed quite a bit through the years, so I try to stay out of these arguments.

However, my development group has to make a change soon in our philosophy for using stored procedures (or hit a brick wall). The way my company has used T-SQL in the past is jeopardizing our future development (besides, I’m an Oracle guy and I think T-SQL is butt-ugly). We’re a product development company that has grown by leaps and bounds from a startup venture and the existing codebase has grown somewhat chaotically. To keep the growth going and accommodate new customers both here and abroad we’re going to need to localize the application for different languages and port our main application to Oracle. Toss in the ongoing adoption of TDD and continuous integration as a means of creating better software quicker, and the usage of stored procedures has to be reevaluated.

What’s Good about Stored Procedures

Before I start ranting about the grotesque misuse of stored procedures I’m dealing with, here is what I think is useful about stored procedures.

Using stored procedures externalizes the SQL from the compiled code. I really like having the SQL in an easy to read format instead of built by string concatenation in the code. This is also great if you need to optimize the stored procedure for performance. I haven’t seen that come up very often, but it only seems to happen on systems where the SQL is embedded in the code. There’s also the issue of allowing a DBA to alter or write stored procedures independently of the code. I have generally had control of both database and code throughout my career, so I’m always suspicious of DBA’s writing stored procedures instead of developers. I do think that dynamically constructed SQL can result in systems that are hard to debug, but that can be beaten with just a little judicious instrumentation in your data access layer.

Performance is an advantage of stored procedures, but maybe not enough by itself to justify stored procedures over other approaches. I think the performance advantage is either largely mitigated by the ability of newer database engines to cache execution plans or simply a case of premature optimization. If you have an opportunity to batch up a set of updates or simply reduce the amount of information you send over the wire to the database a stored procedure certainly leads to a performance improvement. Otherwise, I think the stored procedure advantage is too slight to be a serious consideration.

Security is another advantage that is often cited as an advantage of using stored procedures. On one hand, stored procedures quickly mitigate vulnerability to SQL injection attacks. On the other hand, the database can be tightened down so that no ad hoc SQL can be executed, stopping security breaches right at the start. Personally, I abhor this idea myself because it drags down the development team’s velocity. I don’t see why it’s necessary, but it is an option.

Most of this goodness can be happily achieved with nary a stored procedure. Parameterized queries can eliminate SQL injection vulnerabilities while providing comparable performance. The data access layer we built (I might release this as part of StructureMap someday) for our last project already externalizes SQL from the code for ease of maintenance and decoupling from the database.

When Stored Procedures Go Bad

While there is nothing inherently evil about stored procedures, developers often misuse stored procedures. I think stored procedures should be nothing but basic CRUD. I wrote in an earlier post that “IF/THEN/ELSE” logic in stored procedures is a design smell.

Stored procedures are a terrible place to create business logic. Development environments for the .Net languages or Java are far better suited for developing applications. Intellisense support, debugging, and refactoring are all inferior with stored procedure languages. T-SQL is a procedural language that pretty well forces you into the transaction script style of organizing business logic. One thing I’m observing in the reams of T-SQL I’m wrestling with is the obscene amount of code duplication. Now that we have to support Oracle as well, this duplication stands out like a sore thumb. An OO language can do a much better job of code reuse.

Here’s my list of things I’ve seen put into stored procedures that should never be in a stored procedure.

  1. User input validation that creates the validation messages seen on user screens. I can’t even begin to describe how stupid this is.
  2. Creating HTML, enough said.
  3. Security authorization logic. This is definitely a middle tier responsibility. I’ve used joins to some sort of permission table in select statements to filter records. I think that’s fine, but using procedural logic or even role checks inside stored procedures is nuts. Security is volatile and sensitive. Put it where it’s easy to test. For that matter, always leave a way to test your business logic without security checks if you can. Security always makes code harder to unit test. By wrapping security up with business logic in a bag of stored procedure goo, you’ve essentially shot yourself in the foot for testability.
  4. Business logic. It’s easier to write and test in C#, period. Splitting logic between stored procedures and middle tier code is even worse, but I’ve seen it done many times.

Posted Tue, Jul 5 2005 6:58 PM by Jeremy D. Miller

[Advertisement]

Comments

Jeremy D. Miller -- The Shade Tree Developer wrote Unit Testing Business Logic without Tripping Over the Database
on Wed, Oct 12 2005 3:04 PM
A fairly common topic with TDD practitioners, both newbie and experienced, is how the heck to unit test...
sam smith wrote re: Good and Evil in the Garden of Stored Procedures
on Wed, May 17 2006 2:53 PM
I've seen tons of C#/C++ code that is procedural in nature and has lots of duplicated code.    If you know what you are doing, you don't have to duplicate code using T-SQL. Yeah, it's harder to avoid duplicating code, but you can minimize the duplication.    In short, don't blame the technology, blame the person who didn't know what they were doing when they wrote the code.  or blame his manager.
Richard Lack wrote re: Good and Evil in the Garden of Stored Procedures
on Mon, Oct 29 2007 11:58 AM

Interesting comments, sam.  A few things that came to mind reading your comments:

"[With stored procedures] If you know what you are doing, you don't have to duplicate code using T-SQL. Yeah, it's harder to avoid duplicating code, but you can minimize the duplication."  If it's harder, then that means it takes longer, and there's a much greater change of making mistakes in the process.  That means it takes longer and costs more money, requires smarter people, or produces a worse product, or some combination.  

You mentioned "don't blame the technology, blame the person who didn't know what they were doing when they wrote the code."  It's not the technology so much as the way it's being used.

Slevdi wrote re: Good and Evil in the Garden of Stored Procedures
on Mon, Nov 19 2007 2:57 AM

@jeremy "4.  Business logic. It’s easier to write and test in C#, period. Splitting logic between stored procedures and middle tier code is even worse, but I’ve seen it done many times."

This depends. If the business logic is really to maintain the integrity of the database, then it should be *in* the database. If it affects workflow but not data integrity, it should be *out* of the database. That said, much business logic (including workflow) can be - and usually is - controlled by data in the database, so the right place to do this kind of  business logic is also in the database. Your other examples were presentation and application logic, and I agree totally with you. All  this IMHO of course.

As far as using the programming language to develop SQL, I have found it fine for simple one line statements. But I have only used one line statements in simple situations. Most database accesses are complex in my experience and I couldn't imagine coding them in C# (even with an ORM query language) when there is usually a great visual interface available in most RDBMSes.

I guess I am disagreeing with you.

I prefer to develop by joining the tables visually, adding sub-queries and unions that were developed in a separate visual interface, and do immediate testing without having to recompile the application.

Instead of developing query languages in application code like ORMs and LINQ - all of which I regard as retrograde steps in computing - , it would be much more sensible for vendors to improve the productivity inside the db and provide better tools to manage the development of tables, views, stored procedures and user-defined functions. TDD for sprocs would be a good start, as would built in source control.

Your article, to be fair, points out the evil of *misusing* sprocs. I agree on that one, but surely the answer is to give your staff some db training if they are so bad at it? Not just throw out sprocs. Check your budget for training and you will undoubtedly see 0$ for relational database courses. Just guessing, but I bet I am right.

marky wrote re: Good and Evil in the Garden of Stored Procedures
on Mon, Apr 7 2008 11:20 AM

Interested if you know of any tools (ideally opensource) which can analyze stored procs/ other sql code and produce any stats on  quality or complexity rating etc.

Rob Conery wrote You’re Not Your Data Access
on Thu, Jun 11 2009 11:51 PM

Seems I touched off a bit of a “swirl” with a comment I made on my last blog post: I think, in general

You???re Not Your Data Access wrote You???re Not Your Data Access
on Fri, Jun 12 2009 8:04 AM

Pingback from  You???re Not Your Data Access

You???re Not Your Data Access wrote You???re Not Your Data Access
on Fri, Jun 12 2009 10:03 AM

Pingback from  You???re Not Your Data Access

Business Logic in Stored Procedures is bad! « Rounded Corners wrote Business Logic in Stored Procedures is bad! « Rounded Corners
on Tue, Jun 30 2009 12:21 AM

Pingback from  Business Logic in Stored Procedures is bad! « Rounded Corners

Stored Procedures are EVIL « TheUnical Technologies Blog wrote Stored Procedures are EVIL « TheUnical Technologies Blog
on Sat, Jul 25 2009 11:22 AM

Pingback from  Stored Procedures are EVIL «  TheUnical Technologies Blog

Phil wrote re: Good and Evil in the Garden of Stored Procedures
on Thu, Aug 6 2009 4:36 PM

If everyone in your organization use stored procedures then you better use it too. If everyone write inline SQL, then follow suit. Thats how I sink the endless debate. I know a guy that insisted on inline SQL in a company where other developers wanted stored proc. His code is universally considered bad. There many be cases of the reverse. Just follow suit and call it a day.

Stored Procedures are EVIL | TheUnical Technologies Blog wrote Stored Procedures are EVIL | TheUnical Technologies Blog
on Sat, Sep 26 2009 7:30 AM

Pingback from  Stored Procedures are EVIL | TheUnical Technologies Blog

Jeremy Maddrey wrote Un-LINQ Stored Procedures
on Wed, Nov 18 2009 2:22 PM

Un-LINQ Stored Procedures

Add a Comment

(required)  
(optional)
(required)  
Remember Me?