Are there any guidelines on when you should and should not write a long complicated 2000+ line stored procedure?
In my specific case this stored procedure contains a lot of if/then, case, goto and branching statements. It works by constructing SQL queries depending on inputs and results of queries and uses the execute statement to run the constructed queries. It can execute several constructed queries in one call and uses results of those queries to construct other queries to run.
It is pretty messy and hard to understand. Debugging is tough, the only way to know what is going on is to step through a call to see what it’s doing. There barely any exception handling or logging. Maintaining it is a pain. In fact, no one really knows what it does or how it was created and if we had to make modifications to it we would have to take a “cross your fingers and hope for the best” approach. But, I think it was done this way for performance reasons.
This procedure is used by many applications. The only other way I can think to do something like this is through a web service. It would probably be comparable in complexity, but a lot easier to understand. However, it would probably be multiple times slower as it would still have to make several calls to the database for 1 request.
So, my question(s) are, how do we decide when and when not to write long stored procedures?
Is there something I’m missing or do we just have to put up with our monstrous stored procedure?
Are there ways to structure and break down stored procedures into smaller components so they are easy to understand?
Will a stored procedure always be faster than anything else and the right choice when you need to make many calls to the database?
I’m not sure that it’s ever a good idea to write 2000+ LOC for a single function. My initial reaction is to say that your sproc should be broken up into smaller functions (table-valued or scalar, whatever is appropriate) and stored procedures (for non-query operations). However, that might just be moving the LOC around rather than simplifying the actual operation. Unfortunately, without any source code posted it’d be difficult to give more specific advice.