Are there any recommendations on when you should and should not write a long complex 2000-line stored procedure?
In my particular case, this stored procedure contains many if, then, goto, and branch commands. It works by constructing SQL queries depending on the input data and query results and uses the execute statement to execute the constructed queries. It can execute several built queries in one call and uses the results of these queries to build other queries to run.
This is pretty dirty and hard to understand. Debugging is tough, the only way to know what is going on is to go through the call to see what it is doing. Almost all exception handling or registration. Maintaining this is pain. In fact, no one knows what he is doing and how it was created, and if we had to make changes to it, we would have to take a βcross with our fingers and hope for a betterβ approach. But I think this was done for performance reasons.
This procedure is used by many applications. The only way I can do something like this is with a web service. It would probably be comparable in complexity, but much easier to understand. However, it will probably be several times slower, since it will still have to make several calls to the database for 1 query.
So, my question (s): how do we decide when and when not to write long stored procedures?
Is there something I am missing, or do we just need to put up with our monstrous stored procedure?
Are there ways to structure and break down stored procedures into smaller components so that they are easy to understand?
Will the stored procedure be always faster than anything else, and the right choice when you need to make many calls in the database?
source share